[][src]Trait photonix::focus::Set

pub trait Set<Value> {
    fn set(self, new_value: Value) -> Self;
}

Updates the field of a container (struct/enum) with the provided value. Consumes the original container (unless it implements Copy), returns the updated one.

Auto-derive creates the implementation for all fields in structs and enum variants, works for structs and 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 the original version (see example).

Examples

 #[derive(Set)]
 pub struct Person {
     pub name: String,
     pub age: u8,
 }

 let joe = Person { name: String::from("Joe"), age: 42 };
 let joe_older = joe.set(43u8); // help the compiler by specifying the field type

 assert_eq!(43, joe_older.age);

 let joel = joe_older.set(String::from("Joel"));

 assert_eq!("Joel", joel.name.as_str());

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

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

 let d_new = d.set(10i32);

 assert_eq!(Deviation::Signed(10), d_new);

 let d2_new = d2.set(10i32);

 assert_eq!(Deviation::Unsigned(10), d2_new);

Required methods

fn set(self, new_value: Value) -> Self

Loading content...

Implementations on Foreign Types

impl<T> Set<T> for Option<T>[src]

Loading content...

Implementors

Loading content...