Skip to content

Commit e7e809e

Browse files
committed
Remove bytemuck dependency
1 parent 688a216 commit e7e809e

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ harness = false
1818

1919
[features]
2020
default = ["kms", "x11", "x11-dlopen", "wayland", "wayland-dlopen"]
21-
kms = ["bytemuck", "drm", "rustix"]
21+
kms = ["drm", "rustix"]
2222
wayland = [
2323
"wayland-backend",
2424
"wayland-client",
@@ -32,7 +32,6 @@ wayland = [
3232
wayland-dlopen = ["wayland-sys/dlopen", "winit/wayland-dlopen"]
3333
x11 = [
3434
"as-raw-xcb-connection",
35-
"bytemuck",
3635
"fastrand",
3736
"rustix",
3837
"tiny-xlib",
@@ -48,12 +47,10 @@ raw_window_handle = { package = "raw-window-handle", version = "0.6", features =
4847
tracing = { version = "0.1.41", default-features = false }
4948

5049
[target.'cfg(target_os = "android")'.dependencies]
51-
bytemuck = "1.12.3"
5250
ndk = "0.9.0"
5351

5452
[target.'cfg(not(any(target_os = "android", target_vendor = "apple", target_os = "redox", target_family = "wasm", target_os = "windows")))'.dependencies]
5553
as-raw-xcb-connection = { version = "1.0.0", optional = true }
56-
bytemuck = { version = "1.12.3", optional = true }
5754
drm = { version = "0.14.1", default-features = false, optional = true }
5855
fastrand = { version = "2.0.0", optional = true }
5956
memmap2 = { version = "0.9.0", optional = true }

src/backends/kms.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, Raw
1313

1414
use std::collections::HashSet;
1515
use std::fmt;
16+
use std::mem::size_of;
1617
use std::num::NonZeroU32;
1718
use std::os::unix::io::{AsFd, BorrowedFd};
19+
use std::slice;
1820
use std::sync::Arc;
1921

2022
use crate::backend_interface::*;
@@ -315,7 +317,12 @@ impl BufferInterface for BufferImpl<'_> {
315317

316318
#[inline]
317319
fn pixels_mut(&mut self) -> &mut [u32] {
318-
bytemuck::cast_slice_mut(self.mapping.as_mut())
320+
let ptr = self.mapping.as_mut_ptr().cast::<u32>();
321+
let len = self.mapping.len() / size_of::<u32>();
322+
debug_assert_eq!(self.mapping.len() % size_of::<u32>(), 0);
323+
// SAFETY: `&mut [u8]` can be reinterpreted as `&mut [u32]`, assuming that the allocation
324+
// is aligned to at least a multiple of 4 bytes.
325+
unsafe { slice::from_raw_parts_mut(ptr, len) }
319326
}
320327

321328
#[inline]

src/backends/x11.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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()
@@ -610,7 +623,14 @@ impl ShmBuffer {
610623
let buffer_size = seg.buffer_size();
611624

612625
// SAFETY: No other code should be able to access the segment.
613-
bytemuck::cast_slice_mut(unsafe { &mut seg.as_mut()[..buffer_size] })
626+
let segment = unsafe { &mut seg.as_mut()[..buffer_size] };
627+
628+
let ptr = segment.as_mut_ptr().cast::<u32>();
629+
let len = segment.len() / size_of::<u32>();
630+
debug_assert_eq!(segment.len() % size_of::<u32>(), 0);
631+
// SAFETY: The segment buffer is a multiple of `u32`, and we assume that the
632+
// memmap allocation is aligned to at least a multiple of 4 bytes.
633+
unsafe { slice::from_raw_parts_mut(ptr, len) }
614634
}
615635
None => {
616636
// Nothing has been allocated yet.

0 commit comments

Comments
 (0)