From 7d3cd7eea72eb0a9af3ca9f188632244ac7c8635 Mon Sep 17 00:00:00 2001 From: Giovanni Costagliola Date: Mon, 9 Dec 2024 16:54:55 +0100 Subject: [PATCH] ToResult: support for async --- src/Monads/Maybe/Maybe.cs | 3 +-- src/Monads/Maybe/MaybeAsyncExtensions.cs | 14 ++++++++++++-- src/Monads/Maybe/MaybeExtensions.cs | 1 - 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Monads/Maybe/Maybe.cs b/src/Monads/Maybe/Maybe.cs index c3fd0ea..5d31dec 100644 --- a/src/Monads/Maybe/Maybe.cs +++ b/src/Monads/Maybe/Maybe.cs @@ -93,8 +93,7 @@ public Maybe(TValue? value) public Maybe Map(Func map) where TNewValue : class => Value is not null ? new Maybe(map(Value)) : Maybe.None; - public Result MapToResult() => - Value is not null ? Result.Success(Value) : Result.Failure(MaybeNoneError.Default); + /// public async Task> Map(Func> map) where TNewValue : class diff --git a/src/Monads/Maybe/MaybeAsyncExtensions.cs b/src/Monads/Maybe/MaybeAsyncExtensions.cs index 96686eb..0280cf5 100644 --- a/src/Monads/Maybe/MaybeAsyncExtensions.cs +++ b/src/Monads/Maybe/MaybeAsyncExtensions.cs @@ -191,7 +191,16 @@ public static Task Satisfy(this Task> maybe, Func maybe.Match(predicate, false); - /// + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task> ToResult(this Task> maybeTask) where TValue : class + { + var maybe = await maybeTask; + return maybe.MapToResult(); + } + + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static async Task> ToResult(this Task> maybeTask, Func errorFunc) where TValue : class { @@ -199,7 +208,8 @@ public static async Task> ToResult(this Task + + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static async Task> ToResult(this Task> maybeTask, Func> errorFunc) where TValue : class { diff --git a/src/Monads/Maybe/MaybeExtensions.cs b/src/Monads/Maybe/MaybeExtensions.cs index 47025f0..3c16a84 100644 --- a/src/Monads/Maybe/MaybeExtensions.cs +++ b/src/Monads/Maybe/MaybeExtensions.cs @@ -228,7 +228,6 @@ Func> value /// /// Convert a to a with a default error in case of None. /// - /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Result MapToResult(this Maybe maybe) where TValue : class