pub fn from_err_ptr<T>(ptr: *mut T) -> Result<*mut T>Expand description
Transform a kernel “error pointer” to a normal pointer.
Some kernel C API functions return an “error pointer” which optionally
embeds an errno. Callers are supposed to check the returned pointer
for errors. This function performs the check and converts the “error pointer”
to a normal pointer in an idiomatic fashion.
Note that a NULL pointer is not considered an error pointer, and is returned
as-is, wrapped in Ok.
§Examples
ⓘ
fn devm_platform_ioremap_resource(
pdev: &mut PlatformDevice,
index: u32,
) -> Result<*mut kernel::ffi::c_void> {
// SAFETY: `pdev` points to a valid platform device. There are no safety requirements
// on `index`.
from_err_ptr(unsafe { bindings::devm_platform_ioremap_resource(pdev.to_ptr(), index) })
}// SAFETY: ...
let einval_err = from_err_ptr(unsafe { bindings::einval_err_ptr() });
assert_eq!(einval_err, Err(EINVAL));
// SAFETY: ...
let null_ok = from_err_ptr(unsafe { bindings::null_ptr() });
assert_eq!(null_ok, Ok(core::ptr::null_mut()));
// SAFETY: ...
let non_null = from_err_ptr(unsafe { bindings::non_null_ptr() }).unwrap();
assert_ne!(non_null, core::ptr::null_mut());