Wrapper

Trait Wrapper 

Source
pub trait Wrapper: Sized {
    type Inner;
    type Error;

    const NAME: &'static str;
    const PROCESS: fn(&mut Self::Inner) -> Result<(), Self::Error>;

    // Required methods
    fn new(
        value: impl Into<Self::Inner>,
    ) -> Result<Self, ConstructionError<Self>>;
    fn get(&self) -> &Self::Inner;
    fn set(
        &mut self,
        value: impl Into<Self::Inner>,
    ) -> Result<(), ConstructionError<Self>>;
    fn into_inner(self) -> Self::Inner;
    fn new_unprocessed(value: impl Into<Self::Inner>) -> Self;
    fn set_unprocessed(&mut self, value: impl Into<Self::Inner>);
    fn mutate_unprocessed(&mut self, f: impl FnOnce(&mut Self::Inner));
    fn verify(self) -> Result<Self, VerificationError<Self>>;

    // Provided method
    fn mutate(
        &mut self,
        f: impl FnOnce(&mut Self::Inner),
    ) -> Result<(), MutationError<Self>>
       where Self::Inner: Clone { ... }
}
Expand description

A trait that describes a Newtype wrapper struct generated by define! and extend! macros.

Required Associated Constants§

Source

const NAME: &'static str

Name of the wrapper. Will be used for nice error messages.

Source

const PROCESS: fn(&mut Self::Inner) -> Result<(), Self::Error>

The function that will mutate and validate wrapper’s inner value on every construction and mutation.

It’s behaviour is based on the closures that were provided during the invocation of define!/extend! macros (e.g. adjust, ensure and validate).

If no closures were provided, this function will not mutate the value and always return Ok(()).

Required Associated Types§

Source

type Inner

The inner type of the wrapper.

Source

type Error

The type of an error that can occur during the construction or mutation of the wrapper’s value.

Required Methods§

Source

fn new(value: impl Into<Self::Inner>) -> Result<Self, ConstructionError<Self>>

Construct a new wrapper.

It will return an error if the provided value doesn’t pass Self::PROCESS.

Source

fn get(&self) -> &Self::Inner

Get a shared reference to the inner value.

Source

fn set( &mut self, value: impl Into<Self::Inner>, ) -> Result<(), ConstructionError<Self>>

Replace inner value with the provided one.

It will return an error if the provided value doesn’t pass Self::PROCESS.

Source

fn into_inner(self) -> Self::Inner

Unwrap the value into the inner type.

Source

fn new_unprocessed(value: impl Into<Self::Inner>) -> Self

Construct a new wrapper without calling Self::PROCESS.

Source

fn set_unprocessed(&mut self, value: impl Into<Self::Inner>)

Replace inner value with the provided one without calling Self::PROCESS.

Source

fn mutate_unprocessed(&mut self, f: impl FnOnce(&mut Self::Inner))

Mutate inner value using provided closure without calling Self::PROCESS.

Source

fn verify(self) -> Result<Self, VerificationError<Self>>

Verify that inner value still passes Self::PROCESS.

Provided Methods§

Source

fn mutate( &mut self, f: impl FnOnce(&mut Self::Inner), ) -> Result<(), MutationError<Self>>
where Self::Inner: Clone,

Mutate inner value using provided closure.

To make sure that the closure doesn’t corrupt the inner value, this method is only available when the inner type implements Clone. This way, the closure receives a copy of the inner value, and then, if the mutated value passes Self::PROCESS, it will replace the inner value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§