File tree Expand file tree Collapse file tree 4 files changed +38
-7
lines changed
DotNext.Unsafe/Runtime/InteropServices Expand file tree Collapse file tree 4 files changed +38
-7
lines changed Original file line number Diff line number Diff line change 33
44namespace DotNext . Runtime ;
55
6+ using InteropServices ;
7+
68public sealed class ValueReferenceTests : Test
79{
810 [ Fact ]
@@ -85,6 +87,7 @@ public static void MutableEmptyRef()
8587 var reference = default ( ValueReference < string > ) ;
8688 True ( reference . IsEmpty ) ;
8789 Null ( reference . ToString ( ) ) ;
90+ Throws < NullReferenceException > ( ( ) => reference . Value ) ;
8891
8992 Span < string > span = reference ;
9093 True ( span . IsEmpty ) ;
@@ -99,6 +102,7 @@ public static void ImmutableEmptyRef()
99102 var reference = default ( ReadOnlyValueReference < string > ) ;
100103 True ( reference . IsEmpty ) ;
101104 Null ( reference . ToString ( ) ) ;
105+ Throws < NullReferenceException > ( ( ) => reference . Value ) ;
102106
103107 ReadOnlySpan < string > span = reference ;
104108 True ( span . IsEmpty ) ;
@@ -209,7 +213,7 @@ public static void ReadOnlySpanInterop()
209213
210214
211215
212- private record class MyClass : IResettable
216+ private record MyClass : IResettable
213217 {
214218 internal static string StaticObject ;
215219
@@ -224,4 +228,19 @@ public virtual void Reset()
224228
225229 }
226230 }
231+
232+ [ Fact ]
233+ public static unsafe void PinAnonymousValue ( )
234+ {
235+ ValueReference < int > valueRef = new ( 42 ) ;
236+ fixed ( int * ptr = valueRef )
237+ {
238+ Equal ( 42 , * ptr ) ;
239+ }
240+
241+ fixed ( int * ptr = ( ReadOnlyValueReference < int > ) valueRef )
242+ {
243+ Equal ( 42 , * ptr ) ;
244+ }
245+ }
227246}
Original file line number Diff line number Diff line change @@ -149,7 +149,7 @@ public unsafe Pointer(nuint ptr)
149149 /// Determines whether this pointer is aligned
150150 /// to the size of <typeparamref name="T"/>.
151151 /// </summary>
152- public unsafe bool IsAligned => Address % Intrinsics . AlignOf < T > ( ) is 0 ;
152+ public bool IsAligned => Address % Intrinsics . AlignOf < T > ( ) is 0 ;
153153
154154 /// <summary>
155155 /// Fills the elements of the array with a specified value.
Original file line number Diff line number Diff line change @@ -413,11 +413,7 @@ private static void KeepAlive(ref readonly byte location)
413413 }
414414
415415 internal static ref byte GetRawData ( object obj )
416- {
417- Debug . Assert ( obj is not null ) ;
418-
419- return ref Unsafe . As < RawData > ( obj ) . data ;
420- }
416+ => ref Unsafe . As < RawData > ( obj ) . data ;
421417}
422418
423419file abstract class RawData
Original file line number Diff line number Diff line change @@ -80,6 +80,14 @@ public ValueReference(ref T staticFieldRef)
8080 /// Gets a reference to the field.
8181 /// </summary>
8282 public ref T Value => ref ValueReference . GetObjectData < T > ( owner , offset ) ;
83+
84+ /// <summary>
85+ /// Obtains managed pointer to the referenced value.
86+ /// </summary>
87+ /// <returns>The managed pointer to the referenced value.</returns>
88+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
89+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
90+ public ref T GetPinnableReference ( ) => ref Value ;
8391
8492 /// <inheritdoc cref="IConsumer{T}.Invoke(T)"/>
8593 void IConsumer < T > . Invoke ( T value ) => Value = value ;
@@ -240,6 +248,14 @@ public ReadOnlyValueReference(ref readonly T staticFieldRef)
240248 /// </summary>
241249 public ref readonly T Value => ref ValueReference . GetObjectData < T > ( owner , offset ) ;
242250
251+ /// <summary>
252+ /// Obtains managed pointer to the referenced value.
253+ /// </summary>
254+ /// <returns>The managed pointer to the referenced value.</returns>
255+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
256+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
257+ public ref readonly T GetPinnableReference ( ) => ref Value ;
258+
243259 /// <inheritdoc cref="ISupplier{T}.Invoke()"/>
244260 T ISupplier < T > . Invoke ( ) => Value ;
245261
You can’t perform that action at this time.
0 commit comments