3 unstable releases

0.2.0 Oct 27, 2024
0.1.1 Aug 19, 2020
0.1.0 Jan 29, 2020

#719 in Algorithms

Download history 10/week @ 2024-07-22 27/week @ 2024-07-29 23/week @ 2024-08-05 12/week @ 2024-08-12 18/week @ 2024-08-19 29/week @ 2024-08-26 13/week @ 2024-09-02 14/week @ 2024-09-09 18/week @ 2024-09-16 53/week @ 2024-09-23 18/week @ 2024-09-30 18/week @ 2024-10-07 19/week @ 2024-10-14 159/week @ 2024-10-21 74/week @ 2024-10-28 17/week @ 2024-11-04

270 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::{HasGuts, 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 HasGuts 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