Guest User

Untitled

a guest
May 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. using System.Runtime.CompilerServices;
  2.  
  3. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  4. unsafe public static ref T As<T>(this Span<byte> span) where T : struct
  5. {
  6. fixed (void* p = &span[0])
  7. return ref Unsafe.AsRef<T>(p);
  8. }
  9.  
  10. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  11. unsafe public static ref readonly T As<T>(this ReadOnlySpan<byte> span) where T : struct
  12. {
  13. fixed (void* p = &span[0])
  14. return ref Unsafe.AsRef<T>(p);
  15. }
  16.  
  17. // Usage:
  18. // Span<byte> span = ...
  19. // var data = ref span.As<MyDataStruct>();
  20. // data.MyValue = 22; // Will modify span's memory directly
Add Comment
Please, Sign In to add comment