[][src]Trait photonix::focus::ModifyOption

pub trait ModifyOption<Value> where
    Self: Sized
{ fn modify_option(self, f: impl FnOnce(Value) -> Value) -> Option<Self>; }

Updates the field of a container (enum) by applying the provided function on the target value. Consumes the original container (unless it implements Copy), returns the updated one wrapped in an Option.

Auto-derive creates the implementation for all fields in enum variants, works for enums (both with named and unnamed fields) if

  • all fields have concrete types,
  • all field types are different.

If you are using the derive macro, and the actual enum variant happens to be different from the target variant, it returns None (see example).

Examples

 #[derive(Debug, ModifyOption, PartialEq)]
 pub enum Deviation {
     Signed(i32),
     Unsigned(u32),
 }

 let d = Deviation::Signed(-10);
 let d2 = Deviation::Unsigned(10);

 let d_neg = d.modify_option(|n: i32| -n);

 assert_eq!(Some(Deviation::Signed(10)), d_neg);

 let d2_neg = d2.modify_option(|n: i32| -n);

 assert_eq!(None, d2_neg);

Required methods

fn modify_option(self, f: impl FnOnce(Value) -> Value) -> Option<Self>

Loading content...

Implementors

Loading content...