| 1 | /// Implement [`AsRef`] for a struct. |
| 2 | /// |
| 3 | /// The first argument is that of the struct to create the impl for and the second is the type to |
| 4 | /// produce a reference for. |
| 5 | /// |
| 6 | /// # Examples |
| 7 | /// With a newtype struct: |
| 8 | /// ``` |
| 9 | /// use impl_more::impl_as_ref; |
| 10 | /// |
| 11 | /// struct Foo(String); |
| 12 | /// impl_as_ref!(Foo => String); |
| 13 | /// let foo = Foo("bar".to_owned()); |
| 14 | /// assert_eq!(foo.as_ref(), "bar"); |
| 15 | /// ``` |
| 16 | /// |
| 17 | /// With a named field struct and type parameters: |
| 18 | /// ``` |
| 19 | /// use impl_more::impl_as_ref; |
| 20 | /// |
| 21 | /// struct Foo<T> { inner: T } |
| 22 | /// impl_as_ref!(Foo<T> => inner: T); |
| 23 | /// let foo = Foo { inner: "bar".to_owned() }; |
| 24 | /// assert_eq!(foo.as_ref().as_str(), "bar"); |
| 25 | /// ``` |
| 26 | #[macro_export] |
| 27 | macro_rules! impl_as_ref { |
| 28 | ($this:ident $(<$($generic:ident),+>)? => $inner:ty) => { |
| 29 | impl $(<$($generic),+>)? ::core::convert::AsRef<$inner> for $this $(<$($generic),+>)? { |
| 30 | fn as_ref(&self) -> &$inner { |
| 31 | &self.0 |
| 32 | } |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | ($this:ident $(<$($generic:ident),+>)? => $field:ident : $inner:ty) => { |
| 37 | impl $(<$($generic),+>)? ::core::convert::AsRef<$inner> for $this $(<$($generic),+>)? { |
| 38 | fn as_ref(&self) -> &$inner { |
| 39 | &self.$field |
| 40 | } |
| 41 | } |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | /// Implement [`AsMut`] for a struct. |
| 46 | /// |
| 47 | /// The first argument is that of the struct to create the impl for and the second is the type to |
| 48 | /// produce a reference for. |
| 49 | /// |
| 50 | /// # Examples |
| 51 | /// With a newtype struct: |
| 52 | /// ``` |
| 53 | /// use impl_more::{impl_as_ref, impl_as_mut}; |
| 54 | /// |
| 55 | /// struct Foo(String); |
| 56 | /// |
| 57 | /// impl_as_ref!(Foo => String); |
| 58 | /// impl_as_mut!(Foo => String); |
| 59 | /// |
| 60 | /// let mut foo = Foo("bar".to_owned()); |
| 61 | /// foo.as_mut().push('!'); |
| 62 | /// |
| 63 | /// assert_eq!(foo.as_ref(), "bar!"); |
| 64 | /// ``` |
| 65 | /// |
| 66 | /// With a named field struct and type parameters: |
| 67 | /// ``` |
| 68 | /// use impl_more::{impl_as_ref, impl_as_mut}; |
| 69 | /// |
| 70 | /// struct Foo<T> { inner: T } |
| 71 | /// |
| 72 | /// impl_as_ref!(Foo<T> => inner: T); |
| 73 | /// impl_as_mut!(Foo<T> => inner: T); |
| 74 | /// |
| 75 | /// let mut foo = Foo { inner: "bar".to_owned() }; |
| 76 | /// foo.as_mut().push('!'); |
| 77 | /// |
| 78 | /// assert_eq!(foo.as_ref(), "bar!"); |
| 79 | /// ``` |
| 80 | #[macro_export] |
| 81 | macro_rules! impl_as_mut { |
| 82 | ($this:ident $(<$($generic:ident),+>)? => $inner:ty) => { |
| 83 | impl $(<$($generic),+>)? ::core::convert::AsMut<$inner> for $this $(<$($generic),+>)? { |
| 84 | fn as_mut(&mut self) -> &mut $inner { |
| 85 | &mut self.0 |
| 86 | } |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | ($this:ident $(<$($generic:ident),+>)? => $field:ident : $inner:ty) => { |
| 91 | impl $(<$($generic),+>)? ::core::convert::AsMut<$inner> for $this $(<$($generic),+>)? { |
| 92 | fn as_mut(&mut self) -> &mut $inner { |
| 93 | &mut self.$field |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | } |