Show / Hide Table of Contents

Class ResultExtensions

Extension methods for the Result<T, TErr> type.

Inheritance
object
ResultExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
public static class ResultExtensions

Methods

| Edit this page View Source

AndThen<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 Ok value, if any.

Returns
Type Description
Result<T2, TErr>

The result of calling thenFunc if the result is Ok, otherwise the Err value of self.

Type Parameters
Name Description
T1

The Ok type of self.

T2

The Ok type returned by thenFunc.

TErr

The Err type.

| Edit this page View Source

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>

other if self is Ok, otherwise returns the Err value of self.

Type Parameters
Name Description
T1

The Ok type of self.

T2

The Ok type of other.

TErr

The Err type.

| Edit this page View Source

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 Ok type of the result.

TErr

The Err type of the result.

| Edit this page View Source

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 T1Err value to T2Err.

Returns
Type Description
Result<T, T2Err>

A result containing the mapped error, or Ok.

Type Parameters
Name Description
T

The Ok type.

T1Err

The Err type contained by self.

T2Err

The Err type contained by the return value.

Exceptions
Type Condition
ArgumentNullException

Thrown if errMapper is null.

| Edit this page View Source

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 Ok value to T2.

Func<TErr, T2> defaultFactory

The function that converts a contained Err value to T2.

Returns
Type Description
T2

The mapped value.

Type Parameters
Name Description
T1

The Ok type contained by self.

T2

The Ok type contained by the return value.

TErr

The Err type.

Exceptions
Type Condition
ArgumentNullException

Thrown if mapper or defaultFactory is null.

| Edit this page View Source

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 Ok value to T2.

T2 defaultValue

The default value to return if the result is Err.

Returns
Type Description
T2

The mapped value, or the default value.

Type Parameters
Name Description
T1

The Ok type contained by self.

T2

The Ok type contained by the return value.

TErr

The Err type.

Exceptions
Type Condition
ArgumentNullException

Thrown if mapper is null.

| Edit this page View Source

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 Ok value to T2.

Returns
Type Description
Result<T2, TErr>

A result containing the mapped value, or Err.

Type Parameters
Name Description
T1

The Ok type contained by self.

T2

The Ok type contained by the return value.

TErr

The Err type.

Exceptions
Type Condition
ArgumentNullException

Thrown if mapper is null.

| Edit this page View Source

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 Err value, if any.

Returns
Type Description
Result<T, T2Err>

The Ok value of the result, or the result of passing the Err value to elseFunc.

Type Parameters
Name Description
T

The Ok type of the result.

T1Err

The Err type of self.

T2Err

The Err type returned by elseFunc.

| Edit this page View Source

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 Ok value of self, or returns other.

Type Parameters
Name Description
T

The Ok type of the result.

T1Err

The Err type of self.

T2Err

The Err type of other.

| Edit this page View Source

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 Err.

Returns
Type Description
T

The contained Ok value, or the provided default.

Type Parameters
Name Description
T

The Ok type.

TErr

The Err type.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX