2个版本
0.1.1 | 2020年8月19日 |
---|---|
0.1.0 | 2020年1月29日 |
#2343 在 算法
76 每月下载量
在 7 个crate中使用(通过 signalo_traits)
7KB
guts
概要
用于从/到类型的内部结构构造/解构的特质。
示例
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 许可证许可。