Skip to content

Partial

A result where both a value and an error may be meaningful. Equivalent to Go’s (T, error), where the two are not exclusive.

A common use case is io.Reader.Read, where (n > 0, io.EOF) means that n bytes arrived and the stream ended.

Err arises only where the Go call can return no value at all, such as a nil slice or a nil pointer. io.Reader.Read returns an int, which always carries a value, so it yields Ok or Both and never Err.

enum Partial<T, E> {
Ok(T),
Err(E),
Both(T, E),
}

Returns true if Partial is Ok.

impl<T, E> Partial<T, E> {
fn is_ok(self) -> bool
}
let complete = outcome.is_ok()
true, the read filled without ending

Returns true if Partial is Err.

impl<T, E> Partial<T, E> {
fn is_err(self) -> bool
}
let failed = outcome.is_err()
true only when the call returned no value at all

Returns true if Partial is Both.

impl<T, E> Partial<T, E> {
fn is_both(self) -> bool
}
let stopped_early = outcome.is_both()
true when bytes arrived and the stream ended

Returns Some(value) if Ok or Both, otherwise None.

impl<T, E> Partial<T, E> {
fn ok(self) -> Option<T>
}
let maybe_count = outcome.ok()
Some(5), the byte count

Returns Some(error) if Err or Both, otherwise None.

impl<T, E> Partial<T, E> {
fn err(self) -> Option<E>
}
let maybe_reason = outcome.err()
None when the read carried no error

Returns the wrapped value if Ok or Both, otherwise default.

impl<T, E> Partial<T, E> {
fn unwrap_or(self, default: T) -> T
}
let count = outcome.unwrap_or(0)
5, or 0 when the read failed outright

Returns the wrapped value if Ok or Both, or computes it by calling f with the error.

impl<T, E> Partial<T, E> {
fn unwrap_or_else(self, f: fn(E) -> T) -> T
}
let count = outcome.unwrap_or_else(|failure| failure.Error().length())
runs f only when the read failed outright

Applies f to the wrapped value of an Ok or a Both, leaving Err unchanged.

impl<T, E> Partial<T, E> {
fn map<U>(self, f: fn(T) -> U) -> Partial<U, E>
}
let remaining = outcome.map(|count| buffer.length() - count)
Ok(3), the room left in the buffer

Applies f to the error of an Err or a Both, leaving Ok unchanged.

impl<T, E> Partial<T, E> {
fn map_err<F>(self, f: fn(E) -> F) -> Partial<T, F>
}
let described = outcome.map_err(|failure| failure.Error())
leaves Ok(5) untouched