Trait FromState

Source
pub trait FromState: StateData + Sized {
    // Required methods
    fn try_borrow_from(state: &State) -> Option<&Self>;
    fn borrow_from(state: &State) -> &Self;
    fn try_borrow_mut_from(state: &mut State) -> Option<&mut Self>;
    fn borrow_mut_from(state: &mut State) -> &mut Self;
    fn try_take_from(state: &mut State) -> Option<Self>;
    fn take_from(state: &mut State) -> Self;
}
Expand description

A trait for accessing data that is stored in State.

This provides the easier T::try_borrow_from(&state) API (for example), as an alternative to state.try_borrow::<T>().

Required Methods§

Source

fn try_borrow_from(state: &State) -> Option<&Self>

Tries to borrow a value from the State storage.

§Examples
#[derive(StateData, Eq, PartialEq, Debug)]
struct MyStruct {
    val: &'static str,
}

state.put(MyStruct {
    val: "This is the value!",
});

match MyStruct::try_borrow_from(&state) {
    Some(&MyStruct { val }) => assert_eq!(val, "This is the value!"),
    _ => panic!("expected `MyStruct` to be present"),
}
Source

fn borrow_from(state: &State) -> &Self

Borrows a value from the State storage.

§Panics

If Self is not present in State.

§Examples
#[derive(StateData, Eq, PartialEq, Debug)]
struct MyStruct {
    val: &'static str,
}

state.put(MyStruct {
    val: "This is the value!",
});

let my_struct = MyStruct::borrow_from(&state);
assert_eq!(my_struct.val, "This is the value!");
Source

fn try_borrow_mut_from(state: &mut State) -> Option<&mut Self>

Tries to mutably borrow a value from the State storage.

§Examples
#[derive(StateData, Eq, PartialEq, Debug)]
struct MyStruct {
    val: &'static str,
}

state.put(MyStruct {
    val: "This is the value!",
});

match MyStruct::try_borrow_mut_from(&mut state) {
    Some(&mut MyStruct { ref mut val }) => *val = "This is the new value!",
    _ => panic!("expected `MyStruct` to be present"),
}
Source

fn borrow_mut_from(state: &mut State) -> &mut Self

Mutably borrows a value from the State storage.

§Panics

If Self is not present in State.

§Examples
#[derive(StateData, Eq, PartialEq, Debug)]
struct MyStruct {
    val: &'static str,
}

state.put(MyStruct {
    val: "This is the value!",
});

let my_struct = MyStruct::borrow_mut_from(&mut state);
my_struct.val = "This is the new value!";
Source

fn try_take_from(state: &mut State) -> Option<Self>

Tries to move a value out of the State storage and return ownership.

§Examples
#[derive(StateData, Eq, PartialEq, Debug)]
struct MyStruct {
    val: &'static str,
}

state.put(MyStruct {
    val: "This is the value!",
});

match MyStruct::try_take_from(&mut state) {
    Some(MyStruct { val }) => assert_eq!(val, "This is the value!"),
    _ => panic!("expected `MyStruct` to be present"),
}
Source

fn take_from(state: &mut State) -> Self

Moves a value out of the State storage and returns ownership.

§Panics

If Self is not present in State.

§Examples
#[derive(StateData, Eq, PartialEq, Debug)]
struct MyStruct {
    val: &'static str,
}

state.put(MyStruct {
    val: "This is the value!",
});

let my_struct = MyStruct::take_from(&mut state);
assert_eq!(my_struct.val, "This is the value!");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> FromState for T
where T: StateData,