Skip to content

Commit 54918d4

Browse files
MarijnS95notgull
authored andcommitted
Switch to let-else and ok_or() to reduce nesting
1 parent 93799d9 commit 54918d4

File tree

6 files changed

+33
-47
lines changed

6 files changed

+33
-47
lines changed

src/backends/kms.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ impl<D: HasDisplayHandle + ?Sized> ContextInterface<D> for Arc<KmsDisplayImpl<D>
4343
where
4444
D: Sized,
4545
{
46-
let fd = match display.display_handle()?.as_raw() {
47-
RawDisplayHandle::Drm(drm) => drm.fd,
48-
_ => return Err(InitError::Unsupported(display)),
46+
let RawDisplayHandle::Drm(drm) = display.display_handle()?.as_raw() else {
47+
return Err(InitError::Unsupported(display));
4948
};
50-
if fd == -1 {
49+
if drm.fd == -1 {
5150
return Err(SoftBufferError::IncompleteDisplayHandle.into());
5251
}
5352

5453
// SAFETY: Invariants guaranteed by the user.
55-
let fd = unsafe { BorrowedFd::borrow_raw(fd) };
54+
let fd = unsafe { BorrowedFd::borrow_raw(drm.fd) };
5655

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

153151
let plane_info = display
154152
.get_plane(plane_handle)

src/backends/orbital.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Orbital
135135

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

143142
Ok(Self {

src/backends/wayland/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ impl<D: HasDisplayHandle + ?Sized> ContextInterface<D> for Arc<WaylandDisplayImp
4545
D: Sized,
4646
{
4747
let raw = display.display_handle()?.as_raw();
48-
let wayland_handle = match raw {
49-
RawDisplayHandle::Wayland(w) => w.display,
50-
_ => return Err(InitError::Unsupported(display)),
48+
let RawDisplayHandle::Wayland(w) = raw else {
49+
return Err(InitError::Unsupported(display));
5150
};
5251

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

167165
let surface_id = unsafe {
168166
ObjectId::from_ptr(
169167
wl_surface::WlSurface::interface(),
170-
wayland_handle.as_ptr().cast(),
168+
w.surface.as_ptr().cast(),
171169
)
172170
}
173171
.swbuf_err("Failed to create proxy for surface ID.")?;

src/backends/web.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ pub struct WebDisplayImpl<D> {
2626
impl<D: HasDisplayHandle> ContextInterface<D> for WebDisplayImpl<D> {
2727
fn new(display: D) -> Result<Self, InitError<D>> {
2828
let raw = display.display_handle()?.as_raw();
29-
match raw {
30-
RawDisplayHandle::Web(..) => {}
31-
_ => return Err(InitError::Unsupported(display)),
32-
}
29+
let RawDisplayHandle::Web(..) = raw else {
30+
return Err(InitError::Unsupported(display));
31+
};
3332

3433
let document = web_sys::window()
3534
.swbuf_err("`Window` is not present in this runtime")?

src/backends/win32.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,8 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Win32Im
216216
/// Create a new `Win32Impl` from a `Win32WindowHandle`.
217217
fn new(window: W, _display: &D) -> Result<Self, crate::error::InitError<W>> {
218218
let raw = window.window_handle()?.as_raw();
219-
let handle = match raw {
220-
RawWindowHandle::Win32(handle) => handle,
221-
_ => return Err(crate::InitError::Unsupported(window)),
219+
let RawWindowHandle::Win32(handle) = raw else {
220+
return Err(crate::InitError::Unsupported(window));
222221
};
223222

224223
// Get the handle to the device context.

src/backends/x11.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,8 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
196196
let window_handle = match raw {
197197
RawWindowHandle::Xcb(xcb) => xcb,
198198
RawWindowHandle::Xlib(xlib) => {
199-
let window = match NonZeroU32::new(xlib.window as u32) {
200-
Some(window) => window,
201-
None => return Err(SoftBufferError::IncompleteWindowHandle.into()),
202-
};
199+
let window = NonZeroU32::new(xlib.window as u32)
200+
.ok_or(SoftBufferError::IncompleteWindowHandle)?;
203201
let mut xcb_window_handle = XcbWindowHandle::new(window);
204202
xcb_window_handle.visual_id = NonZeroU32::new(xlib.visual_id as u32);
205203
xcb_window_handle
@@ -705,26 +703,21 @@ impl ShmSegment {
705703
id.set_len(size as u64)?;
706704

707705
// Map the shared memory to our file descriptor space.
708-
let ptr = unsafe {
709-
let ptr = mm::mmap(
706+
let ptr = NonNull::new(unsafe {
707+
mm::mmap(
710708
null_mut(),
711709
size,
712710
mm::ProtFlags::READ | mm::ProtFlags::WRITE,
713711
mm::MapFlags::SHARED,
714712
&id,
715713
0,
716-
)?;
717-
718-
match NonNull::new(ptr.cast()) {
719-
Some(ptr) => ptr,
720-
None => {
721-
return Err(io::Error::new(
722-
io::ErrorKind::Other,
723-
"unexpected null when mapping SHM segment",
724-
));
725-
}
726-
}
727-
};
714+
)?
715+
})
716+
.ok_or(io::Error::new(
717+
io::ErrorKind::Other,
718+
"unexpected null when mapping SHM segment",
719+
))?
720+
.cast();
728721

729722
Ok(Self {
730723
id,

0 commit comments

Comments
 (0)