@@ -223,6 +223,18 @@ impl<const LIMBS: usize> Uint<LIMBS> {
223223#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
224224pub struct EncodedUint < const LIMBS : usize > ( [ Word ; LIMBS ] ) ;
225225
226+ #[ allow( unsafe_code) ]
227+ const fn cast_slice ( limbs : & [ Word ] ) -> & [ u8 ] {
228+ let new_len = size_of_val ( limbs) ;
229+ unsafe { core:: slice:: from_raw_parts ( limbs. as_ptr ( ) as * mut u8 , new_len) }
230+ }
231+
232+ #[ allow( unsafe_code) ]
233+ const fn cast_slice_mut ( limbs : & mut [ Word ] ) -> & mut [ u8 ] {
234+ let new_len = size_of_val ( limbs) ;
235+ unsafe { core:: slice:: from_raw_parts_mut ( limbs. as_mut_ptr ( ) as * mut u8 , new_len) }
236+ }
237+
226238impl < const LIMBS : usize > EncodedUint < LIMBS > {
227239 const fn new_le ( value : & Uint < LIMBS > ) -> Self {
228240 let mut buffer = [ 0 ; LIMBS ] ;
@@ -233,8 +245,7 @@ impl<const LIMBS: usize> EncodedUint<LIMBS> {
233245
234246 // We could cast the whole `buffer` to bytes at once,
235247 // but IndexMut does not work in const context.
236- let dst_bytes: & mut [ u8 ] =
237- bytemuck:: must_cast_slice_mut ( core:: slice:: from_mut ( & mut buffer[ i] ) ) ;
248+ let dst_bytes: & mut [ u8 ] = cast_slice_mut ( core:: slice:: from_mut ( & mut buffer[ i] ) ) ;
238249
239250 // `copy_from_slice` can be used here when MSRV moves past 1.87
240251 let mut j = 0 ;
@@ -257,7 +268,7 @@ impl<const LIMBS: usize> EncodedUint<LIMBS> {
257268 // We could cast the whole `buffer` to bytes at once,
258269 // but IndexMut does not work in const context.
259270 let dst_bytes: & mut [ u8 ] =
260- bytemuck :: must_cast_slice_mut ( core:: slice:: from_mut ( & mut buffer[ LIMBS - 1 - i] ) ) ;
271+ cast_slice_mut ( core:: slice:: from_mut ( & mut buffer[ LIMBS - 1 - i] ) ) ;
261272
262273 // `copy_from_slice` can be used here when MSRV moves past 1.87
263274 let mut j = 0 ;
@@ -280,13 +291,13 @@ impl<const LIMBS: usize> Default for EncodedUint<LIMBS> {
280291
281292impl < const LIMBS : usize > AsRef < [ u8 ] > for EncodedUint < LIMBS > {
282293 fn as_ref ( & self ) -> & [ u8 ] {
283- bytemuck :: must_cast_slice ( & self . 0 )
294+ cast_slice ( & self . 0 )
284295 }
285296}
286297
287298impl < const LIMBS : usize > AsMut < [ u8 ] > for EncodedUint < LIMBS > {
288299 fn as_mut ( & mut self ) -> & mut [ u8 ] {
289- bytemuck :: must_cast_slice_mut ( & mut self . 0 )
300+ cast_slice_mut ( & mut self . 0 )
290301 }
291302}
292303
0 commit comments