11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Diagnostics ;
45using System . Runtime . CompilerServices ;
56using System . Runtime . InteropServices ;
67using System . Runtime . Intrinsics ;
@@ -54,7 +55,8 @@ public static bool TryWriteBytes(Span<byte> destination, bool value)
5455 public static byte [ ] GetBytes ( char value )
5556 {
5657 byte [ ] bytes = new byte [ sizeof ( char ) ] ;
57- Unsafe . As < byte , char > ( ref bytes [ 0 ] ) = value;
58+ bool success = TryWriteBytes ( bytes , value ) ;
59+ Debug . Assert ( success ) ;
5860 return bytes ;
5961 }
6062
@@ -81,7 +83,8 @@ public static bool TryWriteBytes(Span<byte> destination, char value)
8183 public static byte [ ] GetBytes ( short value )
8284 {
8385 byte [ ] bytes = new byte [ sizeof ( short ) ] ;
84- Unsafe . As < byte , short > ( ref bytes [ 0 ] ) = value;
86+ bool success = TryWriteBytes ( bytes , value ) ;
87+ Debug . Assert ( success ) ;
8588 return bytes ;
8689 }
8790
@@ -108,7 +111,8 @@ public static bool TryWriteBytes(Span<byte> destination, short value)
108111 public static byte [ ] GetBytes ( int value )
109112 {
110113 byte [ ] bytes = new byte [ sizeof ( int ) ] ;
111- Unsafe . As < byte , int > ( ref bytes [ 0 ] ) = value;
114+ bool success = TryWriteBytes ( bytes , value ) ;
115+ Debug . Assert ( success ) ;
112116 return bytes ;
113117 }
114118
@@ -135,7 +139,8 @@ public static bool TryWriteBytes(Span<byte> destination, int value)
135139 public static byte [ ] GetBytes ( long value )
136140 {
137141 byte [ ] bytes = new byte [ sizeof ( long ) ] ;
138- Unsafe . As < byte , long > ( ref bytes [ 0 ] ) = value;
142+ bool success = TryWriteBytes ( bytes , value ) ;
143+ Debug . Assert ( success ) ;
139144 return bytes ;
140145 }
141146
@@ -162,7 +167,8 @@ public static bool TryWriteBytes(Span<byte> destination, long value)
162167 public static byte [ ] GetBytes ( Int128 value )
163168 {
164169 byte [ ] bytes = new byte [ Int128 . Size ] ;
165- Unsafe . As < byte , Int128 > ( ref bytes [ 0 ] ) = value;
170+ bool success = TryWriteBytes ( bytes , value ) ;
171+ Debug . Assert ( success ) ;
166172 return bytes ;
167173 }
168174
@@ -190,7 +196,8 @@ public static bool TryWriteBytes(Span<byte> destination, Int128 value)
190196 public static byte [ ] GetBytes ( ushort value )
191197 {
192198 byte [ ] bytes = new byte [ sizeof ( ushort ) ] ;
193- Unsafe . As < byte , ushort > ( ref bytes [ 0 ] ) = value;
199+ bool success = TryWriteBytes ( bytes , value ) ;
200+ Debug . Assert ( success ) ;
194201 return bytes ;
195202 }
196203
@@ -219,7 +226,8 @@ public static bool TryWriteBytes(Span<byte> destination, ushort value)
219226 public static byte [ ] GetBytes ( uint value )
220227 {
221228 byte [ ] bytes = new byte [ sizeof ( uint ) ] ;
222- Unsafe . As < byte , uint > ( ref bytes [ 0 ] ) = value;
229+ bool success = TryWriteBytes ( bytes , value ) ;
230+ Debug . Assert ( success ) ;
223231 return bytes ;
224232 }
225233
@@ -248,7 +256,8 @@ public static bool TryWriteBytes(Span<byte> destination, uint value)
248256 public static byte [ ] GetBytes ( ulong value )
249257 {
250258 byte [ ] bytes = new byte [ sizeof ( ulong ) ] ;
251- Unsafe . As < byte , ulong > ( ref bytes [ 0 ] ) = value;
259+ bool success = TryWriteBytes ( bytes , value ) ;
260+ Debug . Assert ( success ) ;
252261 return bytes ;
253262 }
254263
@@ -277,7 +286,8 @@ public static bool TryWriteBytes(Span<byte> destination, ulong value)
277286 public static byte [ ] GetBytes ( UInt128 value )
278287 {
279288 byte [ ] bytes = new byte [ UInt128 . Size ] ;
280- Unsafe . As < byte , UInt128 > ( ref bytes [ 0 ] ) = value;
289+ bool success = TryWriteBytes ( bytes , value ) ;
290+ Debug . Assert ( success ) ;
281291 return bytes ;
282292 }
283293
@@ -305,7 +315,8 @@ public static bool TryWriteBytes(Span<byte> destination, UInt128 value)
305315 public static unsafe byte [ ] GetBytes ( Half value )
306316 {
307317 byte [ ] bytes = new byte [ sizeof ( Half ) ] ;
308- Unsafe . As < byte , Half > ( ref bytes [ 0 ] ) = value;
318+ bool success = TryWriteBytes ( bytes , value ) ;
319+ Debug . Assert ( success ) ;
309320 return bytes ;
310321 }
311322
@@ -332,7 +343,8 @@ public static unsafe bool TryWriteBytes(Span<byte> destination, Half value)
332343 public static byte [ ] GetBytes ( float value )
333344 {
334345 byte [ ] bytes = new byte [ sizeof ( float ) ] ;
335- Unsafe . As < byte , float > ( ref bytes [ 0 ] ) = value;
346+ bool success = TryWriteBytes ( bytes , value ) ;
347+ Debug . Assert ( success ) ;
336348 return bytes ;
337349 }
338350
@@ -359,7 +371,8 @@ public static bool TryWriteBytes(Span<byte> destination, float value)
359371 public static byte [ ] GetBytes ( double value )
360372 {
361373 byte [ ] bytes = new byte [ sizeof ( double ) ] ;
362- Unsafe . As < byte , double > ( ref bytes [ 0 ] ) = value;
374+ bool success = TryWriteBytes ( bytes , value ) ;
375+ Debug . Assert ( success ) ;
363376 return bytes ;
364377 }
365378
0 commit comments