Skip to content

Commit

Permalink
Switch to let-else and ok_or() to reduce nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Dec 16, 2024
1 parent 09d2f68 commit dbc1246
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 47 deletions.
20 changes: 9 additions & 11 deletions src/backends/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ impl<D: HasDisplayHandle + ?Sized> ContextInterface<D> for Arc<KmsDisplayImpl<D>
where
D: Sized,
{
let fd = match display.display_handle()?.as_raw() {
RawDisplayHandle::Drm(drm) => drm.fd,
_ => return Err(InitError::Unsupported(display)),
let RawDisplayHandle::Drm(drm) = display.display_handle()?.as_raw() else {
return Err(InitError::Unsupported(display));
};
if fd == -1 {
if drm.fd == -1 {
return Err(SoftBufferError::IncompleteDisplayHandle.into());
}

// SAFETY: Invariants guaranteed by the user.
let fd = unsafe { BorrowedFd::borrow_raw(fd) };
let fd = unsafe { BorrowedFd::borrow_raw(drm.fd) };

Ok(Arc::new(KmsDisplayImpl {
fd,
Expand Down Expand Up @@ -142,13 +141,12 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
/// Create a new KMS backend.
fn new(window: W, display: &Arc<KmsDisplayImpl<D>>) -> Result<Self, InitError<W>> {
// Make sure that the window handle is valid.
let plane_handle = match window.window_handle()?.as_raw() {
RawWindowHandle::Drm(drm) => match NonZeroU32::new(drm.plane) {
Some(handle) => plane::Handle::from(handle),
None => return Err(SoftBufferError::IncompleteWindowHandle.into()),
},
_ => return Err(InitError::Unsupported(window)),
let RawWindowHandle::Drm(drm) = window.window_handle()?.as_raw() else {
return Err(InitError::Unsupported(window));
};
let plane_handle =
NonZeroU32::new(drm.plane).ok_or(SoftBufferError::IncompleteWindowHandle)?;
let plane_handle = plane::Handle::from(plane_handle);

let plane_info = display
.get_plane(plane_handle)
Expand Down
5 changes: 2 additions & 3 deletions src/backends/orbital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Orbital

fn new(window: W, _display: &D) -> Result<Self, InitError<W>> {
let raw = window.window_handle()?.as_raw();
let handle = match raw {
RawWindowHandle::Orbital(handle) => handle,
_ => return Err(InitError::Unsupported(window)),
let RawWindowHandle::Orbital(handle) = raw else {
return Err(InitError::Unsupported(window));
};

Ok(Self {
Expand Down
14 changes: 6 additions & 8 deletions src/backends/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ impl<D: HasDisplayHandle + ?Sized> ContextInterface<D> for Arc<WaylandDisplayImp
D: Sized,
{
let raw = display.display_handle()?.as_raw();
let wayland_handle = match raw {
RawDisplayHandle::Wayland(w) => w.display,
_ => return Err(InitError::Unsupported(display)),
let RawDisplayHandle::Wayland(w) = raw else {
return Err(InitError::Unsupported(display));
};

let backend = unsafe { Backend::from_foreign_display(wayland_handle.as_ptr().cast()) };
let backend = unsafe { Backend::from_foreign_display(w.display.as_ptr().cast()) };
let conn = Connection::from_backend(backend);
let (globals, event_queue) =
registry_queue_init(&conn).swbuf_err("Failed to make round trip to server")?;
Expand Down Expand Up @@ -159,15 +158,14 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
fn new(window: W, display: &Arc<WaylandDisplayImpl<D>>) -> Result<Self, InitError<W>> {
// Get the raw Wayland window.
let raw = window.window_handle()?.as_raw();
let wayland_handle = match raw {
RawWindowHandle::Wayland(w) => w.surface,
_ => return Err(InitError::Unsupported(window)),
let RawWindowHandle::Wayland(w) = raw else {
return Err(InitError::Unsupported(window));
};

let surface_id = unsafe {
ObjectId::from_ptr(
wl_surface::WlSurface::interface(),
wayland_handle.as_ptr().cast(),
w.surface.as_ptr().cast(),
)
}
.swbuf_err("Failed to create proxy for surface ID.")?;
Expand Down
7 changes: 3 additions & 4 deletions src/backends/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ pub struct WebDisplayImpl<D> {
impl<D: HasDisplayHandle> ContextInterface<D> for WebDisplayImpl<D> {
fn new(display: D) -> Result<Self, InitError<D>> {
let raw = display.display_handle()?.as_raw();
match raw {
RawDisplayHandle::Web(..) => {}
_ => return Err(InitError::Unsupported(display)),
}
let RawDisplayHandle::Web(..) = raw else {
return Err(InitError::Unsupported(display));
};

let document = web_sys::window()
.swbuf_err("`Window` is not present in this runtime")?
Expand Down
5 changes: 2 additions & 3 deletions src/backends/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Win32Im
/// Create a new `Win32Impl` from a `Win32WindowHandle`.
fn new(window: W, _display: &D) -> Result<Self, crate::error::InitError<W>> {
let raw = window.window_handle()?.as_raw();
let handle = match raw {
RawWindowHandle::Win32(handle) => handle,
_ => return Err(crate::InitError::Unsupported(window)),
let RawWindowHandle::Win32(handle) = raw else {
return Err(crate::InitError::Unsupported(window));
};

// Get the handle to the device context.
Expand Down
29 changes: 11 additions & 18 deletions src/backends/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,8 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
let window_handle = match raw {
RawWindowHandle::Xcb(xcb) => xcb,
RawWindowHandle::Xlib(xlib) => {
let window = match NonZeroU32::new(xlib.window as u32) {
Some(window) => window,
None => return Err(SoftBufferError::IncompleteWindowHandle.into()),
};
let window = NonZeroU32::new(xlib.window as u32)
.ok_or(SoftBufferError::IncompleteWindowHandle)?;
let mut xcb_window_handle = XcbWindowHandle::new(window);
xcb_window_handle.visual_id = NonZeroU32::new(xlib.visual_id as u32);
xcb_window_handle
Expand Down Expand Up @@ -705,26 +703,21 @@ impl ShmSegment {
id.set_len(size as u64)?;

// Map the shared memory to our file descriptor space.
let ptr = unsafe {
let ptr = mm::mmap(
let ptr = NonNull::new(unsafe {
mm::mmap(
null_mut(),
size,
mm::ProtFlags::READ | mm::ProtFlags::WRITE,
mm::MapFlags::SHARED,
&id,
0,
)?;

match NonNull::new(ptr.cast()) {
Some(ptr) => ptr,
None => {
return Err(io::Error::new(
io::ErrorKind::Other,
"unexpected null when mapping SHM segment",
));
}
}
};
)?
})
.ok_or(io::Error::new(
io::ErrorKind::Other,
"unexpected null when mapping SHM segment",
))?
.cast();

Ok(Self {
id,
Expand Down

0 comments on commit dbc1246

Please sign in to comment.