| b9ecf9b9 | 10-Nov-2022 |
Wedson Almeida Filho <[email protected]> |
rust: types: add `Opaque` type
Add the `Opaque` type, which is meant to be used with FFI objects that are never interpreted by Rust code, e.g.:
struct Waiter { completion: Opaque<bindin
rust: types: add `Opaque` type
Add the `Opaque` type, which is meant to be used with FFI objects that are never interpreted by Rust code, e.g.:
struct Waiter { completion: Opaque<bindings::completion>, next: *mut Waiter, }
It has the advantage that the objects don't have to be zero-initialised before calling their init functions, making the code performance closer to C.
Signed-off-by: Wedson Almeida Filho <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| ba20915b | 10-Nov-2022 |
Wedson Almeida Filho <[email protected]> |
rust: types: add `Either` type
Introduce the new `types` module of the `kernel` crate with `Either` as its first type.
`Either<L, R>` is a sum type that always holds either a value of type `L` (`Le
rust: types: add `Either` type
Introduce the new `types` module of the `kernel` crate with `Either` as its first type.
`Either<L, R>` is a sum type that always holds either a value of type `L` (`Left` variant) or `R` (`Right` variant).
For instance:
struct Executor { queue: Either<BoxedQueue, &'static Queue>, }
Signed-off-by: Wedson Almeida Filho <[email protected]> Reviewed-by: Wei Liu <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 0f595bab | 10-Nov-2022 |
Gary Guo <[email protected]> |
rust: build_assert: add `build_{error,assert}!` macros
Add the `build_error!` and `build_assert!` macros which leverage the previously introduced `build_error` crate. Do so in a new module, called `
rust: build_assert: add `build_{error,assert}!` macros
Add the `build_error!` and `build_assert!` macros which leverage the previously introduced `build_error` crate. Do so in a new module, called `build_assert`.
The former fails the build if the code path calling it can possibly be executed. The latter asserts that a boolean expression is `true` at compile time.
In particular, `build_assert!` can be used in some contexts where `static_assert!` cannot:
fn f1<const N: usize>() { static_assert!(N > 1);` // Error. build_assert!(N > 1); // Build-time check. assert!(N > 1); // Run-time check. }
#[inline] fn f2(n: usize) { static_assert!(n > 1); // Error. build_assert!(n > 1); // Build-time check. assert!(n > 1); // Run-time check. }
Signed-off-by: Gary Guo <[email protected]> Reviewed-by: Wei Liu <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| ef9e3797 | 10-Nov-2022 |
Miguel Ojeda <[email protected]> |
rust: static_assert: add `static_assert!` macro
Add the `static_assert!` macro, which is a compile-time assert, similar to the C11 `_Static_assert` and C++11 `static_assert` declarations [1,2]. Do s
rust: static_assert: add `static_assert!` macro
Add the `static_assert!` macro, which is a compile-time assert, similar to the C11 `_Static_assert` and C++11 `static_assert` declarations [1,2]. Do so in a new module, called `static_assert`.
For instance:
static_assert!(42 > 24); static_assert!(core::mem::size_of::<u8>() == 1);
const X: &[u8] = b"bar"; static_assert!(X[1] == b'a');
const fn f(x: i32) -> i32 { x + 2 } static_assert!(f(40) == 42);
Link: https://en.cppreference.com/w/c/language/_Static_assert [1] Link: https://en.cppreference.com/w/cpp/language/static_assert [2] Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| bee16889 | 10-Nov-2022 |
Niklas Mohrin <[email protected]> |
rust: std_vendor: add `dbg!` macro based on `std`'s one
The Rust standard library has a really handy macro, `dbg!` [1,2]. It prints the source location (filename and line) along with the raw source
rust: std_vendor: add `dbg!` macro based on `std`'s one
The Rust standard library has a really handy macro, `dbg!` [1,2]. It prints the source location (filename and line) along with the raw source code that is invoked with and the `Debug` representation of the given expression, e.g.:
let a = 2; let b = dbg!(a * 2) + 1; // ^-- prints: [src/main.rs:2] a * 2 = 4 assert_eq!(b, 5);
Port the macro over to the `kernel` crate inside a new module called `std_vendor`, using `pr_info!` instead of `eprintln!` and make the rules about committing uses of `dbg!` into version control more concrete (i.e. tailored for the kernel).
Since the source code for the macro is taken from the standard library source (with only minor adjustments), the new file is licensed under `Apache 2.0 OR MIT`, just like the original [3,4].
Link: https://doc.rust-lang.org/std/macro.dbg.html [1] Link: https://github.com/rust-lang/rust/blob/master/library/std/src/macros.rs#L212 [2] Link: https://github.com/rust-lang/rust/blob/master/library/std/Cargo.toml [3] Link: https://github.com/rust-lang/rust/blob/master/COPYRIGHT [4] Signed-off-by: Niklas Mohrin <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| ef320549 | 10-Nov-2022 |
Wedson Almeida Filho <[email protected]> |
rust: str: add `fmt!` macro
Add the `fmt!` macro, which is a convenience alias for the Rust `core::format_args!` macro.
For instance, it may be used to create a `CString`:
CString::try_from_fm
rust: str: add `fmt!` macro
Add the `fmt!` macro, which is a convenience alias for the Rust `core::format_args!` macro.
For instance, it may be used to create a `CString`:
CString::try_from_fmt(fmt!("{}{}", "abc", 42))?
Signed-off-by: Wedson Almeida Filho <[email protected]> Reviewed-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 65e1e497 | 10-Nov-2022 |
Wedson Almeida Filho <[email protected]> |
rust: str: add `CString` type
Add the `CString` type, which is an owned string that is guaranteed to have exactly one `NUL` byte at the end, i.e. the owned equivalent to `CStr` introduced earlier.
rust: str: add `CString` type
Add the `CString` type, which is an owned string that is guaranteed to have exactly one `NUL` byte at the end, i.e. the owned equivalent to `CStr` introduced earlier.
It is used for interoperability with kernel APIs that take C strings.
In order to do so, implement the `RawFormatter::new()` constructor and the `RawFormatter::bytes_written()` method as well.
Signed-off-by: Wedson Almeida Filho <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| fffed679 | 10-Nov-2022 |
Wedson Almeida Filho <[email protected]> |
rust: str: add `Formatter` type
Add the `Formatter` type, which leverages `RawFormatter`, but fails if callers attempt to write more than will fit in the buffer.
In order to so, implement the `RawF
rust: str: add `Formatter` type
Add the `Formatter` type, which leverages `RawFormatter`, but fails if callers attempt to write more than will fit in the buffer.
In order to so, implement the `RawFormatter::from_buffer()` constructor as well.
Co-developed-by: Adam Bratschi-Kaye <[email protected]> Signed-off-by: Adam Bratschi-Kaye <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Reviewed-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| b18cb00e | 10-Nov-2022 |
Gary Guo <[email protected]> |
rust: str: add `c_str!` macro
Add `c_str!`, which is a convenience macro that creates a new `CStr` from a string literal.
It is designed to be similar to a `str` in usage, and it is usable in const
rust: str: add `c_str!` macro
Add `c_str!`, which is a convenience macro that creates a new `CStr` from a string literal.
It is designed to be similar to a `str` in usage, and it is usable in const contexts, for instance:
const X: &CStr = c_str!("Example");
Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Signed-off-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 985f1f09 | 10-Nov-2022 |
Milan Landaverde <[email protected]> |
rust: str: add `CStr` unit tests
Add unit tests for `CStr::from_bytes_with_nul()` and `CStr::from_bytes_with_nul_unchecked()`.
These serve as an example of the first unit tests for Rust code (i.e.
rust: str: add `CStr` unit tests
Add unit tests for `CStr::from_bytes_with_nul()` and `CStr::from_bytes_with_nul_unchecked()`.
These serve as an example of the first unit tests for Rust code (i.e. different from documentation tests).
Signed-off-by: Milan Landaverde <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| c07e67bd | 10-Nov-2022 |
Gary Guo <[email protected]> |
rust: str: implement several traits for `CStr`
Implement `Debug`, `Display`, `Deref` (into `BStr`), `AsRef<BStr>` and a set of `Index<...>` traits.
This makes it `CStr` more convenient to use (and
rust: str: implement several traits for `CStr`
Implement `Debug`, `Display`, `Deref` (into `BStr`), `AsRef<BStr>` and a set of `Index<...>` traits.
This makes it `CStr` more convenient to use (and closer to `str`).
Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Co-developed-by: Morgan Bartlett <[email protected]> Signed-off-by: Morgan Bartlett <[email protected]> Signed-off-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| d126d238 | 10-Nov-2022 |
Gary Guo <[email protected]> |
rust: str: add `CStr` type
Add the `CStr` type, which is a borrowed string that is guaranteed to have exactly one `NUL` byte, which is at the end.
It is used for interoperability with kernel APIs t
rust: str: add `CStr` type
Add the `CStr` type, which is a borrowed string that is guaranteed to have exactly one `NUL` byte, which is at the end.
It is used for interoperability with kernel APIs that take C strings.
Add it to the prelude too.
Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Co-developed-by: Milan Landaverde <[email protected]> Signed-off-by: Milan Landaverde <[email protected]> Signed-off-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 650ec515 | 10-Nov-2022 |
Gary Guo <[email protected]> |
rust: str: add `b_str!` macro
Add the `b_str!` macro, which creates a new `BStr` from a string literal.
It is usable in const contexts, for instance:
const X: &BStr = b_str!("Example");
Signe
rust: str: add `b_str!` macro
Add the `b_str!` macro, which creates a new `BStr` from a string literal.
It is usable in const contexts, for instance:
const X: &BStr = b_str!("Example");
Signed-off-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 7c597746 | 10-Nov-2022 |
Gary Guo <[email protected]> |
rust: str: add `BStr` type
Add the `BStr` type, which is a byte string without UTF-8 validity guarantee.
It is simply an alias to `[u8]`, but has a more evident semantical meaning.
Signed-off-by:
rust: str: add `BStr` type
Add the `BStr` type, which is a byte string without UTF-8 validity guarantee.
It is simply an alias to `[u8]`, but has a more evident semantical meaning.
Signed-off-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 25d176a4 | 10-Nov-2022 |
Wedson Almeida Filho <[email protected]> |
rust: prelude: add `error::code::*` constant items
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
rust: prelude: add `error::code::*` constant items
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 <[email protected]> Reviewed-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 76e2c2d9 | 10-Nov-2022 |
Wedson Almeida Filho <[email protected]> |
rust: error: add `From` implementations for `Error`
Add a set of `From` implementations for the `Error` kernel type.
These implementations allow to easily convert from standard Rust error types to
rust: error: add `From` implementations for `Error`
Add a set of `From` implementations for the `Error` kernel type.
These implementations allow to easily convert from standard Rust error types to the usual kernel errors based on one of the `E*` integer codes.
On top of that, the question mark Rust operator (`?`) implicitly performs a conversion on the error value using the `From` trait when propagating. Thus it is extra convenient to use.
For instance, a kernel function that needs to convert a `i64` into a `i32` and to bubble up the error as a kernel error may write:
fn f(x: i64) -> Result<...> { ... let y = i32::try_from(x)?; ... }
which will transform the `TryFromIntError` into an `Err(EINVAL)`.
Co-developed-by: Adam Bratschi-Kaye <[email protected]> Signed-off-by: Adam Bratschi-Kaye <[email protected]> Co-developed-by: Nándor István Krácser <[email protected]> Signed-off-by: Nándor István Krácser <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Reviewed-by: Finn Behrens <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 266def2a | 10-Nov-2022 |
Viktor Garske <[email protected]> |
rust: error: add codes from `errno-base.h`
Only a few codes were added so far. With the `declare_err!` macro in place, add the remaining ones (which is most of them) from `include/uapi/asm-generic/e
rust: error: add codes from `errno-base.h`
Only a few codes were added so far. With the `declare_err!` macro in place, add the remaining ones (which is most of them) from `include/uapi/asm-generic/errno-base.h`.
Co-developed-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Viktor Garske <[email protected]> Reviewed-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 4b0c68bd | 10-Nov-2022 |
Finn Behrens <[email protected]> |
rust: error: declare errors using macro
Add a macro to declare errors, which simplifies the work needed to add each one, avoids repetition of the code and makes it easier to change the way they are
rust: error: declare errors using macro
Add a macro to declare errors, which simplifies the work needed to add each one, avoids repetition of the code and makes it easier to change the way they are declared.
Signed-off-by: Finn Behrens <[email protected]> Reviewed-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| b44becc5 | 10-Nov-2022 |
Gary Guo <[email protected]> |
rust: macros: add `#[vtable]` proc macro
This procedural macro attribute provides a simple way to declare a trait with a set of operations that later users can partially implement, providing compile
rust: macros: add `#[vtable]` proc macro
This procedural macro attribute provides a simple way to declare a trait with a set of operations that later users can partially implement, providing compile-time `HAS_*` boolean associated constants that indicate whether a particular operation was overridden.
This is useful as the Rust counterpart to structs like `file_operations` where some pointers may be `NULL`, indicating an operation is not provided.
For instance:
#[vtable] trait Operations { fn read(...) -> Result<usize> { Err(EINVAL) }
fn write(...) -> Result<usize> { Err(EINVAL) } }
#[vtable] impl Operations for S { fn read(...) -> Result<usize> { ... } }
assert_eq!(<S as Operations>::HAS_READ, true); assert_eq!(<S as Operations>::HAS_WRITE, false);
Signed-off-by: Gary Guo <[email protected]> Reviewed-by: Sergio González Collado <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|
| 4c7f9499 | 10-Nov-2022 |
Miguel Ojeda <[email protected]> |
rust: print: add more `pr_*!` levels
Currently, only `pr_info!` (for the minimal sample) and `pr_emerg!` (for the panic handler) are there.
Add the other levels as new macros, i.e. `pr_alert!`, `pr
rust: print: add more `pr_*!` levels
Currently, only `pr_info!` (for the minimal sample) and `pr_emerg!` (for the panic handler) are there.
Add the other levels as new macros, i.e. `pr_alert!`, `pr_crit!`, `pr_err!`, `pr_warn!`, `pr_notice!` and `pr_debug!`.
Co-developed-by: Adam Bratschi-Kaye <[email protected]> Signed-off-by: Adam Bratschi-Kaye <[email protected]> Co-developed-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Gary Guo <[email protected]> Signed-off-by: Gary Guo <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Reviewed-by: Wei Liu <[email protected]> Reviewed-by: Sergio Gonzalez Collado <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
show more ...
|