@@ -21,7 +21,8 @@ use std::{
2121 collections:: HashSet ,
2222 fmt,
2323 fs:: File ,
24- io, mem,
24+ io,
25+ mem:: { self , size_of} ,
2526 num:: { NonZeroU16 , NonZeroU32 } ,
2627 ptr:: { null_mut, NonNull } ,
2728 slice,
@@ -383,8 +384,15 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
383384 . swbuf_err ( "Failed to fetch image from window" ) ?;
384385
385386 if reply. depth == self . depth && reply. visual == self . visual_id {
386- let mut out = vec ! [ 0u32 ; reply. data. len( ) / 4 ] ;
387- bytemuck:: cast_slice_mut :: < u32 , u8 > ( & mut out) . copy_from_slice ( & reply. data ) ;
387+ let mut out = vec ! [ 0 ; reply. data. len( ) / size_of:: <u32 >( ) ] ;
388+ // SAFETY: `u32` can be re-interpreted as `[u8; 4]`.
389+ let out_u8s = unsafe {
390+ slice:: from_raw_parts_mut (
391+ out. as_mut_ptr ( ) . cast :: < u8 > ( ) ,
392+ out. len ( ) * size_of :: < u32 > ( ) ,
393+ )
394+ } ;
395+ out_u8s. copy_from_slice ( & reply. data ) ;
388396 Ok ( out)
389397 } else {
390398 Err ( SoftBufferError :: PlatformError (
@@ -443,6 +451,11 @@ impl BufferInterface for BufferImpl<'_> {
443451 // This is a suboptimal strategy, raise a stink in the debug logs.
444452 tracing:: debug!( "Falling back to non-SHM method for window drawing." ) ;
445453
454+ // SAFETY: `u32` can be re-interpreted as `[u8; 4]`.
455+ let data = unsafe {
456+ slice:: from_raw_parts ( wire. as_ptr ( ) . cast :: < u8 > ( ) , wire. len ( ) * size_of :: < u32 > ( ) )
457+ } ;
458+
446459 self . connection
447460 . put_image (
448461 xproto:: ImageFormat :: Z_PIXMAP ,
@@ -454,7 +467,7 @@ impl BufferInterface for BufferImpl<'_> {
454467 0 ,
455468 0 ,
456469 self . depth ,
457- bytemuck :: cast_slice ( wire ) ,
470+ data ,
458471 )
459472 . map ( |c| c. ignore_error ( ) )
460473 . push_err ( )
@@ -598,7 +611,14 @@ impl ShmBuffer {
598611 let buffer_size = seg. buffer_size ( ) ;
599612
600613 // SAFETY: No other code should be able to access the segment.
601- bytemuck:: cast_slice_mut ( unsafe { & mut seg. as_mut ( ) [ ..buffer_size] } )
614+ let segment = unsafe { & mut seg. as_mut ( ) [ ..buffer_size] } ;
615+
616+ let ptr = segment. as_mut_ptr ( ) . cast :: < u32 > ( ) ;
617+ let len = segment. len ( ) / size_of :: < u32 > ( ) ;
618+ debug_assert_eq ! ( segment. len( ) % size_of:: <u32 >( ) , 0 ) ;
619+ // SAFETY: The segment buffer is a multiple of `u32`, and we assume that the
620+ // memmap allocation is aligned to at least a multiple of 4 bytes.
621+ unsafe { slice:: from_raw_parts_mut ( ptr, len) }
602622 }
603623 None => {
604624 // Nothing has been allocated yet.
0 commit comments