[v1,11/28] rust: prelude: add `error::code::*` constant items

Message ID 20221110164152.26136-12-ojeda@kernel.org
State New
Headers
Series Rust core additions |

Commit Message

Miguel Ojeda Nov. 10, 2022, 4:41 p.m. UTC
  From: Wedson Almeida Filho <wedsonaf@gmail.com>

It is convenient to have all the `Error` constant items (such as
`EINVAL`) available as-is everywhere (i.e. for code using the kernel
prelude such as kernel modules).

Therefore, add all of them to the prelude.

For instance, this allows to write `Err(EINVAL)` to create
a kernel `Result`:

    fn f() -> Result<...> {
        ...
        Err(EINVAL)
    }

Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/kernel/prelude.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Gary Guo Nov. 14, 2022, 2:32 p.m. UTC | #1
On Thu, 10 Nov 2022 17:41:23 +0100
Miguel Ojeda <ojeda@kernel.org> wrote:

> From: Wedson Almeida Filho <wedsonaf@gmail.com>
> 
> It is convenient to have all the `Error` constant items (such as
> `EINVAL`) available as-is everywhere (i.e. for code using the kernel
> prelude such as kernel modules).
> 
> Therefore, add all of them to the prelude.

Personally, I would prefer to have all error codes as associated
constants of the `kernel::Error` type so the code below would use
`Err(Error::EINVAL)` (more "Rusty" way of writing things). But IIRC
that we settled on the current approach because it's more similar to
existing C code.

> 
> For instance, this allows to write `Err(EINVAL)` to create
> a kernel `Result`:
> 
>     fn f() -> Result<...> {
>         ...
>         Err(EINVAL)
>     }
> 
> Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
> [Reworded, adapted for upstream and applied latest changes]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/prelude.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs
> index 7c4c35bf3c66..1e08b08e9420 100644
> --- a/rust/kernel/prelude.rs
> +++ b/rust/kernel/prelude.rs
> @@ -19,6 +19,6 @@ pub use macros::{module, vtable};
>  
>  pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
>  
> -pub use super::error::{Error, Result};
> +pub use super::error::{code::*, Error, Result};
>  
>  pub use super::ThisModule;
  

Patch

diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs
index 7c4c35bf3c66..1e08b08e9420 100644
--- a/rust/kernel/prelude.rs
+++ b/rust/kernel/prelude.rs
@@ -19,6 +19,6 @@  pub use macros::{module, vtable};
 
 pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
 
-pub use super::error::{Error, Result};
+pub use super::error::{code::*, Error, Result};
 
 pub use super::ThisModule;