Class ResultExtensions
Extension methods for the Result<T, TErr> type.
Inherited Members
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
public static class ResultExtensions
Methods
| Edit this page View SourceAndThen<T1, T2, TErr>(Result<T1, TErr>, Func<T1, Result<T2, TErr>>)
Calls thenFunc
if the result is Ok
, otherwise returns the Err
value of self
.
Declaration
public static Result<T2, TErr> AndThen<T1, T2, TErr>(this Result<T1, TErr> self, Func<T1, Result<T2, TErr>> thenFunc) where T1 : notnull where T2 : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T1, TErr> | self | The result. |
Func<T1, Result<T2, TErr>> | thenFunc | The function to call with the |
Returns
Type | Description |
---|---|
Result<T2, TErr> | The result of calling |
Type Parameters
Name | Description |
---|---|
T1 | The |
T2 | The |
TErr | The |
And<T1, T2, TErr>(Result<T1, TErr>, Result<T2, TErr>)
Returns other
if self
is Ok
, otherwise
returns the Err
value of self
.
Arguments passed to and are eagerly evaluated; if you are passing the result of a function call, it is recommended to use AndThen<T1, T2, TErr>(Result<T1, TErr>, Func<T1, Result<T2, TErr>>), which is lazily evaluated.
Declaration
public static Result<T2, TErr> And<T1, T2, TErr>(this Result<T1, TErr> self, Result<T2, TErr> other) where T1 : notnull where T2 : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T1, TErr> | self | The result. |
Result<T2, TErr> | other | The other result. |
Returns
Type | Description |
---|---|
Result<T2, TErr> |
|
Type Parameters
Name | Description |
---|---|
T1 | The |
T2 | The |
TErr | The |
Flatten<T, TErr>(Result<Result<T, TErr>, TErr>)
Removes one level of nesting from a nested Result<T, TErr>.
Declaration
public static Result<T, TErr> Flatten<T, TErr>(this Result<Result<T, TErr>, TErr> self) where T : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<Result<T, TErr>, TErr> | self | The result. |
Returns
Type | Description |
---|---|
Result<T, TErr> | The inner result. |
Type Parameters
Name | Description |
---|---|
T | The |
TErr | The |
MapErr<T, T1Err, T2Err>(Result<T, T1Err>, Func<T1Err, T2Err>)
Maps a result by applying a function to a contained Err
value, leaving an Ok
value untouched.
Declaration
public static Result<T, T2Err> MapErr<T, T1Err, T2Err>(this Result<T, T1Err> self, Func<T1Err, T2Err> errMapper) where T : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T, T1Err> | self | The result to map. |
Func<T1Err, T2Err> | errMapper | The function that converts a contained |
Returns
Type | Description |
---|---|
Result<T, T2Err> | A result containing the mapped error, or |
Type Parameters
Name | Description |
---|---|
T | The |
T1Err | The |
T2Err | The |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
MapOrElse<T1, T2, TErr>(Result<T1, TErr>, Func<T1, T2>, Func<TErr, T2>)
Maps a Result
by applying fallback function defaultFactory
to a
contained Err
value, or function mapper
to a contained Ok
value.
This function can be used to unpack a successful result while handling an error.
Declaration
public static T2 MapOrElse<T1, T2, TErr>(this Result<T1, TErr> self, Func<T1, T2> mapper, Func<TErr, T2> defaultFactory) where T1 : notnull where T2 : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T1, TErr> | self | The result to map. |
Func<T1, T2> | mapper | The function that converts a contained |
Func<TErr, T2> | defaultFactory | The function that converts a contained |
Returns
Type | Description |
---|---|
T2 | The mapped value. |
Type Parameters
Name | Description |
---|---|
T1 | The |
T2 | The |
TErr | The |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
MapOr<T1, T2, TErr>(Result<T1, TErr>, Func<T1, T2>, T2)
Returns the provided default (if Err
), or applies a function to the contained value (if Ok
).
Arguments passed to MapOr<T1, T2, TErr>(Result<T1, TErr>, Func<T1, T2>, T2) are eagerly evaluated; if you are passing the result of a function call, it is recommended to use MapOrElse<T1, T2, TErr>(Result<T1, TErr>, Func<T1, T2>, Func<TErr, T2>), which is lazily evaluated.
Declaration
public static T2 MapOr<T1, T2, TErr>(this Result<T1, TErr> self, Func<T1, T2> mapper, T2 defaultValue) where T1 : notnull where T2 : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T1, TErr> | self | The result to map. |
Func<T1, T2> | mapper | The function that converts a contained |
T2 | defaultValue | The default value to return if the result is |
Returns
Type | Description |
---|---|
T2 | The mapped value, or the default value. |
Type Parameters
Name | Description |
---|---|
T1 | The |
T2 | The |
TErr | The |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
Map<T1, T2, TErr>(Result<T1, TErr>, Func<T1, T2>)
Maps a result by applying a function to a contained Ok
value, leaving an Err
value untouched.
Declaration
public static Result<T2, TErr> Map<T1, T2, TErr>(this Result<T1, TErr> self, Func<T1, T2> mapper) where T1 : notnull where T2 : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T1, TErr> | self | The result to map. |
Func<T1, T2> | mapper | The function that converts a contained |
Returns
Type | Description |
---|---|
Result<T2, TErr> | A result containing the mapped value, or |
Type Parameters
Name | Description |
---|---|
T1 | The |
T2 | The |
TErr | The |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
OrElse<T, T1Err, T2Err>(Result<T, T1Err>, Func<T1Err, Result<T, T2Err>>)
Calls elseFunc
if the result is Err
, otherwise returns the Ok
value of self
.
Declaration
public static Result<T, T2Err> OrElse<T, T1Err, T2Err>(this Result<T, T1Err> self, Func<T1Err, Result<T, T2Err>> elseFunc) where T : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T, T1Err> | self | The result. |
Func<T1Err, Result<T, T2Err>> | elseFunc | The function to call with the |
Returns
Type | Description |
---|---|
Result<T, T2Err> | The |
Type Parameters
Name | Description |
---|---|
T | The |
T1Err | The |
T2Err | The |
Or<T, T1Err, T2Err>(Result<T, T1Err>, Result<T, T2Err>)
Returns other
if the result is Err
, otherwise returns the Ok
value of self
.
Arguments passed to Or<T, T1Err, T2Err>(Result<T, T1Err>, Result<T, T2Err>) are eagerly evaluated; if you are passing the result of a function call, it is recommended to use OrElse<T, T1Err, T2Err>(Result<T, T1Err>, Func<T1Err, Result<T, T2Err>>), which is lazily evaluated.
Declaration
public static Result<T, T2Err> Or<T, T1Err, T2Err>(this Result<T, T1Err> self, Result<T, T2Err> other) where T : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T, T1Err> | self | The result. |
Result<T, T2Err> | other | The other result. |
Returns
Type | Description |
---|---|
Result<T, T2Err> | The |
Type Parameters
Name | Description |
---|---|
T | The |
T1Err | The |
T2Err | The |
UnwrapOr<T, TErr>(Result<T, TErr>, T)
Returns the contained Ok
value, or a provided default.
Arguments passed to UnwrapOr<T, TErr>(Result<T, TErr>, T) are eagerly evaluated; if you are passing the result of a function call, it is recommended to use UnwrapOrElse(Func<TErr, T>), which is lazily evaluated.
Declaration
public static T UnwrapOr<T, TErr>(this Result<T, TErr> self, T defaultValue) where T : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T, TErr> | self | The result to unwrap. |
T | defaultValue | The default value to return if the result is |
Returns
Type | Description |
---|---|
T | The contained |
Type Parameters
Name | Description |
---|---|
T | The |
TErr | The |