* [PATCH] rust: file: use to_result for error handling
@ 2025-08-21 9:10 Onur Özkan
2025-08-22 10:34 ` Christian Brauner
2025-08-25 22:54 ` Elle Rhumsaa
0 siblings, 2 replies; 3+ messages in thread
From: Onur Özkan @ 2025-08-21 9:10 UTC (permalink / raw)
To: rust-for-linux
Cc: viro, brauner, jack, ojeda, alex.gaynor, boqun.feng, gary,
bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
linux-fsdevel, linux-kernel, Onur Özkan
Simplifies error handling by replacing the manual check
of the return value with the `to_result` helper.
Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
rust/kernel/fs/file.rs | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs
index 35fd5db35c46..924f01bd64c2 100644
--- a/rust/kernel/fs/file.rs
+++ b/rust/kernel/fs/file.rs
@@ -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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] rust: file: use to_result for error handling
2025-08-21 9:10 [PATCH] rust: file: use to_result for error handling Onur Özkan
@ 2025-08-22 10:34 ` Christian Brauner
2025-08-25 22:54 ` Elle Rhumsaa
1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2025-08-22 10:34 UTC (permalink / raw)
To: Onur Özkan
Cc: rust-for-linux, viro, jack, ojeda, alex.gaynor, boqun.feng, gary,
bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
linux-fsdevel, linux-kernel
On Thu, Aug 21, 2025 at 12:10:01PM +0300, Onur Özkan wrote:
> Simplifies error handling by replacing the manual check
> of the return value with the `to_result` helper.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> ---
Applied, thanks!
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] rust: file: use to_result for error handling
2025-08-21 9:10 [PATCH] rust: file: use to_result for error handling Onur Özkan
2025-08-22 10:34 ` Christian Brauner
@ 2025-08-25 22:54 ` Elle Rhumsaa
1 sibling, 0 replies; 3+ messages in thread
From: Elle Rhumsaa @ 2025-08-25 22:54 UTC (permalink / raw)
To: Onur Özkan
Cc: rust-for-linux, viro, brauner, jack, ojeda, alex.gaynor,
boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
tmgross, dakr, linux-fsdevel, linux-kernel
On Thu, Aug 21, 2025 at 12:10:01PM +0300, Onur Özkan wrote:
> Simplifies error handling by replacing the manual check
> of the return value with the `to_result` helper.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> ---
> rust/kernel/fs/file.rs | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs
> index 35fd5db35c46..924f01bd64c2 100644
> --- a/rust/kernel/fs/file.rs
> +++ b/rust/kernel/fs/file.rs
> @@ -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,
Can be further simplified with:
```rust
to_result(fd).map(|_| Self { fd: fd as u32, // rest... })
```
> --
> 2.50.0
Reviewed-by: Elle Rhumsaa <elle@weathered-steel.dev>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-08-25 22:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-21 9:10 [PATCH] rust: file: use to_result for error handling Onur Özkan
2025-08-22 10:34 ` Christian Brauner
2025-08-25 22:54 ` Elle Rhumsaa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).