-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor JetStream method results to use NatsResult
- Loading branch information
Showing
2 changed files
with
51 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace NATS.Client.Core; | ||
|
||
public readonly struct NatsResult<T> | ||
{ | ||
private readonly T? _value; | ||
private readonly Exception? _error; | ||
|
||
public NatsResult(T value) | ||
{ | ||
_value = value; | ||
_error = null; | ||
} | ||
|
||
public NatsResult(Exception error) | ||
{ | ||
_value = default; | ||
_error = error; | ||
} | ||
|
||
public T Value => _value ?? ThrowValueIsNotSetException(); | ||
|
||
public Exception Error => _error ?? ThrowErrorIsNotSetException(); | ||
|
||
public bool Success => _error == null; | ||
|
||
public static implicit operator NatsResult<T>(T value) => new(value); | ||
|
||
public static implicit operator NatsResult<T>(Exception error) => new(error); | ||
|
||
private static T ThrowValueIsNotSetException() => throw CreateInvalidOperationException("Result value is not set"); | ||
|
||
private static Exception ThrowErrorIsNotSetException() => throw CreateInvalidOperationException("Result error is not set"); | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static Exception CreateInvalidOperationException(string message) => new InvalidOperationException(message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters