From 2ec03d714956bfb24539a540cc4eafd1e6ecf601 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Thu, 9 Nov 2023 00:35:50 +0100 Subject: [PATCH] Switch to `let-else` and `ok_or()` to reduce nesting --- src/backends/kms.rs | 20 +++++++++----------- src/backends/orbital.rs | 5 ++--- src/backends/wayland/mod.rs | 14 ++++++-------- src/backends/web.rs | 7 +++---- src/backends/win32.rs | 5 ++--- src/backends/x11.rs | 29 +++++++++++------------------ 6 files changed, 33 insertions(+), 47 deletions(-) diff --git a/src/backends/kms.rs b/src/backends/kms.rs index 901fc738..da0369b6 100644 --- a/src/backends/kms.rs +++ b/src/backends/kms.rs @@ -43,16 +43,15 @@ impl ContextInterface for Arc 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, @@ -139,13 +138,12 @@ impl SurfaceInterface fo /// Create a new KMS backend. fn new(window: W, display: &Arc>) -> Result> { // 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) diff --git a/src/backends/orbital.rs b/src/backends/orbital.rs index 96c3db1d..52f3636e 100644 --- a/src/backends/orbital.rs +++ b/src/backends/orbital.rs @@ -132,9 +132,8 @@ impl SurfaceInterface for Orbital fn new(window: W, _display: &D) -> Result> { 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 { diff --git a/src/backends/wayland/mod.rs b/src/backends/wayland/mod.rs index 2dda8d2b..fe4cf366 100644 --- a/src/backends/wayland/mod.rs +++ b/src/backends/wayland/mod.rs @@ -45,12 +45,11 @@ impl ContextInterface for Arc 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")?; @@ -156,15 +155,14 @@ impl SurfaceInterface fn new(window: W, display: &Arc>) -> Result> { // 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.")?; diff --git a/src/backends/web.rs b/src/backends/web.rs index 539e2d2a..5c0e00f1 100644 --- a/src/backends/web.rs +++ b/src/backends/web.rs @@ -26,10 +26,9 @@ pub struct WebDisplayImpl { impl ContextInterface for WebDisplayImpl { fn new(display: D) -> Result> { 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")? diff --git a/src/backends/win32.rs b/src/backends/win32.rs index 084ad47b..8d7108b9 100644 --- a/src/backends/win32.rs +++ b/src/backends/win32.rs @@ -213,9 +213,8 @@ impl SurfaceInterface for Win32Im /// Create a new `Win32Impl` from a `Win32WindowHandle`. fn new(window: W, _display: &D) -> Result> { 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. diff --git a/src/backends/x11.rs b/src/backends/x11.rs index b01fb5f3..bde94a78 100644 --- a/src/backends/x11.rs +++ b/src/backends/x11.rs @@ -193,10 +193,8 @@ impl SurfaceInterface 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 @@ -702,26 +700,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,