pub struct Valid<T>where
T: Validate,{ /* private fields */ }Expand description
A wrapper of T that validate the state of T every time accessing it.
- It validates the state before accessing it, i.e., if when a invalid state is written to it, it won’t panic until next time accessing it.
- The validation is turned on only when
debug_assertionsis enabled.
An example of defining field a whose value must not exceed 10.
ⓘ
struct Foo { a: u64 }
impl Validate for Foo {
fn validate(&self) -> Result<(), Box<dyn Error>> {
less_equal!(self.a, 10);
Ok(())
}
}
let f = Valid::new(Foo { a: 20 });
let _x = f.a; // panic: panicked at 'invalid state: expect: self.a(20) <= 10(10) ...Validation is triggered when:
DereforDerefMutis called. This is the major use case.Cloneis called.PartialEqorEqis called.PartialOrdorOrdis called.Hashis called.
Validation is not triggered when:
Copy: Because it is just a byte copy.DebugandDisplay: for being able to examine the value for debugging.
Implementations§
Source§impl<T: Validate> Valid<T>
impl<T: Validate> Valid<T>
Sourcepub fn enable_validation(&mut self, enabled: bool)
pub fn enable_validation(&mut self, enabled: bool)
Set whether to validate the state when accessing it.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Get whether to validate the state when accessing it.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume self and return the wrapped value.
This does NOT validate the state.
Trait Implementations§
Source§impl<T> Ord for Valid<T>
impl<T> Ord for Valid<T>
Source§impl<T> PartialOrd for Valid<T>where
T: Validate + PartialOrd,
impl<T> PartialOrd for Valid<T>where
T: Validate + PartialOrd,
impl<T> Copy for Valid<T>
impl<T> Eq for Valid<T>
Auto Trait Implementations§
impl<T> Freeze for Valid<T>where
T: Freeze,
impl<T> RefUnwindSafe for Valid<T>where
T: RefUnwindSafe,
impl<T> Send for Valid<T>where
T: Send,
impl<T> Sync for Valid<T>where
T: Sync,
impl<T> Unpin for Valid<T>where
T: Unpin,
impl<T> UnwindSafe for Valid<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more