pub struct Session { /* private fields */ }Expand description
A session and the phase it is in.
apply takes &self and returns a new value rather than mutating: the
transition table is then the only thing that can produce a state, and a
caller that gets an Err still holds the session it had. The value is four
machine words, so returning one costs less than the clone-and-mutate the
alternative would need at the call site.
Implementations§
Source§impl Session
impl Session
Sourcepub const fn new(id: SessionId, opened_at: Instant) -> Self
pub const fn new(id: SessionId, opened_at: Instant) -> Self
A new session, waiting for a handshake.
Sourcepub const fn stateless(id: SessionId, opened_at: Instant) -> Self
pub const fn stateless(id: SessionId, opened_at: Instant) -> Self
A session that negotiates per message and retains nothing.
Sourcepub const fn state(&self) -> SessionState
pub const fn state(&self) -> SessionState
Where it is in its lifecycle.
Sourcepub const fn transitions(&self) -> u32
pub const fn transitions(&self) -> u32
How many transitions have been applied.
A cheap way for the event log to order two sessions’ histories, and the number a test asserts on to show that a refused event changed nothing.
Sourcepub const fn admits_ordinary_work(&self) -> bool
pub const fn admits_ordinary_work(&self) -> bool
Whether ordinary work is admitted right now.
Sourcepub const fn apply(&self, event: LifecycleEvent) -> Result<Self, LifecycleError>
pub const fn apply(&self, event: LifecycleEvent) -> Result<Self, LifecycleError>
Applies one event.
§Errors
LifecycleError::Terminal from SessionState::Closed, and
LifecycleError::IllegalTransition for any event the table does not
list for the current state.
§Examples
use protocol_droid::domain::session::{LifecycleEvent, Session, SessionId};
let open = Session::new(SessionId::new(1), Default::default());
let starting = open.apply(LifecycleEvent::HandshakeStarted)?;
assert_eq!(starting.state().as_str(), "initialising");
// A second handshake offer is not a thing the specification has, so it
// is refused rather than treated as a restart.
assert!(starting.apply(LifecycleEvent::HandshakeStarted).is_err());