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),}Partial.is_ok()
Section titled “Partial.is_ok()”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 endingPartial.is_err()
Section titled “Partial.is_err()”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 allPartial.is_both()
Section titled “Partial.is_both()”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 endedPartial.ok()
Section titled “Partial.ok()”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 countPartial.err()
Section titled “Partial.err()”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 errorPartial.unwrap_or()
Section titled “Partial.unwrap_or()”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 outrightPartial.unwrap_or_else()
Section titled “Partial.unwrap_or_else()”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 outrightPartial.map()
Section titled “Partial.map()”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 bufferPartial.map_err()
Section titled “Partial.map_err()”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