Skip to content

feature/result-async #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions OnixLabs.Core/OnixLabs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<Title>OnixLabs.Core</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Core API for .NET</Description>
<AssemblyVersion>8.9.0</AssemblyVersion>
<AssemblyVersion>8.10.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.9.0</PackageVersion>
<PackageVersion>8.10.0</PackageVersion>
</PropertyGroup>
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand Down
46 changes: 46 additions & 0 deletions OnixLabs.Core/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace OnixLabs.Core;
Expand Down Expand Up @@ -83,6 +84,29 @@ public static async Task<Result> OfAsync(Func<Task> func)
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </summary>
/// <param name="func">The function to invoke to obtain a successful or failed result.</param>
/// <param name="token">The cancellation token to pass to the invoked function.</param>
/// <returns>
/// Returns a new instance of the <see cref="Result"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </returns>
public static async Task<Result> OfAsync(Func<CancellationToken, Task> func, CancellationToken token = default)
{
try
{
await func(token);
return Success();
}
catch (Exception exception)
{
return exception;
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result"/> class, where the underlying value represents a successful result.
/// </summary>
Expand Down Expand Up @@ -291,6 +315,28 @@ public static async Task<Result<T>> OfAsync(Func<Task<T>> func)
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result{T}"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </summary>
/// <param name="func">The function to invoke to obtain a successful or failed result.</param>
/// <param name="token">The cancellation token to pass to the invoked function.</param>
/// <returns>
/// Returns a new instance of the <see cref="Result{T}"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </returns>
public static async Task<Result<T>> OfAsync(Func<CancellationToken, Task<T>> func, CancellationToken token = default)
{
try
{
return await func(token);
}
catch (Exception exception)
{
return exception;
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result{T}"/> class, where the underlying value represents a successful result.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions OnixLabs.Numerics/OnixLabs.Numerics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<Title>OnixLabs.Numerics</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Numerics API for .NET</Description>
<AssemblyVersion>8.9.0</AssemblyVersion>
<AssemblyVersion>8.10.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.9.0</PackageVersion>
<PackageVersion>8.10.0</PackageVersion>
<LangVersion>12</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<Title>OnixLabs.Security.Cryptography</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Cryptography API for .NET</Description>
<AssemblyVersion>8.9.0</AssemblyVersion>
<AssemblyVersion>8.10.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.9.0</PackageVersion>
<PackageVersion>8.10.0</PackageVersion>
<LangVersion>12</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions OnixLabs.Security/OnixLabs.Security.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<Title>OnixLabs.Security</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Security API for .NET</Description>
<AssemblyVersion>8.9.0</AssemblyVersion>
<AssemblyVersion>8.10.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.9.0</PackageVersion>
<PackageVersion>8.10.0</PackageVersion>
<LangVersion>12</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
Loading