#state-machine #internal #traits #constructing-destructuring #from-into

guts

Traits for constructing/destructuring from/into a type's internal guts

2 releases

0.1.1 Aug 19, 2020
0.1.0 Jan 29, 2020

#2287 in Algorithms

Download history 7/week @ 2023-12-19 7/week @ 2023-12-26 13/week @ 2024-01-02 58/week @ 2024-01-09 33/week @ 2024-01-16 46/week @ 2024-01-23 36/week @ 2024-01-30 16/week @ 2024-02-06 47/week @ 2024-02-13 31/week @ 2024-02-20 34/week @ 2024-02-27 29/week @ 2024-03-05 30/week @ 2024-03-12 25/week @ 2024-03-19 44/week @ 2024-03-26 56/week @ 2024-04-02

156 downloads per month
Used in 7 crates (via signalo_traits)

MPL-2.0 license

7KB

guts

Downloads Version License

Synopsis

Traits for constructing/destructuring from/into a type's internal guts.

Example

mod state_machine {
    use guts::{Guts, FromGutsUnchecked};

    /// A State machine's internal state.
    pub enum State {
        Off,
        On,
    }

    /// A State machine that hides its internal state.
    pub struct StateMachine {
        state: State,
    }

    impl Default for StateMachine {
        /// Creates a state machine in the only allowed initial state: `Off`.
        fn default() -> Self {
            Self { state: State::Off }
        }
    }

    impl Guts for StateMachine {
        type Guts = State;
    }

    impl FromGutsUnchecked for StateMachine {
        /// Creates a state machine in an arbitrary state, unsafely.
        unsafe fn from_guts_unchecked(guts: Self::Guts) -> Self {
            Self { state: guts }
        }
    }
}

use guts::FromGutsUnchecked;
use state_machine::{State, StateMachine};

// A machine can easily be safely created in its initial state:
let machine = StateMachine::default();

// To create a machine in a non-initial state `unsafe { … }` is required:
let machine = unsafe { StateMachine::from_guts_unchecked(State::On) };

License

This project is licensed under the MPL-2.0.

No runtime deps

Features