Skip to content

Commit 0d59baa

Browse files
asahilinaojeda
authored andcommitted
rust: error: Add Error::to_ptr()
This is the Rust equivalent to ERR_PTR(), for use in C callbacks. Marked as #[allow(dead_code)] for now, since it does not have any consumers yet. Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Signed-off-by: Asahi Lina <[email protected]> Reviewed-by: Gary Guo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 72080fe commit 0d59baa

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

rust/helpers.c

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <linux/bug.h>
2222
#include <linux/build_bug.h>
23+
#include <linux/err.h>
2324
#include <linux/refcount.h>
2425

2526
__noreturn void rust_helper_BUG(void)
@@ -46,6 +47,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r)
4647
}
4748
EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
4849

50+
__force void *rust_helper_ERR_PTR(long err)
51+
{
52+
return ERR_PTR(err);
53+
}
54+
EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
55+
4956
/*
5057
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
5158
* as the Rust `usize` type, so we can use it in contexts where Rust

rust/kernel/error.rs

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ impl Error {
7676
pub fn to_errno(self) -> core::ffi::c_int {
7777
self.0
7878
}
79+
80+
/// Returns the error encoded as a pointer.
81+
#[allow(dead_code)]
82+
pub(crate) fn to_ptr<T>(self) -> *mut T {
83+
// SAFETY: self.0 is a valid error due to its invariant.
84+
unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ }
85+
}
7986
}
8087

8188
impl From<AllocError> for Error {

0 commit comments

Comments
 (0)