Skip to content

Commit 7dbb95f

Browse files
author
Sven Van Asbroeck
committed
rust/kernel: move from_kernel_result! macro to error.rs
This macro is well-suited for use elsewhere in the Rust core. Move it out to a suitable place, error.rs. Signed-off-by: Sven Van Asbroeck <[email protected]>
1 parent fc2b177 commit 7dbb95f

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

rust/kernel/error.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use crate::{bindings, c_types};
88
use alloc::{alloc::AllocError, collections::TryReserveError};
9+
use core::convert::TryFrom;
910
use core::{num::TryFromIntError, str::Utf8Error};
1011

1112
/// Generic integer kernel error.
@@ -104,3 +105,23 @@ impl From<AllocError> for Error {
104105
Error::ENOMEM
105106
}
106107
}
108+
109+
pub fn from_kernel_result_helper<T>(r: Result<T>) -> T
110+
where
111+
T: TryFrom<c_types::c_int>,
112+
T::Error: core::fmt::Debug,
113+
{
114+
match r {
115+
Ok(v) => v,
116+
Err(e) => T::try_from(e.to_kernel_errno()).unwrap(),
117+
}
118+
}
119+
120+
#[macro_export]
121+
macro_rules! from_kernel_result {
122+
($($tt:tt)*) => {{
123+
$crate::error::from_kernel_result_helper((|| {
124+
$($tt)*
125+
})())
126+
}};
127+
}

rust/kernel/file_operations.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
bindings, c_types,
1414
error::{Error, Result},
1515
file::File,
16+
from_kernel_result,
1617
io_buffer::{IoBufferReader, IoBufferWriter},
1718
iov_iter::IovIter,
1819
sync::CondVar,
@@ -78,25 +79,6 @@ pub enum SeekFrom {
7879
Current(i64),
7980
}
8081

81-
fn from_kernel_result<T>(r: Result<T>) -> T
82-
where
83-
T: TryFrom<c_types::c_int>,
84-
T::Error: core::fmt::Debug,
85-
{
86-
match r {
87-
Ok(v) => v,
88-
Err(e) => T::try_from(e.to_kernel_errno()).unwrap(),
89-
}
90-
}
91-
92-
macro_rules! from_kernel_result {
93-
($($tt:tt)*) => {{
94-
from_kernel_result((|| {
95-
$($tt)*
96-
})())
97-
}};
98-
}
99-
10082
unsafe extern "C" fn open_callback<A: FileOpenAdapter, T: FileOpener<A::Arg>>(
10183
inode: *mut bindings::inode,
10284
file: *mut bindings::file,

0 commit comments

Comments
 (0)