diff --git a/README.md b/README.md index 408e418..b652c16 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,11 @@ Getters are generated as `fn field(&self) -> &type`, while setters are generated These macros are not intended to be used on fields which require custom logic inside of their setters and getters. Just write your own in that case! ```rust -use getset::{CopyGetters, Getters, MutGetters, Setters, WithSetters}; +use std::sync::Arc; -#[derive(Getters, Setters, MutGetters, CopyGetters, WithSetters, Default)] +use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters}; + +#[derive(Getters, Setters, WithSetters, MutGetters, CopyGetters, CloneGetters, Default)] pub struct Foo where T: Copy + Clone + Default, @@ -30,6 +32,10 @@ where /// Multiline, even. #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")] public: T, + + /// Arc supported through CloneGetters + #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")] + arc: Arc, } fn main() { @@ -39,88 +45,134 @@ fn main() { assert_eq!(*foo.private(), 2); foo = foo.with_private(3); assert_eq!(*foo.private(), 3); + assert_eq!(*foo.arc(), 0); } ``` You can use `cargo-expand` to generate the output. Here are the functions that the above generates (Replicate with `cargo expand --example simple`): ```rust -use getset::{CopyGetters, Getters, MutGetters, Setters, WithSetters}; -pub struct Foo -where - T: Copy + Clone + Default, -{ - /// Doc comments are supported! - /// Multiline, even. - #[getset(get, get, get_mut)] - private: T, - /// Doc comments are supported! - /// Multiline, even. - #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")] - public: T, -} -impl Foo -where - T: Copy + Clone + Default, -{ - /// Doc comments are supported! - /// Multiline, even. - #[inline(always)] - fn private(&self) -> &T { - &self.private - } -} -impl Foo -where - T: Copy + Clone + Default, -{ - /// Doc comments are supported! - /// Multiline, even. - #[inline(always)] - pub fn set_public(&mut self, val: T) -> &mut Self { - self.public = val; - self - } -} -impl Foo -where - T: Copy + Clone + Default, -{ - /// Doc comments are supported! - /// Multiline, even. - #[inline(always)] +use std::sync::Arc; +use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters}; +pub struct Foo +where + T: Copy + Clone + Default, +{ + /// Doc comments are supported! + /// Multiline, even. + #[getset(get, set, get_mut, set_with)] + private: T, + /// Doc comments are supported! + /// Multiline, even. + #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")] + public: T, + /// Arc supported through CloneGetters + #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")] + arc: Arc, +} +impl Foo +where + T: Copy + Clone + Default, +{ + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] + fn private(&self) -> &T { + &self.private + } +} +impl Foo +where + T: Copy + Clone + Default, +{ + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] + fn set_private(&mut self, val: T) -> &mut Self { + self.private = val; + self + } + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] + pub fn set_public(&mut self, val: T) -> &mut Self { + self.public = val; + self + } + /// Arc supported through CloneGetters + #[inline(always)] + pub fn set_arc(&mut self, val: Arc) -> &mut Self { + self.arc = val; + self + } +} +impl Foo +where + T: Copy + Clone + Default, +{ + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] + fn with_private(mut self, val: T) -> Self { + self.private = val; + self + } + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] + pub fn with_public(mut self, val: T) -> Self { + self.public = val; + self + } + /// Arc supported through CloneGetters + #[inline(always)] + pub fn with_arc(mut self, val: Arc) -> Self { + self.arc = val; + self + } +} +impl Foo +where + T: Copy + Clone + Default, +{ + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] fn private_mut(&mut self) -> &mut T { - &mut self.private - } - /// Doc comments are supported! - /// Multiline, even. - #[inline(always)] - pub fn public_mut(&mut self) -> &mut T { - &mut self.public - } -} -impl Foo -where - T: Copy + Clone + Default, -{ - /// Doc comments are supported! - /// Multiline, even. - #[inline(always)] - pub fn public(&self) -> T { - self.public - } -} -impl Foo -where - T: Copy + Clone + Default, -{ - /// Doc comments are supported! - /// Multiline, even. - #[inline(always)] - pub fn with_public(mut self, val: T) -> Self { - self.public = val; - self - } + &mut self.private + } + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] + pub fn public_mut(&mut self) -> &mut T { + &mut self.public + } + /// Arc supported through CloneGetters + #[inline(always)] + pub fn arc_mut(&mut self) -> &mut Arc { + &mut self.arc + } +} +impl Foo +where + T: Copy + Clone + Default, +{ + /// Doc comments are supported! + /// Multiline, even. + #[inline(always)] + pub fn public(&self) -> T { + self.public + } +} +impl Foo +where + T: Copy + Clone + Default, +{ + /// Arc supported through CloneGetters + #[inline(always)] + pub fn arc(&self) -> Arc { + self.arc.clone() + } } ``` diff --git a/examples/simple.rs b/examples/simple.rs index 8814693..720106e 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,6 +1,8 @@ -use getset::{CopyGetters, Getters, MutGetters, Setters, WithSetters}; +use std::sync::Arc; -#[derive(Getters, Setters, WithSetters, MutGetters, CopyGetters, Default)] +use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters}; + +#[derive(Getters, Setters, WithSetters, MutGetters, CopyGetters, CloneGetters, Default)] pub struct Foo where T: Copy + Clone + Default, @@ -14,6 +16,10 @@ where /// Multiline, even. #[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")] public: T, + + /// Arc supported through CloneGetters + #[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")] + arc: Arc, } fn main() { @@ -23,4 +29,5 @@ fn main() { assert_eq!(*foo.private(), 2); foo = foo.with_private(3); assert_eq!(*foo.private(), 3); + assert_eq!(*foo.arc(), 0); }