Skip to content

Commit 2167d4d

Browse files
committed
Fixed AOT compatibility
1 parent 404024f commit 2167d4d

File tree

6 files changed

+79
-72
lines changed

6 files changed

+79
-72
lines changed

src/DotNext.AotTests/Runtime/BoxedValueTests.cs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,55 @@ public void BoxUnbox()
99
var obj = (BoxedValue<int>)42;
1010
Assert.AreEqual(42.GetHashCode(), obj.GetHashCode());
1111
Assert.AreEqual(42, obj.Unbox());
12-
Assert.AreEqual(42, (int)obj);
12+
Assert.AreEqual(42, obj);
1313
Assert.AreEqual(typeof(int), obj.GetType());
1414
}
1515

16-
// [TestMethod]
17-
// public void Unwrap()
18-
// {
19-
// object? obj = null;
20-
// Assert.IsNull(BoxedValue<int>.GetTypedReference(obj));
21-
//
22-
// obj = 42;
23-
// Assert.AreEqual(42, BoxedValue<int>.GetTypedReference(obj).Value);
24-
//
25-
// obj = string.Empty;
26-
// Assert.ThrowsException<ArgumentException>(() => BoxedValue<int>.GetTypedReference(obj));
27-
// }
28-
//
29-
// [TestMethod]
30-
// public void ToUntypedReference()
31-
// {
32-
// ValueType obj = BoxedValue<int>.Box(42);
33-
// Assert.AreEqual(42, obj);
34-
// }
35-
//
36-
// private struct MutableStruct
37-
// {
38-
// public int Value;
39-
// }
40-
//
41-
// [TestMethod]
42-
// public void BitwiseCopyImmutable()
43-
// {
44-
// var boxed1 = (BoxedValue<int>)42;
45-
// var boxed2 = boxed1.Copy();
46-
// Assert.AreNotSame(boxed1, boxed2);
47-
// Assert.AreEqual(42, boxed1);
48-
// Assert.AreEqual(42, boxed2);
49-
// }
50-
//
51-
// [TestMethod]
52-
// public void BitwiseCopyMutable()
53-
// {
54-
// var boxed1 = (BoxedValue<MutableStruct>)new MutableStruct();
55-
// var boxed2 = boxed1.Copy();
56-
// Assert.AreNotSame(boxed1, boxed2);
57-
//
58-
// boxed1.Value.Value = 42;
59-
// boxed2.Value.Value = 43;
60-
//
61-
// Assert.AreNotEqual(boxed1.Value.Value, boxed2.Value.Value);
62-
// }
16+
[TestMethod]
17+
public void Unwrap()
18+
{
19+
object? obj = null;
20+
Assert.IsNull(BoxedValue<int>.GetTypedReference(obj));
21+
22+
obj = 42;
23+
Assert.AreEqual(42, BoxedValue<int>.GetTypedReference(obj));
24+
25+
obj = string.Empty;
26+
Assert.ThrowsException<ArgumentException>(() => BoxedValue<int>.GetTypedReference(obj));
27+
}
28+
29+
[TestMethod]
30+
public void ToUntypedReference()
31+
{
32+
ValueType obj = BoxedValue<int>.Box(42);
33+
Assert.AreEqual(42, obj);
34+
}
35+
36+
private struct MutableStruct
37+
{
38+
public int Value;
39+
}
40+
41+
[TestMethod]
42+
public void BitwiseCopyImmutable()
43+
{
44+
var boxed1 = (BoxedValue<int>)42;
45+
var boxed2 = boxed1.Copy();
46+
Assert.AreNotSame(boxed1, boxed2);
47+
Assert.AreEqual(42, boxed1);
48+
Assert.AreEqual(42, boxed2);
49+
}
50+
51+
[TestMethod]
52+
public void BitwiseCopyMutable()
53+
{
54+
var boxed1 = (BoxedValue<MutableStruct>)new MutableStruct();
55+
var boxed2 = boxed1.Copy();
56+
Assert.AreNotSame(boxed1, boxed2);
57+
58+
boxed1.Unbox().Value = 42;
59+
boxed2.Unbox().Value = 43;
60+
61+
Assert.AreNotEqual(boxed1.Unbox().Value, boxed2.Unbox().Value);
62+
}
6363
}

src/DotNext.AotTests/Runtime/ValueReferenceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public void BoxedValueInterop()
145145
var boxedInt = BoxedValue<int>.Box(42);
146146
ValueReference<int> reference = boxedInt;
147147

148-
boxedInt.Value = 56;
149-
Assert.AreEqual(boxedInt.Value, reference.Value);
148+
boxedInt.Unbox() = 56;
149+
Assert.AreEqual(boxedInt, reference.Value);
150150
}
151151

152152
[TestMethod]

src/DotNext.Tests/Runtime/BoxedValueTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public static void BoxUnbox()
77
{
88
var obj = (BoxedValue<int>)42;
99
Equal(42.GetHashCode(), obj.GetHashCode());
10-
Equal(42, obj.Value);
11-
Equal(42, (int)obj);
10+
Equal(42, obj.Unbox());
11+
Equal(42, obj);
1212
Equal(typeof(int), obj.GetType());
1313
}
1414

@@ -19,7 +19,7 @@ public static void Unwrap()
1919
Null(BoxedValue<int>.GetTypedReference(obj));
2020

2121
obj = 42;
22-
Equal(42, BoxedValue<int>.GetTypedReference(obj).Value);
22+
Equal(42, BoxedValue<int>.GetTypedReference(obj));
2323

2424
obj = string.Empty;
2525
Throws<ArgumentException>(() => BoxedValue<int>.GetTypedReference(obj));
@@ -54,9 +54,9 @@ public static void BitwiseCopyMutable()
5454
var boxed2 = boxed1.Copy();
5555
NotSame(boxed1, boxed2);
5656

57-
boxed1.Value.Value = 42;
58-
boxed2.Value.Value = 43;
57+
boxed1.Unbox().Value = 42;
58+
boxed2.Unbox().Value = 43;
5959

60-
NotEqual(boxed1.Value.Value, boxed2.Value.Value);
60+
NotEqual(boxed1.Unbox().Value, boxed2.Unbox().Value);
6161
}
6262
}

src/DotNext.Tests/Runtime/ValueReferenceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ public static void BoxedValueInterop()
173173
var boxedInt = BoxedValue<int>.Box(42);
174174
ValueReference<int> reference = boxedInt;
175175

176-
boxedInt.Value = 56;
177-
Equal(boxedInt.Value, reference.Value);
176+
boxedInt.Unbox() = 56;
177+
Equal(boxedInt, reference.Value);
178178
}
179179

180180
[Fact]

src/DotNext/Runtime/BoxedValue.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using System.Diagnostics.CodeAnalysis;
32
using System.Runtime.CompilerServices;
43

@@ -26,12 +25,6 @@ namespace DotNext.Runtime;
2625
{
2726
internal T value;
2827

29-
/// <summary>
30-
/// Gets a reference to the boxed value.
31-
/// </summary>
32-
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
33-
public ref T Value => ref value;
34-
3528
/// <summary>
3629
/// Converts untyped reference to a boxed value into a typed reference.
3730
/// </summary>
@@ -86,12 +79,6 @@ namespace DotNext.Runtime;
8679
public static implicit operator ValueType?(BoxedValue<T>? boxedValue)
8780
=> Unsafe.As<ValueType>(boxedValue);
8881

89-
/// <summary>
90-
/// Creates a bitwise copy of the boxed value.
91-
/// </summary>
92-
/// <returns>A reference to bitwise copy of the boxed value.</returns>
93-
public BoxedValue<T> Copy() => Unsafe.As<BoxedValue<T>>(MemberwiseClone());
94-
9582
/// <summary>
9683
/// Boxes nullable value type to an object.
9784
/// </summary>
@@ -118,9 +105,28 @@ public static implicit operator ValueReference<T>(BoxedValue<T> boxedValue)
118105
public abstract override string ToString(); // abstract to avoid inlining by AOT/JIT
119106
}
120107

108+
/// <summary>
109+
/// Represents extension methods for <see cref="BoxedValue{T}"/> class.
110+
/// </summary>
121111
public static class BoxedValue
122112
{
113+
/// <summary>
114+
/// Unboxes the value.
115+
/// </summary>
116+
/// <param name="boxedValue">A reference to the boxed value.</param>
117+
/// <typeparam name="T">The value type.</typeparam>
118+
/// <returns>A reference to the boxed value.</returns>
123119
public static ref T Unbox<T>(this BoxedValue<T> boxedValue)
124120
where T : struct
125121
=> ref boxedValue.value;
122+
123+
/// <summary>
124+
/// Creates a bitwise copy of the boxed value.
125+
/// </summary>
126+
/// <returns>A reference to bitwise copy of the boxed value.</returns>
127+
public static BoxedValue<T> Copy<T>(this BoxedValue<T> boxedValue) where T : struct
128+
=> Unsafe.As<BoxedValue<T>>(MemberwiseClone(boxedValue));
129+
130+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(MemberwiseClone))]
131+
private static extern object MemberwiseClone(object target);
126132
}

src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/IPersistentState.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace DotNext.Net.Cluster.Consensus.Raft;
22

3+
using Runtime;
34
using BoxedClusterMemberId = Runtime.BoxedValue<ClusterMemberId>;
45

56
/// <summary>
@@ -54,5 +55,5 @@ public interface IPersistentState : IO.Log.IAuditTrail<IRaftLogEntry>
5455
ValueTask UpdateVotedForAsync(ClusterMemberId member, CancellationToken token = default);
5556

5657
internal static bool IsVotedFor(BoxedClusterMemberId? lastVote, in ClusterMemberId expected)
57-
=> lastVote is null || lastVote.Value == expected;
58+
=> lastVote is null || lastVote.Unbox() == expected;
5859
}

0 commit comments

Comments
 (0)