From 79afcde7d5560df1d87b2c8b65b809720727fc00 Mon Sep 17 00:00:00 2001 From: Lars Volkheimer <40475367+laesse@users.noreply.github.com> Date: Tue, 13 Feb 2024 18:51:05 +0100 Subject: [PATCH] IMPROVEMENT better code maintainablity by removing duplicated code (#2) * feat: adhere to the strictest clean code standards and DRY * docs: document enterprise grade arichitecture * fix: copy pasta * Small improvements * make clippy happy --------- Co-authored-by: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> --- README.md | 5 +++++ src/boolean_toggler.rs | 22 ++++++++++++++++++++++ src/lib.rs | 9 +++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/boolean_toggler.rs diff --git a/README.md b/README.md index 591521f..176538d 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,11 @@ This crate supports a professional re-export of the trait, `BoolToggleExt`. It is only available when compiling with `--cfg enterprise_license` and obtaining an enterprise license. For license inquiries, send mail to `/dev/null`. +## Architecture + +This Crate features the world's most resilient bit flipping algorithm. Its code adheres to the highest clean code standards +and aims to provide a maintainable and future proof solution to all the boolean toggling needs there are. + ## MSRV The minimum supported Rust version of this crate is 1.1000.0. diff --git a/src/boolean_toggler.rs b/src/boolean_toggler.rs new file mode 100644 index 0000000..b89bc46 --- /dev/null +++ b/src/boolean_toggler.rs @@ -0,0 +1,22 @@ +use super::TogglingIsALifestyle; + +pub struct BooleanToggler<'a> { + bool_to_toggle: &'a mut bool, +} + +impl TogglingIsALifestyle for BooleanToggler<'_> { + #[inline] + fn toggle(&mut self) { + // i'm enterprise fast + *self.bool_to_toggle ^= true; + } +} + +pub struct BooleanTogglerFactory; + +impl BooleanTogglerFactory { + #[inline] + pub fn create_boolean_toggler(bool_to_toggle: &mut bool) -> BooleanToggler { + BooleanToggler { bool_to_toggle } + } +} diff --git a/src/lib.rs b/src/lib.rs index ece3177..3e785ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,10 @@ #![doc = include_str!("../README.md")] #![cfg_attr(docsrs, feature(doc_cfg))] +mod boolean_toggler; + +use boolean_toggler::BooleanTogglerFactory; + // Extension trait for toggling a bool. pub trait TogglingIsALifestyle { /// Toggle the bool. @@ -17,7 +21,7 @@ pub use TogglingIsALifestyle as IAmTheToggler; impl TogglingIsALifestyle for bool { fn toggle(&mut self) { // i am so smart - *self ^= true; + BooleanTogglerFactory::create_boolean_toggler(self).toggle(); } } @@ -26,7 +30,8 @@ impl TogglingIsALifestyle for [bool; N] { fn toggle(&mut self) { // i am so fast for b in self { - *b ^= true; + let mut bool_toggler = BooleanTogglerFactory::create_boolean_toggler(b); + bool_toggler.toggle(); } } }