From f000a24fa11c6e6d2fbf078f20bd8e571efd2d15 Mon Sep 17 00:00:00 2001 From: Meadow Liu Date: Thu, 16 Jan 2025 20:24:02 -0800 Subject: [PATCH] add a few things to builder module --- src/builder.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index f09d519a9..963d9c562 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,7 +1,7 @@ extern crate wiwiwiwiwi; use crate::prelude::*; -use self::private::{ AcceptDefaultSealed, InitialisationStatusSealed }; +use self::private::*; pub use wiwiwiwiwi::builder; @@ -9,9 +9,20 @@ pub struct Uninit { __private: () } +pub trait IsUninit: IsUninitSealed {} + +impl IsUninit for Uninit {} +impl IsUninitSealed for Uninit {} + pub struct Init { __private: () } + +pub trait IsInit: IsInitSealed {} + +impl IsInit for Init {} +impl IsInitSealed for Init {} + /// Compile-time known value for if the current type encodes initialised or not /// /// This can be used for example for a state that has a default variant, where @@ -25,16 +36,21 @@ pub struct Init { /// /// # Safety /// -/// Implementing this trait is a promise that your [`IS_INIT`](InitialisationStatus::IS_INIT) -/// value is actually reflective of initialisation state, assuming the types -/// have been used correctly. +/// Implementing this trait is a promise that your +/// [`IS_INIT`](InitialisationStatus::IS_INIT) +/// value is actually reflective of initialisation state, and +/// [`InvertInitialisationStatus`](InitialisationStatus::InvertInitialisationStatus) +/// is too, assuming the types have been used correctly. pub unsafe trait InitialisationStatus: InitialisationStatusSealed { const IS_INIT: bool; + const IS_UNINIT: bool = !Self::IS_INIT; + type InvertInitialisationStatus: InitialisationStatus; } // SAFETY: `Uninit` represents being uninitialised unsafe impl InitialisationStatus for Uninit { const IS_INIT: bool = false; + type InvertInitialisationStatus = Init; } impl InitialisationStatusSealed for Uninit {} @@ -42,6 +58,7 @@ impl InitialisationStatusSealed for Uninit {} // SAFETY: `Init` represents being initialised unsafe impl InitialisationStatus for Init { const IS_INIT: bool = true; + type InvertInitialisationStatus = Uninit; } impl InitialisationStatusSealed for Init {} @@ -76,10 +93,16 @@ where impl AcceptDefaultSealed for Option {} +pub type PhantomDataInvariant = PhantomData T>; + /// notouchie mod private { /// notouchie - pub trait AcceptDefaultSealed {} + pub trait IsUninitSealed {} + /// notouchie + pub trait IsInitSealed {} /// notouchie pub trait InitialisationStatusSealed {} + /// notouchie + pub trait AcceptDefaultSealed {} }