Skip to content

Commit

Permalink
Added support of anonymous value reference
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Aug 14, 2024
1 parent 2eaf742 commit b2187bc
Show file tree
Hide file tree
Showing 2 changed files with 24 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 @@ -94,6 +94,16 @@ public static void ImmutableEmptyRef()
Null(reference.ToString());
}

[Fact]
public static void AnonymousValue()
{
var reference = new ValueReference<int>(42);
Equal(42, reference.Value);

ReadOnlyValueReference<int> roRef = reference;
Equal(42, roRef.Value);
}

private record class MyClass : IResettable
{
internal int Field;
Expand Down
14 changes: 14 additions & 0 deletions src/DotNext/Runtime/ValueReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public ValueReference(T[] array, int index)
{
}

private ValueReference(StrongBox<T> box)
: this(box, ref box.Value!)
{
}

/// <summary>
/// Creates a reference to the anonymous value.
/// </summary>
/// <param name="value">The anonymous value.</param>
public ValueReference(T value)
: this(new StrongBox<T> { Value = value })
{
}

/// <summary>
/// Gets a value indicating that is reference is empty.
/// </summary>
Expand Down

0 comments on commit b2187bc

Please sign in to comment.