use std::convert::Infallible;
use crate::Frame;
use super::{ExtractContext, Extractor};
#[derive(Clone)]
pub struct State<T: Clone>(pub T);
impl<StateContents> Extractor<StateContents> for State<StateContents>
where
StateContents: Clone + Send + Sync,
{
type Error = Infallible;
fn extract(_: Frame, (_, state): &ExtractContext<StateContents>) -> Result<Self, Self::Error> {
Ok(State(state.clone()))
}
}
impl<State> Extractor<State> for () {
type Error = Infallible;
fn extract(_: Frame, _: &ExtractContext<State>) -> Result<Self, Self::Error> {
Ok(())
}
}
#[test]
fn extract_state_from_frame() {
use crate::ActionContext;
let state = 42;
let frame = Frame::default();
let extracted = State::extract(frame, &(ActionContext::default(), state)).unwrap();
assert_eq!(extracted.0, 42);
}