Class OptionResultExtensions
Extension methods that transform Option<T> to Result<T, TErr>, or vice-versa.
Inherited Members
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
public static class OptionResultExtensions
Methods
| Edit this page View SourceErr<T, TErr>(Result<T, TErr>)
Converts from the Err
state of Result<T, TErr> to Option<T>.
Declaration
public static Option<TErr> Err<T, TErr>(this Result<T, TErr> self) where T : notnull where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T, TErr> | self | The result to be converted. |
Returns
Type | Description |
---|---|
Option<TErr> |
|
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
TErr | The type of the error. |
OkNumber<T, TErr>(Result<T, TErr>)
Converts from the Ok
state of Result<T, TErr> to NumericOption<T>.
Declaration
public static NumericOption<T> OkNumber<T, TErr>(this Result<T, TErr> self) where T : struct, INumber<T> where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T, TErr> | self | The result to be converted. |
Returns
Type | Description |
---|---|
NumericOption<T> |
|
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
TErr | The type of the error. |
OkOrElse<T, TErr>(NumericOption<T>, Func<TErr>)
Transforms the NumericOption<T> into a Result<T, TErr>,
mapping Some
to Ok
and None
to Err
using the provided
errorFactory
.
Declaration
public static Result<T, TErr> OkOrElse<T, TErr>(this NumericOption<T> self, Func<TErr> errorFactory) where T : struct, INumber<T> where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | self | The option to transform. |
Func<TErr> | errorFactory | A function that creates an error object to be used if the option is |
Returns
Type | Description |
---|---|
Result<T, TErr> | A Result<T, TErr> that contains either the option's value, or the provided error. |
Type Parameters
Name | Description |
---|---|
T | The type of the option's value. |
TErr | The type of the error. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
OkOrElse<T, TErr>(Option<T>, Func<TErr>)
Transforms the Option<T> into a Result<T, TErr>,
mapping Some
to Ok
and None
to Err
using the provided
errorFactory
.
Declaration
public static Result<T, TErr> OkOrElse<T, TErr>(this Option<T> self, Func<TErr> errorFactory) where T : notnull where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
Option<T> | self | The option to transform. |
Func<TErr> | errorFactory | A function that creates an error object to be used if the option is |
Returns
Type | Description |
---|---|
Result<T, TErr> | A Result<T, TErr> that contains either the option's value, or the provided error. |
Type Parameters
Name | Description |
---|---|
T | The type of the option's value. |
TErr | The type of the error. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
OkOr<T, TErr>(NumericOption<T>, TErr)
Transforms the NumericOption<T> into a Result<T, TErr>,
mapping Some
to Ok
and None
to Err
using the provided
error
.
Declaration
public static Result<T, TErr> OkOr<T, TErr>(this NumericOption<T> self, TErr error) where T : struct, INumber<T> where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
NumericOption<T> | self | The option to transform. |
TErr | error | The error to use if the option is |
Returns
Type | Description |
---|---|
Result<T, TErr> | A Result<T, TErr> that contains either the option's value, or the provided error. |
Type Parameters
Name | Description |
---|---|
T | The type of the option's value. |
TErr | The type of the error. |
OkOr<T, TErr>(Option<T>, TErr)
Transforms the Option<T> into a Result<T, TErr>,
mapping Some
to Ok
and None
to Err
using the provided
error
.
Declaration
public static Result<T, TErr> OkOr<T, TErr>(this Option<T> self, TErr error) where T : notnull where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
Option<T> | self | The option to transform. |
TErr | error | The error to use if the option is |
Returns
Type | Description |
---|---|
Result<T, TErr> | A Result<T, TErr> that contains either the option's value, or the provided error. |
Type Parameters
Name | Description |
---|---|
T | The type of the option's value. |
TErr | The type of the error. |
Ok<T, TErr>(Result<T, TErr>)
Converts from the Ok
state of Result<T, TErr> to Option<T>.
Declaration
public static Option<T> Ok<T, TErr>(this Result<T, TErr> self) where T : notnull where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<T, TErr> | self | The result to be converted. |
Returns
Type | Description |
---|---|
Option<T> |
|
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
TErr | The type of the error. |
Transpose<T, TErr>(Option<Result<T, TErr>>)
Transposes an Option
of a Result
into a Result
of an Option
.
None
will be mapped to Ok(None)
.
Some(Ok(_))
and Some(Err(_))
will be mapped to Ok(Some(_))
and Err(_)
.
Declaration
public static Result<Option<T>, TErr> Transpose<T, TErr>(this Option<Result<T, TErr>> self) where T : notnull where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
Option<Result<T, TErr>> | self | An option containing a result. |
Returns
Type | Description |
---|---|
Result<Option<T>, TErr> | An equivalent result containing an option. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
TErr | The type of the error. |
Transpose<T, TErr>(Result<Option<T>, TErr>)
Transposes a Result
of an Option
into an Option
of a Result
.
Ok(None)
will be mapped to None
.
Ok(Some(_))
and Err(_)
will be mapped to Some(Ok(_))
and Some(Err(_))
.
Declaration
public static Option<Result<T, TErr>> Transpose<T, TErr>(this Result<Option<T>, TErr> self) where T : notnull where TErr : notnull
Parameters
Type | Name | Description |
---|---|---|
Result<Option<T>, TErr> | self | A result containing an option. |
Returns
Type | Description |
---|---|
Option<Result<T, TErr>> | An equivalent option containing a result. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
TErr | The type of the error. |