Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Runtime.CompilerServices;
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- unsafe public static ref T As<T>(this Span<byte> span) where T : struct
- {
- fixed (void* p = &span[0])
- return ref Unsafe.AsRef<T>(p);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- unsafe public static ref readonly T As<T>(this ReadOnlySpan<byte> span) where T : struct
- {
- fixed (void* p = &span[0])
- return ref Unsafe.AsRef<T>(p);
- }
- // Usage:
- // Span<byte> span = ...
- // var data = ref span.As<MyDataStruct>();
- // data.MyValue = 22; // Will modify span's memory directly
Add Comment
Please, Sign In to add comment