Skip to contentSkip to conversation

rust: error: allow specifying error type on `Result`

PATCH Applied Alice Ryhl 6 participants 6 messages May 2, 2023 – May 31, 2023 Rust

lore
Share & tools
lore

Alice Ryhl

author reviewer
#0 lore raw

PATCH 1/1 rust: error: allow specifying error type on `Result`

Currently, if the kernel::error::Result type is in scope (which is often is, since it's in the kernel's prelude), you cannot write Result<T, SomeOtherErrorType> when you want to use a different error type than kernel::error::Error.

To solve this we change the error type from being hard-coded to just being a default generic parameter. This still lets you write Result<T> when you just want to use the Error error type, but also lets you write Result<T, SomeOtherErrorType> when necessary.

---
 rust/kernel/error.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/error.rs +1 -1
index 5f4114b30b94..01dd4d2f63d2 100644
@@ -177,7 +177,7 @@ impl From<core::convert::Infallible> for Error {
 /// Note that even if a function does not return anything when it succeeds,
 /// it should still be modeled as returning a `Result` rather than
 /// just an [`Error`].
-pub type Result<T = ()> = core::result::Result<T, Error>;
+pub type Result<T = (), E = Error> = core::result::Result<T, E>;
 
 /// Converts an integer as returned by a C kernel function to an error if it's negative, and
 /// `Ok(())` otherwise.

base-commit: ea76e08f4d901a450619831a255e9e0a4c0ed162

Signature · 1 line
2.40.1.495.gc816e09b53d-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Alice Ryhl

Currently, if the kernel::error::Result type is in scope (which is often is, since it's in the kernel's prelude), you cannot write Result<T, SomeOtherErrorType> when you want to use a different error type than kernel::error::Error.

To solve this we change the error type from being hard-coded to just being a default generic parameter. This still lets you write Result<T> when you just want to use the Error error type, but also lets you write Result<T, SomeOtherErrorType> when necessary.

---
  rust/kernel/error.rs | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/error.rs +1 -1
index 5f4114b30b94..01dd4d2f63d2 100644
@@ -177,7 +177,7 @@ impl From<core::convert::Infallible> for Error {
  /// Note that even if a function does not return anything when it succeeds,
  /// it should still be modeled as returning a `Result` rather than
  /// just an [`Error`].
-pub type Result<T = ()> = core::result::Result<T, Error>;
+pub type Result<T = (), E = Error> = core::result::Result<T, E>;
 
  /// Converts an integer as returned by a C kernel function to an error if it's negative, and
  /// `Ok(())` otherwise.
Quoted 1 line

base-commit: ea76e08f4d901a450619831a255e9e0a4c0ed162

~~ Lina

Reviewed-by on v1 1/1
#2 lore raw
In reply to Alice Ryhl
Quoted 11 lines · Alice Ryhl

Currently, if the kernel::error::Result type is in scope (which is often is, since it's in the kernel's prelude), you cannot write Result<T, SomeOtherErrorType> when you want to use a different error type than kernel::error::Error.

To solve this we change the error type from being hard-coded to just being a default generic parameter. This still lets you write Result<T> when you just want to use the Error error type, but also lets you write Result<T, SomeOtherErrorType> when necessary.

Quoted 22 lines
---
  rust/kernel/error.rs | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/error.rs +1 -1
index 5f4114b30b94..01dd4d2f63d2 100644
@@ -177,7 +177,7 @@ impl From<core::convert::Infallible> for Error {
  /// Note that even if a function does not return anything when it succeeds,
  /// it should still be modeled as returning a `Result` rather than
  /// just an [`Error`].
-pub type Result<T = ()> = core::result::Result<T, Error>;
+pub type Result<T = (), E = Error> = core::result::Result<T, E>;
 
  /// Converts an integer as returned by a C kernel function to an error if it's negative, and
  /// `Ok(())` otherwise.

base-commit: ea76e08f4d901a450619831a255e9e0a4c0ed162 --
2.40.1.495.gc816e09b53d-goog

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Alice Ryhl
Quoted 11 lines · On Tue, 2 May 2023 12:40:15 +0000 Alice Ryhl <[email protected]> wrote:

Currently, if the kernel::error::Result type is in scope (which is often is, since it's in the kernel's prelude), you cannot write Result<T, SomeOtherErrorType> when you want to use a different error type than kernel::error::Error.

To solve this we change the error type from being hard-coded to just being a default generic parameter. This still lets you write Result<T> when you just want to use the Error error type, but also lets you write Result<T, SomeOtherErrorType> when necessary.

Quoted 19 lines
---
 rust/kernel/error.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/error.rs +1 -1
index 5f4114b30b94..01dd4d2f63d2 100644
@@ -177,7 +177,7 @@ impl From<core::convert::Infallible> for Error {
 /// Note that even if a function does not return anything when it succeeds,
 /// it should still be modeled as returning a `Result` rather than
 /// just an [`Error`].
-pub type Result<T = ()> = core::result::Result<T, Error>;
+pub type Result<T = (), E = Error> = core::result::Result<T, E>;
 
 /// Converts an integer as returned by a C kernel function to an error if it's negative, and
 /// `Ok(())` otherwise.

base-commit: ea76e08f4d901a450619831a255e9e0a4c0ed162

Reviewed-by on v1 1/1
#4 lore raw
In reply to Alice Ryhl
Quoted 5 lines · Alice Ryhl

Currently, if the kernel::error::Result type is in scope (which is often is, since it's in the kernel's prelude), you cannot write Result<T, SomeOtherErrorType> when you want to use a different error type than kernel::error::Error.

Quoted 7 lines

To solve this we change the error type from being hard-coded to just being a default generic parameter. This still lets you write Result<T> when you just want to use the Error error type, but also lets you write Result<T, SomeOtherErrorType> when necessary.

---

Quoted 18 lines
 rust/kernel/error.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/error.rs +1 -1
index 5f4114b30b94..01dd4d2f63d2 100644
@@ -177,7 +177,7 @@ impl From<core::convert::Infallible> for Error {
 /// Note that even if a function does not return anything when it succeeds,
 /// it should still be modeled as returning a `Result` rather than
 /// just an [`Error`].
-pub type Result<T = ()> = core::result::Result<T, Error>;
+pub type Result<T = (), E = Error> = core::result::Result<T, E>;
 
 /// Converts an integer as returned by a C kernel function to an error if it's negative, and
 /// `Ok(())` otherwise.

base-commit: ea76e08f4d901a450619831a255e9e0a4c0ed162

Applied
#5 lore raw
In reply to Alice Ryhl
Quoted 6 lines · Alice Ryhl

Currently, if the kernel::error::Result type is in scope (which is often is, since it's in the kernel's prelude), you cannot write Result<T, SomeOtherErrorType> when you want to use a different error type than kernel::error::Error.

Quoted 6 lines

To solve this we change the error type from being hard-coded to just being a default generic parameter. This still lets you write Result<T> when you just want to use the Error error type, but also lets you write Result<T, SomeOtherErrorType> when necessary.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel