Crates.io | guts |
lib.rs | guts |
version | 0.2.0 |
source | src |
created_at | 2020-01-29 22:37:50.631504 |
updated_at | 2024-10-27 18:29:11.959802 |
description | Traits for constructing/destructuring from/into a type's internal guts |
homepage | |
repository | https://github.com/regexident/guts |
max_upload_size | |
id | 203131 |
size | 9,509 |
Traits for constructing/destructuring from/into a type's internal guts.
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) };
This project is licensed under the MPL-2.0.