1 | macro_rules! option_op_assign { |
2 | ($trait:ident, $op:ident, $op_name:ident $(, $extra_doc:expr)? $(,)?) => { |
3 | paste::paste! { |
4 | #[doc = "Trait for values and `Option`s " $op_name " assignment." ] |
5 | /// |
6 | /// Implementing this trait leads to the following auto-implementations: |
7 | /// |
8 | #[doc = "- `" [<Option $trait Assign>] "<Option<InnerRhs>>` for `T`." ] |
9 | #[doc = "- `" [<Option $trait Assign>] "<Rhs>` for `Option<T>`." ] |
10 | #[doc = "- `" [<Option $trait Assign>] "<Option<InnerRhs>>` for `Option<T>`." ] |
11 | /// - ... and some variants with references. |
12 | /// |
13 | /// This trait is auto-implemented for [`OptionOperations`] types implementing |
14 | #[doc = "`" $trait "<Rhs>`." ] |
15 | pub trait [<Option $trait Assign>]<Rhs = Self, InnerRhs = Rhs> { |
16 | #[doc = "Performs the " $op_name " assignment." ] |
17 | /// |
18 | /// `self` is unchanged if `rhs` is `None`. |
19 | $(#[doc = $extra_doc])? |
20 | fn [<opt_ $op _assign>](&mut self, rhs: Rhs); |
21 | } |
22 | |
23 | impl<T, Rhs> [<Option $trait Assign>]<Rhs> for T |
24 | where |
25 | T: OptionOperations + [<$trait Assign>]<Rhs>, |
26 | { |
27 | fn [<opt_ $op _assign>](&mut self, rhs: Rhs) { |
28 | self.[<$op _assign>](rhs) |
29 | } |
30 | } |
31 | |
32 | impl<T, InnerRhs> [<Option $trait Assign>]<Option<InnerRhs>, InnerRhs> for T |
33 | where |
34 | T: OptionOperations + [<Option $trait Assign>]<InnerRhs>, |
35 | { |
36 | fn [<opt_ $op _assign>](&mut self, rhs: Option<InnerRhs>) { |
37 | if let Some(inner_rhs) = rhs { |
38 | self.[<opt_ $op _assign>](inner_rhs) |
39 | } |
40 | } |
41 | } |
42 | |
43 | impl<T, InnerRhs> [<Option $trait Assign>]<&Option<InnerRhs>, InnerRhs> for T |
44 | where |
45 | T: OptionOperations + [<Option $trait Assign>]<InnerRhs>, |
46 | InnerRhs: Copy, |
47 | { |
48 | fn [<opt_ $op _assign>](&mut self, rhs: &Option<InnerRhs>) { |
49 | if let Some(inner_rhs) = rhs.as_ref() { |
50 | self.[<opt_ $op _assign>](*inner_rhs) |
51 | } |
52 | } |
53 | } |
54 | |
55 | impl<T, Rhs> [<Option $trait Assign>]<Rhs> for Option<T> |
56 | where |
57 | T: OptionOperations + [<Option $trait Assign>]<Rhs>, |
58 | { |
59 | fn [<opt_ $op _assign>](&mut self, rhs: Rhs) { |
60 | if let Some(inner_self) = self { |
61 | inner_self.[<opt_ $op _assign>](rhs) |
62 | } |
63 | } |
64 | } |
65 | |
66 | impl<T, InnerRhs> [<Option $trait Assign>]<Option<InnerRhs>, InnerRhs> for Option<T> |
67 | where |
68 | T: OptionOperations + [<Option $trait Assign>]<InnerRhs>, |
69 | { |
70 | fn [<opt_ $op _assign>](&mut self, rhs: Option<InnerRhs>) { |
71 | if let Some((inner_self, inner_rhs)) = self.as_mut().zip(rhs) { |
72 | inner_self.[<opt_ $op _assign>](inner_rhs) |
73 | } |
74 | } |
75 | } |
76 | |
77 | impl<T, InnerRhs> [<Option $trait Assign>]<&Option<InnerRhs>, InnerRhs> for Option<T> |
78 | where |
79 | T: OptionOperations + [<Option $trait Assign>]<InnerRhs>, |
80 | InnerRhs: Copy, |
81 | { |
82 | fn [<opt_ $op _assign>](&mut self, rhs: &Option<InnerRhs>) { |
83 | if let Some((inner_self, inner_rhs)) = self.as_mut().zip(rhs.as_ref()) { |
84 | inner_self.[<opt_ $op _assign>](*inner_rhs) |
85 | } |
86 | } |
87 | } |
88 | } |
89 | }; |
90 | } |
91 | |