repr_rs

Struct Repr

Source
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>

Source

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,
);
Source

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",
);
Source

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

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

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 + Clone, I: Fn(&T) -> bool + Clone> Clone for Repr<T, I>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, I: Fn(&T) -> bool> Debug for Repr<T, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Debug + Display, I: Fn(&T) -> bool> Display for Repr<T, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Debug + 'static, I: Fn(&T) -> bool> From<CacheableRepr<T, I>> for Repr<T, I>

Source§

fn from(value: CacheableRepr<T, I>) -> Self

Converts to this type from the input type.
Source§

impl<T: Debug + 'static, I: Fn(&T) -> bool> From<Repr<T, I>> for CacheableRepr<T, I>

Source§

fn from(value: Repr<T, I>) -> Self

Converts to this type from the input type.
Source§

impl<T: Debug + Hash, I: Fn(&T) -> bool> Hash for Repr<T, I>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Debug + PartialEq, I: Fn(&T) -> bool> PartialEq for Repr<T, I>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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>
where I: Send, T: Send,

§

impl<T, I> !Sync for Repr<T, I>

§

impl<T, I> Unpin for Repr<T, I>
where I: Unpin, T: Unpin,

§

impl<T, I> UnwindSafe for Repr<T, I>
where I: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

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>

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)

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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.