Struct MaybeValidated

Source
pub struct MaybeValidated<T> { /* private fields */ }
Expand description

A data structure for tagging data as already being validated to prevent redundant work.

§Example

struct Foo;
struct Error;

/// Performs expensive validation of `foo`.
fn validate_foo(foo: &Foo) -> Result<bool, Error>;

fn do_stuff(foo: Foo) {
    let foo = MaybeValidated::from(foo);
    do_stuff_with_foo(&foo);
    if foo.validate_with(validate_foo) {
        println!("^_^");
    }
}

fn do_stuff_with_foo(foo: &MaybeValidated<Foo) {
    // …
    if maybe_do_something && foo.validate_with(validate_foo) {
        println!("@_@");
    }
    // …
}

Implementations§

Source§

impl<T> MaybeValidated<T>

Source

pub fn from_validated(payload: T) -> Self

Creates new MaybeValidated object marking payload as validated. No verification is performed; it’s caller’s responsibility to make sure the payload has indeed been validated.

§Example
use unc_primitives::utils::MaybeValidated;

let value = MaybeValidated::from_validated(42);
assert!(value.is_validated());
assert_eq!(Ok(true), value.validate_with::<(), _>(|_| panic!()));
Source

pub fn validate_with<E, F: FnOnce(&T) -> Result<bool, E>>( &self, validator: F, ) -> Result<bool, E>

Validates payload with given validator function and returns result of the validation. If payload has already been validated returns Ok(true). Note that this method changes the internal validated flag so it’s probably incorrect to call it with different validator functions.

§Example
use unc_primitives::utils::MaybeValidated;

let value = MaybeValidated::from(42);
assert_eq!(Err(()), value.validate_with(|_| Err(())));
assert_eq!(Ok(false), value.validate_with::<(), _>(|v| Ok(*v == 24)));
assert!(!value.is_validated());
assert_eq!(Ok(true), value.validate_with::<(), _>(|v| Ok(*v == 42)));
assert!(value.is_validated());
assert_eq!(Ok(true), value.validate_with::<(), _>(|_| panic!()));
Source

pub fn mark_as_valid(&self)

Marks the payload as valid. No verification is performed; it’s caller’s responsibility to make sure the payload has indeed been validated.

Source

pub fn map<U, F: FnOnce(T) -> U>(self, validator: F) -> MaybeValidated<U>

Applies function to the payload (whether it’s been validated or not) and returns new object with result of the function as payload. Validated state is not changed.

§Example
use unc_primitives::utils::MaybeValidated;

let value = MaybeValidated::from(42);
assert_eq!("42", value.map(|v| v.to_string()).into_inner());
Source

pub fn as_ref(&self) -> MaybeValidated<&T>

Returns a new object storing reference to this object’s payload. Note that the two objects do not share the validated state so calling validate_with on one of them does not affect the other.

§Example
use unc_primitives::utils::MaybeValidated;

let value = MaybeValidated::from(42);
let value_as_ref = value.as_ref();
assert_eq!(Ok(true), value_as_ref.validate_with::<(), _>(|&&v| Ok(v == 42)));
assert!(value_as_ref.is_validated());
assert!(!value.is_validated());
Source

pub fn is_validated(&self) -> bool

Returns whether the payload has been validated.

Source

pub fn into_inner(self) -> T

Extracts the payload whether or not it’s been validated.

Source

pub fn get_inner(&self) -> &T

Returns a reference to the payload

Trait Implementations§

Source§

impl<T: Clone> Clone for MaybeValidated<T>

Source§

fn clone(&self) -> MaybeValidated<T>

Returns a duplicate 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: Sized> Deref for MaybeValidated<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> From<T> for MaybeValidated<T>

Source§

fn from(payload: T) -> Self

Creates new MaybeValidated object marking payload as not validated.

Auto Trait Implementations§

§

impl<T> !Freeze for MaybeValidated<T>

§

impl<T> !RefUnwindSafe for MaybeValidated<T>

§

impl<T> Send for MaybeValidated<T>
where T: Send,

§

impl<T> !Sync for MaybeValidated<T>

§

impl<T> Unpin for MaybeValidated<T>
where T: Unpin,

§

impl<T> UnwindSafe for MaybeValidated<T>
where 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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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, dest: *mut u8)

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

impl<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

Source§

fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromFd for T
where T: From<OwnedFd>,

Source§

fn from_fd(owned_fd: OwnedFd) -> T

👎Deprecated since 1.0.0: FromFd::from_fd is replaced by From<OwnedFd>::from
Constructs a new instance of Self from the given file descriptor. Read more
Source§

fn from_into_fd<Owned>(into_owned: Owned) -> Self
where Owned: Into<OwnedFd>, Self: Sized + From<OwnedFd>,

Constructs a new instance of Self from the given file descriptor converted from into_owned. Read more
Source§

impl<T> FromFilelike for T
where T: From<OwnedFd>,

Source§

fn from_filelike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given filelike object. Read more
Source§

fn from_into_filelike<Owned>(owned: Owned) -> T
where Owned: IntoFilelike,

Constructs a new instance of Self from the given filelike object converted from into_owned. Read more
Source§

impl<T> FromSocketlike for T
where T: From<OwnedFd>,

Source§

fn from_socketlike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given socketlike object.
Source§

fn from_into_socketlike<Owned>(owned: Owned) -> T
where Owned: IntoSocketlike,

Constructs a new instance of Self from the given socketlike object converted from into_owned.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The type for metadata in pointers and references to Self.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,