#状态机 #内部 #特质 #构造/解构 #from-into

guts

用于从/到类型的内部结构构造/解构的特质

2个版本

0.1.1 2020年8月19日
0.1.0 2020年1月29日

#2343算法

Download history 26/week @ 2024-03-11 24/week @ 2024-03-18 37/week @ 2024-03-25 69/week @ 2024-04-01 22/week @ 2024-04-08 49/week @ 2024-04-15 49/week @ 2024-04-22 18/week @ 2024-04-29 27/week @ 2024-05-06 24/week @ 2024-05-13 18/week @ 2024-05-20 23/week @ 2024-05-27 24/week @ 2024-06-03 16/week @ 2024-06-10 12/week @ 2024-06-17 23/week @ 2024-06-24

76 每月下载量
7 个crate中使用(通过 signalo_traits

MPL-2.0 许可证

7KB

guts

Downloads Version License

概要

用于从/到类型的内部结构构造/解构的特质。

示例

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) };

许可证

本项目受 MPL-2.0 许可证许可。

无运行时依赖

特性