pub struct Repr<T: Debug, I: Fn(&T) -> bool> { /* private fields */ }Expand description
Wraps a value and ensures that an invariant is maintained while allowing that value to be
mutated. The invariant is checked after every mutation.
See crate::CacheableRepr for a version of this struct that supports caching.
Implementations§
Source§impl<T: Debug, I: Fn(&T) -> bool> Repr<T, I>
impl<T: Debug, I: Fn(&T) -> bool> Repr<T, I>
Sourcepub const fn new(inner: T, invariant: I) -> Self
pub const fn new(inner: T, invariant: I) -> Self
Creates a new representation invariant with the given value and invariant function.
use repr_rs::Repr;
struct MinMax { min: i32, max: i32 }
Repr::new(
MinMax { min: 1, max: 5 },
|mm| mm.min < mm.max,
);Sourcepub const fn with_msg(
inner: T,
invariant: I,
violation_message: &'static str,
) -> Self
pub const fn with_msg( inner: T, invariant: I, violation_message: &'static str, ) -> Self
Creates a new representation invariant with the given value, invariant function, and violation message.
use repr_rs::Repr;
struct MinMax { min: i32, max: i32 }
Repr::with_msg(
MinMax { min: 1, max: 5 },
|mm| mm.min < mm.max,
"min must be less than max",
);Sourcepub fn read(&self) -> &T
pub fn read(&self) -> &T
Borrows a read-only view of the value in the representation invariant.
use repr_rs::Repr;
struct MinMax { min: i32, max: i32 }
let repr = Repr::new(MinMax { min: 1, max: 5 }, |mm| mm.min < mm.max);
let view = repr.read();
assert_eq!(1, view.min);
assert_eq!(5, view.max);Sourcepub fn write(&mut self) -> ReprMutator<'_, T, I>
pub fn write(&mut self) -> ReprMutator<'_, T, I>
Borrows a mutable view of the value in the representation invariant.
use repr_rs::Repr;
struct MinMax { min: i32, max: i32 }
let mut repr = Repr::new(MinMax { min: 1, max: 5 }, |mm| mm.min < mm.max);
{
let view = repr.read();
assert_eq!(1, view.min);
assert_eq!(5, view.max);
}
repr.write().min = 4;
let view = repr.read();
assert_eq!(4, view.min);
assert_eq!(5, view.max);Rust’s borrowing rules prevent the read-only view being held while a mutation occurs. For example, this won’t compile:
ⓘ
use repr_rs::Repr;
struct MinMax { min: i32, max: i32 }
let mut repr = Repr::new(MinMax { min: 1, max: 5 }, |mm| mm.min < mm.max);
let view = repr.borrow();
assert_eq!(1, view.min);
assert_eq!(5, view.max);
// error[E0502]: cannot borrow `repr` as mutable because it is also borrowed as immutable
repr.borrow_mut().min = 4;
assert_eq!(4, view.min);
assert_eq!(5, view.max);Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the representation invariant and returns the inner value.
use repr_rs::Repr;
struct MinMax { min: i32, max: i32 }
let repr = Repr::new(MinMax { min: 1, max: 5 }, |mm| mm.min < mm.max);
let inner = repr.into_inner();
assert_eq!(1, inner.min);Trait Implementations§
Source§impl<T: Debug + 'static, I: Fn(&T) -> bool> From<CacheableRepr<T, I>> for Repr<T, I>
impl<T: Debug + 'static, I: Fn(&T) -> bool> From<CacheableRepr<T, I>> for Repr<T, I>
Source§fn from(value: CacheableRepr<T, I>) -> Self
fn from(value: CacheableRepr<T, I>) -> Self
Converts to this type from the input type.
impl<T: Debug + Eq, I: Fn(&T) -> bool> Eq for Repr<T, I>
Auto Trait Implementations§
impl<T, I> !Freeze for Repr<T, I>
impl<T, I> !RefUnwindSafe for Repr<T, I>
impl<T, I> Send for Repr<T, I>
impl<T, I> !Sync for Repr<T, I>
impl<T, I> Unpin for Repr<T, I>
impl<T, I> UnwindSafe for Repr<T, I>where
I: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.