Setting

Struct Setting 

Source
pub struct Setting<T> { /* private fields */ }

Implementations§

Source§

impl<T> Setting<T>
where T: Default + Clone,

Source

pub fn cb(&mut self, f: impl FnMut(&T, &T) + 'static)

Set a callback, guaranteed to be called every time inner value changes

Examples found in repository?
examples/readme.rs (line 12)
8fn main() {
9    let mut settings = Settings::default(); // equivalent of `Settings { port: 0, domain: "".to_string() }`
10    settings
11        .port
12        .cb(|old, new| println!("port: {:?} -> {:?}", old, new));
13    settings.port.set(1);
14    settings.port.set(10);
15    let _ = settings.domain.get();
16    let _ = settings.domain.as_ref();
17}
More examples
Hide additional examples
examples/sharing_state.rs (lines 14-18)
9fn main() {
10    let mut settings = Settings::default();
11    let s = Rc::new("abc".to_string());
12    {
13        let mut ss = s.to_string();
14        settings.str.cb(move |o, n| {
15            println!("{:?} -> {:?}", o, n);
16            ss.push_str("...");
17            println!("{:?} from closure", ss);
18        });
19    }
20    println!("{}", s);
21    settings.str.set("new".to_string());
22    println!("{:?}", settings);
23}
examples/simple.rs (line 14)
9fn main() {
10    let mut settings = Settings::default();
11    // Register callbacks for value changes
12    settings
13        .domain
14        .cb(|o, n| println!("domain: {:?} -> {:?}", o, n));
15    settings
16        .port
17        .cb(|o, n| println!("port: {:?} -> {:?}", o, n));
18    // Set new values, callbacks are now invoked when setting is changed
19    println!("{:?}", settings);
20    settings.domain.set("127.0.0.1".to_string());
21    settings.port.set(1337);
22    println!("{:?}", settings);
23}
examples/more_complex.rs (line 14)
11fn main() {
12    let mut settings = Settings::default();
13
14    settings.cell.cb(|o, n| println!("{:?} -> {:?}", o, n));
15    settings.vec.cb(|o, n| println!("{:?} -> {:?}", o, n));
16
17    // Field name should be empty by default
18    println!("name should be empty by default: {:?}", settings.str.get());
19
20    // Change Settings struct, user callbacks should be called if they were are assigned
21    settings.str.set("new_name".to_string());
22    settings.cell.set(10.into());
23    settings.cell.set(2222.into());
24
25    // Changing Vec elements is a bit more tedious
26    let mut vec = settings.vec.get();
27    vec.extend([1, 2, 3, 4]);
28    settings.vec.set(vec);
29
30    println!("{:?}", settings);
31}
examples/real_world_mpsc.rs (lines 16-19)
12fn main() {
13    // Settings
14    let (sender, receiver) = channel();
15    let mut settings = Settings::default();
16    settings.port.cb(move |old, new| {
17        let old_and_new = (old.clone(), new.clone());
18        sender.send(old_and_new).unwrap();
19    });
20
21    // Port subscriber
22    thread::spawn(move || loop {
23        let port = receiver.recv();
24        if port.is_ok() {
25            println!(
26                "port changed: {:?} -> {:?}",
27                port.unwrap().0,
28                port.unwrap().1
29            );
30        }
31    });
32
33    // Port publisher
34    let mut changing_port = 0u16;
35    loop {
36        changing_port = changing_port + 1;
37        settings.port.set(changing_port);
38        thread::sleep(Duration::from_millis(500));
39    }
40}
Source

pub fn set(&mut self, new_val: T)

Set value

Examples found in repository?
examples/readme.rs (line 13)
8fn main() {
9    let mut settings = Settings::default(); // equivalent of `Settings { port: 0, domain: "".to_string() }`
10    settings
11        .port
12        .cb(|old, new| println!("port: {:?} -> {:?}", old, new));
13    settings.port.set(1);
14    settings.port.set(10);
15    let _ = settings.domain.get();
16    let _ = settings.domain.as_ref();
17}
More examples
Hide additional examples
examples/sharing_state.rs (line 21)
9fn main() {
10    let mut settings = Settings::default();
11    let s = Rc::new("abc".to_string());
12    {
13        let mut ss = s.to_string();
14        settings.str.cb(move |o, n| {
15            println!("{:?} -> {:?}", o, n);
16            ss.push_str("...");
17            println!("{:?} from closure", ss);
18        });
19    }
20    println!("{}", s);
21    settings.str.set("new".to_string());
22    println!("{:?}", settings);
23}
examples/simple.rs (line 20)
9fn main() {
10    let mut settings = Settings::default();
11    // Register callbacks for value changes
12    settings
13        .domain
14        .cb(|o, n| println!("domain: {:?} -> {:?}", o, n));
15    settings
16        .port
17        .cb(|o, n| println!("port: {:?} -> {:?}", o, n));
18    // Set new values, callbacks are now invoked when setting is changed
19    println!("{:?}", settings);
20    settings.domain.set("127.0.0.1".to_string());
21    settings.port.set(1337);
22    println!("{:?}", settings);
23}
examples/more_complex.rs (line 21)
11fn main() {
12    let mut settings = Settings::default();
13
14    settings.cell.cb(|o, n| println!("{:?} -> {:?}", o, n));
15    settings.vec.cb(|o, n| println!("{:?} -> {:?}", o, n));
16
17    // Field name should be empty by default
18    println!("name should be empty by default: {:?}", settings.str.get());
19
20    // Change Settings struct, user callbacks should be called if they were are assigned
21    settings.str.set("new_name".to_string());
22    settings.cell.set(10.into());
23    settings.cell.set(2222.into());
24
25    // Changing Vec elements is a bit more tedious
26    let mut vec = settings.vec.get();
27    vec.extend([1, 2, 3, 4]);
28    settings.vec.set(vec);
29
30    println!("{:?}", settings);
31}
examples/real_world_mpsc.rs (line 37)
12fn main() {
13    // Settings
14    let (sender, receiver) = channel();
15    let mut settings = Settings::default();
16    settings.port.cb(move |old, new| {
17        let old_and_new = (old.clone(), new.clone());
18        sender.send(old_and_new).unwrap();
19    });
20
21    // Port subscriber
22    thread::spawn(move || loop {
23        let port = receiver.recv();
24        if port.is_ok() {
25            println!(
26                "port changed: {:?} -> {:?}",
27                port.unwrap().0,
28                port.unwrap().1
29            );
30        }
31    });
32
33    // Port publisher
34    let mut changing_port = 0u16;
35    loop {
36        changing_port = changing_port + 1;
37        settings.port.set(changing_port);
38        thread::sleep(Duration::from_millis(500));
39    }
40}
Source

pub fn get(&self) -> T

Get value by cloning

Examples found in repository?
examples/readme.rs (line 15)
8fn main() {
9    let mut settings = Settings::default(); // equivalent of `Settings { port: 0, domain: "".to_string() }`
10    settings
11        .port
12        .cb(|old, new| println!("port: {:?} -> {:?}", old, new));
13    settings.port.set(1);
14    settings.port.set(10);
15    let _ = settings.domain.get();
16    let _ = settings.domain.as_ref();
17}
More examples
Hide additional examples
examples/more_complex.rs (line 18)
11fn main() {
12    let mut settings = Settings::default();
13
14    settings.cell.cb(|o, n| println!("{:?} -> {:?}", o, n));
15    settings.vec.cb(|o, n| println!("{:?} -> {:?}", o, n));
16
17    // Field name should be empty by default
18    println!("name should be empty by default: {:?}", settings.str.get());
19
20    // Change Settings struct, user callbacks should be called if they were are assigned
21    settings.str.set("new_name".to_string());
22    settings.cell.set(10.into());
23    settings.cell.set(2222.into());
24
25    // Changing Vec elements is a bit more tedious
26    let mut vec = settings.vec.get();
27    vec.extend([1, 2, 3, 4]);
28    settings.vec.set(vec);
29
30    println!("{:?}", settings);
31}

Trait Implementations§

Source§

impl<T> AsRef<T> for Setting<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Debug for Setting<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Setting<T>
where T: Default + Debug + Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Setting<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for Setting<T>

§

impl<T> !Send for Setting<T>

§

impl<T> !Sync for Setting<T>

§

impl<T> Unpin for Setting<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Setting<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.