Skip to main content

MaybeValidOwned

Enum MaybeValidOwned 

Source
pub enum MaybeValidOwned<V: Validated, P> {
    Valid(V),
    Invalid(P, V::InvalidReason),
}
Expand description

The outcome of consuming a value into a validated form of type V.

Returned by IntoValidated::into_validated. The Valid variant holds the constructed V; the Invalid variant holds the original precursor (returned unchanged, by move) alongside the diagnostic.

§Why both variants matter

MaybeValidOwned is deliberately not a Result. The Invalid variant is not an error to handle and discard: it returns the caller’s input to them, intact, so they can retry, repair, log, or fall through to an alternative. Routing this through Result would frame precursor recovery as error-handling boilerplate; MaybeValidOwned frames it as a first-class outcome.

The precursor is returned by move, not by clone. The trait does not require Self: Clone, and no allocation or duplication occurs on the invalid path beyond what constructing the diagnostic requires.

§Examples

let bytes = vec![0xff, 0xfe];
let validated: MaybeValidOwned<String, Vec<u8>> = bytes.into_validated();
match validated {
    MaybeValidOwned::Valid(s) => println!("got: {}", s),
    MaybeValidOwned::Invalid(bytes, reason) => {
        // `bytes` is the original Vec<u8>, moved back to us.
        assert_eq!(bytes, vec![0xff, 0xfe]);
        eprintln!("invalid at byte {}", reason.valid_up_to());
    }
}

§Construction

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

Variants§

§

Valid(V)

The precursor satisfied V’s predicate.

Holds the constructed validated value.

§

Invalid(P, V::InvalidReason)

The precursor did not satisfy V’s predicate.

Holds the original precursor (returned unchanged, by move) and the diagnostic reason.

Implementations§

Source§

impl<V: Validated, P> MaybeValidOwned<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<V>

Returns the validated value, or None if invalid.

Discards the precursor on the invalid path; use invalid_parts to retain both the precursor and the reason.

Source

pub fn invalid_precursor(self) -> Option<P>

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

Discards the reason; use invalid_parts to retain both.

Source

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

Returns the invalid reason, or None if valid.

Discards the precursor; use invalid_parts to retain both.

Source

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

Returns the precursor 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 for inspecting an owned outcome without consuming it. The returned value borrows V as &V and the precursor as &P, and clones the InvalidReason.

Source

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

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

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

Source

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

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

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.

Auto Trait Implementations§

§

impl<V, P> Freeze for MaybeValidOwned<V, P>
where V: Freeze, P: Freeze, <V as Validated>::InvalidReason: Freeze,

§

impl<V, P> RefUnwindSafe for MaybeValidOwned<V, P>

§

impl<V, P> Send for MaybeValidOwned<V, P>
where V: Send, P: Send, <V as Validated>::InvalidReason: Send,

§

impl<V, P> Sync for MaybeValidOwned<V, P>
where V: Sync, P: Sync, <V as Validated>::InvalidReason: Sync,

§

impl<V, P> Unpin for MaybeValidOwned<V, P>
where V: Unpin, P: Unpin, <V as Validated>::InvalidReason: Unpin,

§

impl<V, P> UnsafeUnpin for MaybeValidOwned<V, P>

§

impl<V, P> UnwindSafe for MaybeValidOwned<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.