Skip to main content

StateOwned

Struct StateOwned 

Source
pub struct StateOwned<T, S> { /* private fields */ }
Expand description

A directly owned runtime implementation T whose compile-time state is S.

Without the tracing feature, the state marker has no runtime storage and StateOwned<T, S> has the same size and alignment as T. With tracing, the wrapper also stores a Vec<TraceEntry> containing the transition history for this value.

StateOwned is the simplest storage representation. Generic implementation methods usually use State<SOwned, T, S> instead, because the same method can then work for owned, boxed, pinned, shared-guard, and discriminated storage. StateOwned<T, S> remains useful when you want the direct transparent wrapper explicitly.

The state marker S is compile-time authority, not runtime data. A transition consumes one state token and returns another:

use magicstatemachines::{StateOwned, transition};
use test_def::states::{Connected, Disconnected};

let disconnected: StateOwned<Connection, Disconnected> =
    StateOwned::new(Connection::new("localhost:8080"));
let connected: StateOwned<Connection, Connected> = transition!(disconnected);

When tracing is enabled, the same transition appends a diagnostic record. The trace is historical only; it is not consulted to decide which methods are callable. The compiler-enforced state remains the S type parameter.

let connected = transition!(disconnected);
let entry = &connected.trace()[0];

assert!(entry.from().type_name().ends_with("::Disconnected"));
assert!(entry.to().type_name().ends_with("::Connected"));
use magicstatemachines::{State, StateOwned, SOwned, transition};
use test_def::states::{Connected, Disconnected};

let disconnected: StateOwned<Connection, Disconnected> =
    StateOwned::new(Connection::new("localhost:8080"));

// Generic APIs usually spell the same owned state like this:
let disconnected: State<SOwned, Connection, Disconnected> =
    State::new(Connection::new("localhost:8080"));
// State-specific methods are implemented on `Connection` with arbitrary
// self types, so they can accept this generic owned state directly.
let connected: State<SOwned, Connection, Connected> =
    connection_method_that_connects(disconnected);

State tokens are linear and shared ownership is not valid state storage:

use std::rc::Rc;
use magicstatemachines::{Initial, StateMachineImpl, StateOwned};

struct Machine;
struct Ready;
struct Runtime;
struct Token;

impl Initial<Ready> for Machine {}
impl StateMachineImpl for Runtime {
    type Standin = Machine;
    type Impl = Self;
    type TransitionToken = Token;
}

let _: StateOwned<Rc<Runtime>, Ready> = StateOwned::new(Rc::new(Runtime));

Implementations§

Source§

impl<T, S> StateOwned<T, S>
where T: StateMachineImpl, T::Standin: Initial<S>,

Source

pub const fn new(value: T) -> Self

Wraps an implementation in a state declared initial by its definition.

This only compiles when T::Standin: Initial<S>. Use transition methods generated in the implementation module to reach every later state.

Trait Implementations§

Source§

impl<T, S> Clone for StateOwned<T, S>
where T: Clone, S: StateClone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, S> Copy for StateOwned<T, S>
where T: Copy, S: StateClone + StateCopy,

Available on non-crate feature tracing only.
Source§

impl<T: Debug, S> Debug for StateOwned<T, S>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, S> Deref for StateOwned<T, S>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, S> DerefMut for StateOwned<T, S>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<T, S> Freeze for StateOwned<T, S>
where T: Freeze,

§

impl<T, S> RefUnwindSafe for StateOwned<T, S>
where T: RefUnwindSafe,

§

impl<T, S> Send for StateOwned<T, S>
where T: Send,

§

impl<T, S> StateClone for StateOwned<T, S>
where T: StateClone,

§

impl<T, S> StateCopy for StateOwned<T, S>
where T: StateCopy,

§

impl<T, S> Sync for StateOwned<T, S>
where T: Sync,

§

impl<T, S> Unpin for StateOwned<T, S>
where T: Unpin,

§

impl<T, S> UnsafeUnpin for StateOwned<T, S>
where T: UnsafeUnpin,

§

impl<T, S> UnwindSafe for StateOwned<T, S>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.