Skip to main content

Outcome

Enum Outcome 

Source
pub enum Outcome<T, E> {
    Ok(T),
    Err(E),
    Cancelled(CancelReason),
    Panicked(PanicPayload),
}
Expand description

The four-valued outcome of a concurrent operation.

Forms a severity lattice where worse outcomes dominate: Ok < Err < Cancelled < Panicked

Variants§

§

Ok(T)

Success with a value.

§

Err(E)

Application-level error.

§

Cancelled(CancelReason)

The operation was cancelled.

§

Panicked(PanicPayload)

The operation panicked.

Implementations§

Source§

impl<T, E> Outcome<T, E>

Source

pub const fn ok(value: T) -> Outcome<T, E>

Creates a successful outcome with the given value.

§Examples
use asupersync::Outcome;

let outcome: Outcome<i32, &str> = Outcome::ok(42);
assert!(outcome.is_ok());
assert_eq!(outcome.unwrap(), 42);
Source

pub const fn err(error: E) -> Outcome<T, E>

Creates an error outcome with the given error.

§Examples
use asupersync::Outcome;

let outcome: Outcome<i32, &str> = Outcome::err("not found");
assert!(outcome.is_err());
Source

pub const fn cancelled(reason: CancelReason) -> Outcome<T, E>

Creates a cancelled outcome with the given reason.

§Examples
use asupersync::{Outcome, CancelReason};

let outcome: Outcome<i32, &str> = Outcome::cancelled(CancelReason::timeout());
assert!(outcome.is_cancelled());
Source

pub const fn panicked(payload: PanicPayload) -> Outcome<T, E>

Creates a panicked outcome with the given payload.

§Examples
use asupersync::{Outcome, PanicPayload};

let outcome: Outcome<i32, &str> = Outcome::panicked(PanicPayload::new("oops"));
assert!(outcome.is_panicked());
Source

pub const fn severity(&self) -> Severity

Returns the severity level of this outcome.

The severity levels are ordered: Ok < Err < Cancelled < Panicked. This is useful for aggregation where the worst outcome should win.

§Examples
use asupersync::{Outcome, Severity, CancelReason};

let ok: Outcome<i32, &str> = Outcome::ok(42);
let err: Outcome<i32, &str> = Outcome::err("oops");
let cancelled: Outcome<i32, &str> = Outcome::cancelled(CancelReason::timeout());

assert_eq!(ok.severity(), Severity::Ok);
assert_eq!(err.severity(), Severity::Err);
assert_eq!(cancelled.severity(), Severity::Cancelled);
assert!(ok.severity() < err.severity());
Source

pub const fn severity_u8(&self) -> u8

Returns the numeric severity level (0 = Ok, 3 = Panicked).

Prefer severity() for type-safe comparisons.

Source

pub const fn is_terminal(&self) -> bool

Returns true if this is a terminal outcome (any non-pending state).

All Outcome variants are terminal states.

Source

pub const fn is_ok(&self) -> bool

Returns true if this outcome is Ok.

§Examples
use asupersync::Outcome;

let ok: Outcome<i32, &str> = Outcome::ok(42);
let err: Outcome<i32, &str> = Outcome::err("oops");

assert!(ok.is_ok());
assert!(!err.is_ok());
Source

pub const fn is_err(&self) -> bool

Returns true if this outcome is Err.

§Examples
use asupersync::Outcome;

let err: Outcome<i32, &str> = Outcome::err("oops");
assert!(err.is_err());
Source

pub const fn is_cancelled(&self) -> bool

Returns true if this outcome is Cancelled.

§Examples
use asupersync::{Outcome, CancelReason};

let cancelled: Outcome<i32, &str> = Outcome::cancelled(CancelReason::timeout());
assert!(cancelled.is_cancelled());
Source

pub const fn is_panicked(&self) -> bool

Returns true if this outcome is Panicked.

§Examples
use asupersync::{Outcome, PanicPayload};

let panicked: Outcome<i32, &str> = Outcome::panicked(PanicPayload::new("oops"));
assert!(panicked.is_panicked());
Source

pub fn into_result(self) -> Result<T, OutcomeError<E>>

Converts this outcome to a standard Result, with cancellation and panic as errors.

This is useful when interfacing with code that expects Result.

Source

pub fn map<U, F>(self, f: F) -> Outcome<U, E>
where F: FnOnce(T) -> U,

Maps the success value using the provided function.

Source

pub fn map_err<F2, G>(self, g: G) -> Outcome<T, F2>
where G: FnOnce(E) -> F2,

Maps the error value using the provided function.

§Examples
use asupersync::Outcome;

let err: Outcome<i32, &str> = Outcome::err("short");
let mapped = err.map_err(str::len);
assert!(matches!(mapped, Outcome::Err(5)));
Source

pub fn and_then<U, F>(self, f: F) -> Outcome<U, E>
where F: FnOnce(T) -> Outcome<U, E>,

Applies a function to the success value, flattening the result.

This is the monadic bind operation, useful for chaining operations that might fail.

§Examples
use asupersync::Outcome;

fn parse_int(s: &str) -> Outcome<i32, &'static str> {
    s.parse::<i32>().map_err(|_| "parse error").into()
}

fn double(x: i32) -> Outcome<i32, &'static str> {
    Outcome::ok(x * 2)
}

let result = parse_int("21").and_then(double);
assert_eq!(result.unwrap(), 42);

let result = parse_int("abc").and_then(double);
assert!(result.is_err());
Source

pub fn ok_or_else<F2, G>(self, f: G) -> Result<T, F2>
where G: FnOnce() -> F2,

Returns the success value, or computes a fallback from a closure.

Unlike unwrap_or_else, this returns a Result that preserves the distinction between different non-Ok outcomes.

§Examples
use asupersync::{Outcome, CancelReason};

let ok: Outcome<i32, &str> = Outcome::ok(42);
let result: Result<i32, &str> = ok.ok_or_else(|| "default error");
assert_eq!(result, Ok(42));

let cancelled: Outcome<i32, &str> = Outcome::cancelled(CancelReason::timeout());
let result: Result<i32, &str> = cancelled.ok_or_else(|| "was cancelled");
assert_eq!(result, Err("was cancelled"));
Source

pub fn join(self, other: Outcome<T, E>) -> Outcome<T, E>

Joins this outcome with another, returning the outcome with higher severity.

This implements the lattice join operation for aggregating outcomes from parallel tasks. The outcome with the worst (highest) severity wins.

§Note on Value Handling

When both outcomes are Ok, this method returns self. When both are the same non-Ok severity, the first (self) is returned.

§Examples
use asupersync::{Outcome, CancelReason};

let ok1: Outcome<i32, &str> = Outcome::ok(1);
let ok2: Outcome<i32, &str> = Outcome::ok(2);
let err: Outcome<i32, &str> = Outcome::err("error");
let cancelled: Outcome<i32, &str> = Outcome::cancelled(CancelReason::timeout());

// Ok + Ok = first Ok (both same severity)
assert!(ok1.clone().join(ok2).is_ok());

// Ok + Err = Err (Err is worse)
assert!(ok1.clone().join(err.clone()).is_err());

// Err + Cancelled = Cancelled (Cancelled is worse)
assert!(err.join(cancelled).is_cancelled());
Source

pub fn unwrap(self) -> T
where E: Debug,

Returns the success value or panics.

§Panics

Panics if the outcome is not Ok.

Source

pub fn unwrap_or(self, default: T) -> T

Returns the success value or a default.

Source

pub fn unwrap_or_else<F>(self, f: F) -> T
where F: FnOnce() -> T,

Returns the success value or computes it from a closure.

Trait Implementations§

Source§

impl<T, E> Clone for Outcome<T, E>
where T: Clone, E: Clone,

Source§

fn clone(&self) -> Outcome<T, E>

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, E> Debug for Outcome<T, E>
where T: Debug, E: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T, E> From<Result<T, E>> for Outcome<T, E>

Source§

fn from(result: Result<T, E>) -> Outcome<T, E>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, E> Freeze for Outcome<T, E>
where T: Freeze, E: Freeze,

§

impl<T, E> RefUnwindSafe for Outcome<T, E>

§

impl<T, E> Send for Outcome<T, E>
where T: Send, E: Send,

§

impl<T, E> Sync for Outcome<T, E>
where T: Sync, E: Sync,

§

impl<T, E> Unpin for Outcome<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnwindSafe for Outcome<T, E>
where T: UnwindSafe, E: 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> 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> 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<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