This constructor is public since commit 5ed147473458 ("rust: error:
make conversion functions public"), and we will refer to it from the
documentation of `to_result` in a later commit.
Thus improve its documentation, including adding examples.
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
rust/kernel/error.rs | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index a41de293dcd1..c415c3d3a3b6 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -101,8 +101,23 @@ macro_rules! declare_err {
impl Error {
/// Creates an [`Error`] from a kernel error code.
///
- /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
- /// be returned in such a case.
+ /// `errno` must be within error code range (i.e. `>= -MAX_ERRNO && < 0`).
+ ///
+ /// It is a bug to pass an out-of-range `errno`. [`code::EINVAL`] is returned in such a case.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// assert_eq!(Error::from_errno(-1), EPERM);
+ /// assert_eq!(Error::from_errno(-2), ENOENT);
+ /// ```
+ ///
+ /// The following calls are considered a bug:
+ ///
+ /// ```
+ /// assert_eq!(Error::from_errno(0), EINVAL);
+ /// assert_eq!(Error::from_errno(-1000000), EINVAL);
+ /// ```
pub fn from_errno(errno: crate::ffi::c_int) -> Error {
if let Some(error) = Self::try_from_errno(errno) {
error
--
2.51.0
On Fri Aug 29, 2025 at 9:22 PM CEST, Miguel Ojeda wrote:
> This constructor is public since commit 5ed147473458 ("rust: error:
> make conversion functions public"), and we will refer to it from the
> documentation of `to_result` in a later commit.
>
> Thus improve its documentation, including adding examples.
>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
---
Cheers,
Benno
> ---
> rust/kernel/error.rs | 19 +++++++++++++++++--
> 1 file changed, 17 insertions(+), 2 deletions(-)
On Sat Aug 30, 2025 at 4:22 AM JST, Miguel Ojeda wrote:
> This constructor is public since commit 5ed147473458 ("rust: error:
> make conversion functions public"), and we will refer to it from the
> documentation of `to_result` in a later commit.
>
> Thus improve its documentation, including adding examples.
>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This reads very smoothly!
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> rust/kernel/error.rs | 19 +++++++++++++++++--
> 1 file changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> index a41de293dcd1..c415c3d3a3b6 100644
> --- a/rust/kernel/error.rs
> +++ b/rust/kernel/error.rs
> @@ -101,8 +101,23 @@ macro_rules! declare_err {
> impl Error {
> /// Creates an [`Error`] from a kernel error code.
> ///
> - /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
> - /// be returned in such a case.
> + /// `errno` must be within error code range (i.e. `>= -MAX_ERRNO && < 0`).
> + ///
> + /// It is a bug to pass an out-of-range `errno`. [`code::EINVAL`] is returned in such a case.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// assert_eq!(Error::from_errno(-1), EPERM);
> + /// assert_eq!(Error::from_errno(-2), ENOENT);
Small nit: this leaves a tiny chance of misunderstanding that this
function actually expects literal negative integers. How about using
e.g. `bindings::EPERM` instead of `-1`?
On Sat, Aug 30, 2025 at 2:18 PM Alexandre Courbot <acourbot@nvidia.com> wrote: > > Small nit: this leaves a tiny chance of misunderstanding that this > function actually expects literal negative integers. How about using > e.g. `bindings::EPERM` instead of `-1`? Sounds good -- or perhaps `EPERM.to_errno()`? Thanks! Cheers, Miguel
On Sat Aug 30, 2025 at 10:00 PM JST, Miguel Ojeda wrote: > On Sat, Aug 30, 2025 at 2:18 PM Alexandre Courbot <acourbot@nvidia.com> wrote: >> >> Small nit: this leaves a tiny chance of misunderstanding that this >> function actually expects literal negative integers. How about using >> e.g. `bindings::EPERM` instead of `-1`? > > Sounds good -- or perhaps `EPERM.to_errno()`? Looking good to me!
© 2016 - 2026 Red Hat, Inc.