Skip to content

Commit 9aa35e7

Browse files
committed
rust: test serde support
A trivial example based on `serde`'s `example-format' [1]. It contains both a in-`kernel` data format later used by the kernel module, as well as a local data format in the module. The kernel module gives an output such as: [ 0.801425] rust_serde: Rust serde sample (init) [ 0.801634] rust_serde: original = S { a: (), b: false, c: true, d: () } [ 0.802079] rust_serde: serialized = [2, 0, 1, 0, 1, 1, 0, 3] [ 0.802506] rust_serde: deserialized = S { a: (), b: false, c: true, d: () } [ 0.802718] rust_serde: serialized (local) = [2, 0, 1, 42, 1, 43, 0, 3] [ 0.802895] rust_serde: deserialized (local) = S { a: (), b: false, c: true, d: () } [ 0.808954] rust_serde: Rust serde sample (exit) Note that this is just a quick draft/hack to check the previous commits work. It is not intended to be merged at all. Link: https://github.com/serde-rs/example-format [1] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 0ebc567 commit 9aa35e7

File tree

12 files changed

+1989
-1
lines changed

12 files changed

+1989
-1
lines changed

rust/kernel/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl From<core::convert::Infallible> for Error {
177177
/// Note that even if a function does not return anything when it succeeds,
178178
/// it should still be modeled as returning a `Result` rather than
179179
/// just an [`Error`].
180-
pub type Result<T = ()> = core::result::Result<T, Error>;
180+
pub type Result<T = (), E = Error> = core::result::Result<T, E>;
181181

182182
/// Converts an integer as returned by a C kernel function to an error if it's negative, and
183183
/// `Ok(())` otherwise.

rust/kernel/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
101101
// instead of `!`. See <https://github.com/rust-lang/rust-bindgen/issues/2094>.
102102
loop {}
103103
}
104+
105+
pub mod test_serde;

rust/kernel/test_serde.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
3+
//! Test `serde`.
4+
//!
5+
//! It contains a data format used by the `rust_serde` sample, as well
6+
//! as a quick check that `serde_derive` works in the `kernel` crate too.
7+
8+
#![allow(missing_docs)]
9+
10+
mod de;
11+
mod error;
12+
mod ser;
13+
14+
pub use de::{from_bytes, Deserializer};
15+
pub use error::{Error, Result};
16+
pub use ser::{to_vec, Serializer};
17+
18+
use serde_derive::{Deserialize, Serialize};
19+
20+
#[derive(Serialize, Deserialize, Debug)]
21+
pub struct S {
22+
a: (),
23+
b: bool,
24+
c: bool,
25+
d: (),
26+
}

0 commit comments

Comments
 (0)