Skip to content

Commit

Permalink
nullable tooptionasync
Browse files Browse the repository at this point in the history
  • Loading branch information
pimbrouwers committed Oct 10, 2024
1 parent 325178b commit 1c0a6b2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Danom/Option/OptionTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,40 @@ public static Task<Option<T>> OrElseWithAsync<T>(
Func<Option<T>> ifNoneWith,
CancellationToken? cancellationToken = null) =>
optionTask.MatchAsync(Option<T>.Some, ifNoneWith, cancellationToken);

/// <summary>
/// Converts a nullable value to an option.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="x"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Option<T>> ToOptionAsync<T>(
this Task<T?> x,
CancellationToken? cancellationToken = null) =>
(await x.WaitOrCancel(cancellationToken)).ToOption();

/// <summary>
/// Converts a nullable strct to an option.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="x"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Option<T>> ToOptionAsync<T>(
this Task<T?> x,
CancellationToken? cancellationToken = null) where T : struct =>
(await x.WaitOrCancel(cancellationToken)).ToOption();

/// <summary>
/// Converts a nullable string to an option.
/// </summary>
/// <param name="x"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Option<string>> ToOptionAsync(
this Task<string?> x,
CancellationToken? cancellationToken = null) =>
(await x.WaitOrCancel(cancellationToken)).ToOption();

}

0 comments on commit 1c0a6b2

Please sign in to comment.