intrepid_core/extract/
state.rs1use std::convert::Infallible;
2
3use crate::{Context, Frame, StateNotReadyError};
4
5use super::Extractor;
6
7#[derive(Clone)]
12pub struct State<T: Clone>(pub T);
13
14impl<StateContents> Extractor<StateContents> for State<StateContents>
15where
16 StateContents: Clone + Send + Sync,
17{
18 type Error = StateNotReadyError;
19
20 fn extract(_: Frame, context: &Context<StateContents>) -> Result<Self, Self::Error> {
21 Ok(State(context.state()?))
22 }
23}
24
25impl<State> Extractor<State> for () {
26 type Error = Infallible;
27
28 fn extract(_: Frame, _: &Context<State>) -> Result<Self, Self::Error> {
29 Ok(())
30 }
31}
32
33#[test]
34fn extract_state_from_frame() {
35 let state = 42;
36 let frame = Frame::default();
37 let context = Context::from(state);
38 let extracted = State::extract(frame, &context).unwrap();
39
40 assert_eq!(extracted.0, 42);
41}