Skip to contentSkip to conversation

rust: file: use to_result for error handling

PATCH Applied Onur Özkan 3 participants 3 messages Aug 21, 2025 – Aug 25, 2025

Subsystems (2)
lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

PATCH 1/1 rust: file: use to_result for error handling

Simplifies error handling by replacing the manual check of the return value with the to_result helper.

---
 rust/kernel/fs/file.rs | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
rust/kernel/fs/file.rs +3 -4
index 35fd5db35c46..924f01bd64c2 100644
@@ -10,7 +10,7 @@
 use crate::{
     bindings,
     cred::Credential,
-    error::{code::*, Error, Result},
+    error::{code::*, to_result, Error, Result},
     types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque},
 };
 use core::ptr;
@@ -398,9 +398,8 @@ impl FileDescriptorReservation {
     pub fn get_unused_fd_flags(flags: u32) -> Result<Self> {
         // SAFETY: FFI call, there are no safety requirements on `flags`.
         let fd: i32 = unsafe { bindings::get_unused_fd_flags(flags) };
-        if fd < 0 {
-            return Err(Error::from_errno(fd));
-        }
+        to_result(fd)?;
+
         Ok(Self {
             fd: fd as u32,
             _not_send: NotThreadSafe,

--
2.50.0

Reviewed-by on v1 1/1
#2 lore raw
In reply to Onur Özkan
Quoted 23 lines · Onur Özkan

Simplifies error handling by replacing the manual check of the return value with the to_result helper.

---
 rust/kernel/fs/file.rs | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
rust/kernel/fs/file.rs +1 -1
index 35fd5db35c46..924f01bd64c2 100644
@@ -10,7 +10,7 @@
 use crate::{
     bindings,
     cred::Credential,
-    error::{code::*, Error, Result},
+    error::{code::*, to_result, Error, Result},
     types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque},
 };
 use core::ptr;
@@ -398,9 +398,8 @@ impl FileDescriptorReservation {
     pub fn get_unused_fd_flags(flags: u32) -> Result<Self> {

rust/kernel/fs/file.rs impl FileDescriptorReservation { · line 405

Quoted 10 lines
rust/kernel/fs/file.rs +2 -3
         // SAFETY: FFI call, there are no safety requirements on `flags`.
         let fd: i32 = unsafe { bindings::get_unused_fd_flags(flags) };
-        if fd < 0 {
-            return Err(Error::from_errno(fd));
-        }
+        to_result(fd)?;
+
         Ok(Self {
             fd: fd as u32,
             _not_send: NotThreadSafe,

Can be further simplified with:

```rust
to_result(fd).map(|_| Self { fd: fd as u32, // rest... })
```
Quoted 2 lines

--
2.50.0