Skip to content

Commit

Permalink
Merge pull request #583 from marcinjahn/allocation-free-result-maperror
Browse files Browse the repository at this point in the history
feat: add allocation-free overloads for Result.MapError
  • Loading branch information
vkhorikov authored Dec 23, 2024
2 parents 1b13ecc + 394f925 commit b6ca7d1
Show file tree
Hide file tree
Showing 14 changed files with 3,379 additions and 106 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ public static partial class ResultExtensions
/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<Result> MapError(this Task<Result> resultTask, Func<string, string> errorFactory)
public static async Task<Result> MapError(
this Task<Result> resultTask,
Func<string, string> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -20,10 +23,29 @@ public static async Task<Result> MapError(this Task<Result> resultTask, Func<str
return Result.Failure(error);
}

public static async Task<Result> MapError<TContext>(
this Task<Result> resultTask,
Func<string, TContext, string> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return Result.Success();
}

var error = errorFactory(result.Error, context);
return Result.Failure(error);
}

/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<UnitResult<E>> MapError<E>(this Task<Result> resultTask, Func<string, E> errorFactory)
public static async Task<UnitResult<E>> MapError<E>(
this Task<Result> resultTask,
Func<string, E> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -35,10 +57,29 @@ public static async Task<UnitResult<E>> MapError<E>(this Task<Result> resultTask
return UnitResult.Failure(error);
}

public static async Task<UnitResult<E>> MapError<E, TContext>(
this Task<Result> resultTask,
Func<string, TContext, E> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return UnitResult.Success<E>();
}

var error = errorFactory(result.Error, context);
return UnitResult.Failure(error);
}

/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<Result<T>> MapError<T>(this Task<Result<T>> resultTask, Func<string, string> errorFactory)
public static async Task<Result<T>> MapError<T>(
this Task<Result<T>> resultTask,
Func<string, string> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -50,10 +91,29 @@ public static async Task<Result<T>> MapError<T>(this Task<Result<T>> resultTask,
return Result.Failure<T>(error);
}

public static async Task<Result<T>> MapError<T, TContext>(
this Task<Result<T>> resultTask,
Func<string, TContext, string> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return Result.Success(result.Value);
}

var error = errorFactory(result.Error, context);
return Result.Failure<T>(error);
}

/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<Result<T, E>> MapError<T, E>(this Task<Result<T>> resultTask, Func<string, E> errorFactory)
public static async Task<Result<T, E>> MapError<T, E>(
this Task<Result<T>> resultTask,
Func<string, E> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -65,10 +125,29 @@ public static async Task<Result<T, E>> MapError<T, E>(this Task<Result<T>> resul
return Result.Failure<T, E>(error);
}

public static async Task<Result<T, E>> MapError<T, E, TContext>(
this Task<Result<T>> resultTask,
Func<string, TContext, E> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return Result.Success<T, E>(result.Value);
}

var error = errorFactory(result.Error, context);
return Result.Failure<T, E>(error);
}

/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<Result> MapError<E>(this Task<UnitResult<E>> resultTask, Func<E, string> errorFactory)
public static async Task<Result> MapError<E>(
this Task<UnitResult<E>> resultTask,
Func<E, string> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -80,10 +159,29 @@ public static async Task<Result> MapError<E>(this Task<UnitResult<E>> resultTask
return Result.Failure(error);
}

public static async Task<Result> MapError<E, TContext>(
this Task<UnitResult<E>> resultTask,
Func<E, TContext, string> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return Result.Success();
}

var error = errorFactory(result.Error, context);
return Result.Failure(error);
}

/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<UnitResult<E2>> MapError<E, E2>(this Task<UnitResult<E>> resultTask, Func<E, E2> errorFactory)
public static async Task<UnitResult<E2>> MapError<E, E2>(
this Task<UnitResult<E>> resultTask,
Func<E, E2> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -95,10 +193,29 @@ public static async Task<UnitResult<E2>> MapError<E, E2>(this Task<UnitResult<E>
return UnitResult.Failure(error);
}

public static async Task<UnitResult<E2>> MapError<E, E2, TContext>(
this Task<UnitResult<E>> resultTask,
Func<E, TContext, E2> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return UnitResult.Success<E2>();
}

var error = errorFactory(result.Error, context);
return UnitResult.Failure(error);
}

/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<Result<T>> MapError<T, E>(this Task<Result<T, E>> resultTask, Func<E, string> errorFactory)
public static async Task<Result<T>> MapError<T, E>(
this Task<Result<T, E>> resultTask,
Func<E, string> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -110,10 +227,29 @@ public static async Task<Result<T>> MapError<T, E>(this Task<Result<T, E>> resul
return Result.Failure<T>(error);
}

public static async Task<Result<T>> MapError<T, E, TContext>(
this Task<Result<T, E>> resultTask,
Func<E, TContext, string> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return Result.Success(result.Value);
}

var error = errorFactory(result.Error, context);
return Result.Failure<T>(error);
}

/// <summary>
/// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
/// </summary>
public static async Task<Result<T, E2>> MapError<T, E, E2>(this Task<Result<T, E>> resultTask, Func<E, E2> errorFactory)
public static async Task<Result<T, E2>> MapError<T, E, E2>(
this Task<Result<T, E>> resultTask,
Func<E, E2> errorFactory
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
Expand All @@ -124,5 +260,21 @@ public static async Task<Result<T, E2>> MapError<T, E, E2>(this Task<Result<T, E
var error = errorFactory(result.Error);
return Result.Failure<T, E2>(error);
}

public static async Task<Result<T, E2>> MapError<T, E, E2, TContext>(
this Task<Result<T, E>> resultTask,
Func<E, TContext, E2> errorFactory,
TContext context
)
{
var result = await resultTask.DefaultAwait();
if (result.IsSuccess)
{
return Result.Success<T, E2>(result.Value);
}

var error = errorFactory(result.Error, context);
return Result.Failure<T, E2>(error);
}
}
}
}
Loading

0 comments on commit b6ca7d1

Please sign in to comment.