Trait gotham::state::FromState
[−]
[src]
pub trait FromState: StateData + Sized { fn try_borrow_from(_: &State) -> Option<&Self>; fn borrow_from(_: &State) -> &Self; fn try_borrow_mut_from(_: &mut State) -> Option<&mut Self>; fn borrow_mut_from(_: &mut State) -> &mut Self; fn try_take_from(_: &mut State) -> Option<Self>; fn take_from(_: &mut State) -> Self; }
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
fn try_borrow_from(_: &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"), }
ⓘImportant traits for &'a mut W
fn borrow_from(_: &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!");
fn try_borrow_mut_from(_: &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"), }
ⓘImportant traits for &'a mut W
fn borrow_mut_from(_: &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!";
fn try_take_from(_: &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"), }
fn take_from(_: &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!");
Implementors
impl<T> FromState for T where
T: StateData,