1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-07-04 23:36:00 +02:00

Added '->' support to nullables and Result<T>

This commit is contained in:
Brian Fiete 2022-06-22 12:06:40 -07:00
parent e4cac2ca24
commit 36a8c2c6ae
5 changed files with 93 additions and 8 deletions

View file

@ -35,6 +35,15 @@ namespace System
}
}
public T ValueOrDefault
{
[Inline]
get
{
return mValue;
}
}
public ref T ValueRef
{
[Inline]
@ -96,9 +105,19 @@ namespace System
[Inline]
public static explicit operator T(Nullable<T> value)
{
if (!value.mHasValue)
Debug.FatalError("Value requested for null nullable.");
return value.mValue;
}
[Inline]
public static ref T operator->(ref Nullable<T> value)
{
if (!value.mHasValue)
Debug.FatalError("Value requested for null nullable.");
return ref value.mValue;
}
[Inline, Commutable]
public static bool operator==(Nullable<T> lhs, T rhs)
{

View file

@ -27,6 +27,22 @@ namespace System
}
}
public ref T ValueRef
{
[Inline]
get mut
{
switch (this)
{
case .Ok(var ref val): return ref val;
case .Err:
{
Internal.FatalError("Unhandled error in result", 2);
}
}
}
}
[Inline]
public static implicit operator Result<T>(T value)
{
@ -39,6 +55,19 @@ namespace System
return result.Unwrap();
}
[Inline]
public static mut T operator->(ref Result<T> result)
{
switch (result)
{
case .Ok(var mut val): return mut val;
case .Err:
{
Internal.FatalError("Unhandled error in result", 2);
}
}
}
[Inline]
public void IgnoreError()
{
@ -116,9 +145,9 @@ namespace System
switch (this)
{
case .Ok(var val): return val;
case .Err(var err):
case .Err:
{
Internal.FatalError(scope String()..AppendF("Unhandled error in result:\n {}", err), 2);
Internal.FatalError("Unhandled error in result", 2);
}
}
}
@ -131,16 +160,47 @@ namespace System
}
}
public ref T ValueRef
{
[Inline]
get mut
{
switch (this)
{
case .Ok(var ref val): return ref val;
case .Err:
{
Internal.FatalError("Unhandled error in result", 2);
}
}
}
}
[Inline]
public static implicit operator Result<T, TErr>(T value)
{
return .Ok(value);
}
[Inline]
public static implicit operator T(Result<T, TErr> result)
{
return result.Unwrap();
}
[Inline]
public static mut T operator->(ref Result<T, TErr> result)
{
switch (result)
{
case .Ok(var mut val): return mut val;
case .Err:
{
Internal.FatalError("Unhandled error in result", 2);
}
}
}
public void IgnoreError()
{
}