Skip to content

Commit

Permalink
Added support of boxed value reference
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Aug 17, 2024
1 parent 29a3202 commit bd0a56e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/DotNext.Tests/Runtime/ValueReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ public static void ReferenceSize()
Equal(Unsafe.SizeOf<ValueReference<float>>(), nint.Size + nint.Size);
}

[Fact]
public static void BoxedValueInterop()
{
var boxedInt = BoxedValue<int>.Box(42);
ValueReference<int> reference = boxedInt;

boxedInt.Value = 56;
Equal(boxedInt.Value, reference.Value);
}

private record class MyClass : IResettable
{
internal static string StaticObject;
Expand Down
8 changes: 8 additions & 0 deletions src/DotNext/Runtime/BoxedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ namespace DotNext.Runtime;
[return: NotNullIfNotNull(nameof(value))]
public static explicit operator BoxedValue<T>?(in T? value) => TryBox(in value);

/// <summary>
/// Obtains a reference to the boxed value.
/// </summary>
/// <param name="boxedValue">Boxed value.</param>
/// <returns>Mutable reference to the boxed value.</returns>
public static implicit operator ValueReference<T>(BoxedValue<T> boxedValue)
=> new(boxedValue, ref boxedValue.Value);

/// <inheritdoc />
public abstract override bool Equals([NotNullWhen(true)] object? obj); // abstract to avoid inlining by AOT/JIT

Expand Down

0 comments on commit bd0a56e

Please sign in to comment.