Skip to main content

MaybeValidRef

Enum MaybeValidRef 

Source
pub enum MaybeValidRef<'a, V: Validated + ?Sized, P: ?Sized> {
    Valid(&'a V),
    Invalid(&'a P, V::InvalidReason),
}
Expand description

The outcome of borrowing a value as a validated view of type V.

Returned by AsValidated::as_validated. Both variants carry a reference into the caller’s original value — the Valid variant as &V, the Invalid variant as &P (the precursor). The Invalid variant additionally carries the diagnostic reason.

§Why the precursor is repeated in Invalid

On the invalid path, the caller could reach the precursor through their existing &self binding; including it in the Invalid variant is structurally redundant. It is retained anyway because:

  • The type then honestly describes both outcomes as “a reference into the caller’s value, plus (on the invalid path) a reason.” Readers do not have to infer the precursor’s availability from context.

  • The shape mirrors MaybeValidOwned, where the precursor must be returned structurally because consumption would otherwise lose it. Generic code and documentation can describe both enums in parallel.

  • The cost is a pointer-sized move, not a clone or allocation.

§Why both variants matter

MaybeValidRef is deliberately not a Result. The Invalid variant is not an error to handle and discard: it is a structured peer of Valid, describing the state of data the caller may wish to continue working with (rendering a partial view, producing a repair, emitting a diagnostic that references the caller’s own bytes).

§Examples

let bytes: &[u8] = b"hello";
let validated: MaybeValidRef<'_, str, [u8]> = bytes.as_validated();
match validated {
    MaybeValidRef::Valid(s) => assert_eq!(s, "hello"),
    MaybeValidRef::Invalid(bytes, reason) => {
        eprintln!(
            "invalid at byte {} of {} total",
            reason.valid_up_to(),
            bytes.len(),
        );
    }
}

§Construction

MaybeValidRef values are produced by AsValidated implementations. Direct construction is public and unrestricted: V’s own invariants are enforced by V, not by this enum.

Variants§

§

Valid(&'a V)

The borrowed value satisfies V’s predicate.

§

Invalid(&'a P, V::InvalidReason)

The borrowed value does not satisfy V’s predicate.

Holds a reference to the original precursor (aliasing the caller’s value) and the diagnostic reason.

Implementations§

Source§

impl<'a, V: Validated + ?Sized, P: ?Sized> MaybeValidRef<'a, V, P>

Source

pub fn is_valid(&self) -> bool

Returns true if this is the Valid variant.

Source

pub fn is_invalid(&self) -> bool

Returns true if this is the Invalid variant.

Source

pub fn valid(self) -> Option<&'a V>

Returns the validated reference, or None if invalid.

Source

pub fn invalid_precursor(self) -> Option<&'a P>

Returns the precursor reference on the invalid path, or None if valid.

Source

pub fn invalid_reason(self) -> Option<V::InvalidReason>

Returns the invalid reason, or None if valid.

Discards the precursor reference; use invalid_parts to retain both.

Source

pub fn invalid_parts(self) -> Option<(&'a P, V::InvalidReason)>

Returns the precursor reference and reason on the invalid path, or None if valid.

Source

pub fn as_ref(&self) -> MaybeValidRef<'_, V, P>
where V::InvalidReason: Clone,

Returns a MaybeValidRef that borrows from this one, with the same variant structure.

Useful when a caller holds a MaybeValidRef by value but needs to inspect it without consuming it. The returned value borrows the precursor/validated references from self and clones the InvalidReason on the invalid path.

Source

pub fn into_result(self) -> Result<&'a V, (&'a P, V::InvalidReason)>

Converts into a Result, discarding the peer framing and bundling the precursor reference into the error.

Useful when integrating with code written against Result and ?, at the cost of the explicit-match ergonomics MaybeValidRef encourages.

Source

pub fn into_result_reason_only(self) -> Result<&'a V, V::InvalidReason>

Converts into a Result that carries only the reason on the error path, discarding the precursor reference.

Prefer into_result when the precursor is still useful to the caller; this method is a convenience for call sites that only need the diagnostic.

Source§

impl<'a, V, P> MaybeValidRef<'a, V, P>
where V: Validated + ToOwned + ?Sized, V::Owned: Validated<InvalidReason = V::InvalidReason>, P: ToOwned + ?Sized,

Source

pub fn into_owned(self) -> MaybeValidOwned<V::Owned, P::Owned>

Produces an owned version of this outcome by cloning the borrowed V (or P) into its owned form.

Auto Trait Implementations§

§

impl<'a, V, P> Freeze for MaybeValidRef<'a, V, P>
where <V as Validated>::InvalidReason: Freeze, V: ?Sized, P: ?Sized,

§

impl<'a, V, P> RefUnwindSafe for MaybeValidRef<'a, V, P>

§

impl<'a, V, P> Send for MaybeValidRef<'a, V, P>
where <V as Validated>::InvalidReason: Send, V: Sync + ?Sized, P: Sync + ?Sized,

§

impl<'a, V, P> Sync for MaybeValidRef<'a, V, P>
where <V as Validated>::InvalidReason: Sync, V: Sync + ?Sized, P: Sync + ?Sized,

§

impl<'a, V, P> Unpin for MaybeValidRef<'a, V, P>
where <V as Validated>::InvalidReason: Unpin, V: ?Sized, P: ?Sized,

§

impl<'a, V, P> UnsafeUnpin for MaybeValidRef<'a, V, P>
where <V as Validated>::InvalidReason: UnsafeUnpin, V: ?Sized, P: ?Sized,

§

impl<'a, V, P> UnwindSafe for MaybeValidRef<'a, V, P>

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> 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, 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.