pub trait FromState: StateData + Sized {
    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

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"),
}

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!");

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"),
}

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!";

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"),
}

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!");

Implementors