Struct unc_primitives::utils::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>
impl<T> MaybeValidated<T>
sourcepub fn from_validated(payload: T) -> Self
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!()));sourcepub fn validate_with<E, F: FnOnce(&T) -> Result<bool, E>>(
&self,
validator: F
) -> Result<bool, E>
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!()));sourcepub fn mark_as_valid(&self)
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.
sourcepub fn map<U, F: FnOnce(T) -> U>(self, validator: F) -> MaybeValidated<U>
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());sourcepub fn as_ref(&self) -> MaybeValidated<&T>
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());sourcepub fn is_validated(&self) -> bool
pub fn is_validated(&self) -> bool
Returns whether the payload has been validated.
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Extracts the payload whether or not it’s been validated.
Trait Implementations§
source§impl<T: Clone> Clone for MaybeValidated<T>
impl<T: Clone> Clone for MaybeValidated<T>
source§fn clone(&self) -> MaybeValidated<T>
fn clone(&self) -> MaybeValidated<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<T: Sized> Deref for MaybeValidated<T>
impl<T: Sized> Deref for MaybeValidated<T>
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> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata
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
source§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
source§impl<T> FromFd for T
impl<T> FromFd for T
source§impl<T> FromFilelike for T
impl<T> FromFilelike for T
source§fn from_filelike(owned: OwnedFd) -> T
fn from_filelike(owned: OwnedFd) -> T
Self from the given filelike object. Read moresource§fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
Self from the given filelike object
converted from into_owned. Read moresource§impl<T> FromSocketlike for T
impl<T> FromSocketlike for T
source§fn from_socketlike(owned: OwnedFd) -> T
fn from_socketlike(owned: OwnedFd) -> T
Self from the given socketlike object.source§fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
Self from the given socketlike object
converted from into_owned.