Skip to content

Commit

Permalink
rust: test serde support
Browse files Browse the repository at this point in the history
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 <ojeda@kernel.org>
  • Loading branch information
ojeda committed May 8, 2023
1 parent 9121b11 commit 7d2ea51
Show file tree
Hide file tree
Showing 12 changed files with 2,035 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
// instead of `!`. See <https://github.com/rust-lang/rust-bindgen/issues/2094>.
loop {}
}

pub mod test_serde;
26 changes: 26 additions & 0 deletions rust/kernel/test_serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Test `serde`.
//!
//! It contains a data format used by the `rust_serde` sample, as well
//! as a quick check that `serde_derive` works in the `kernel` crate too.
#![allow(missing_docs)]

mod de;
mod error;
mod ser;

pub use de::{from_bytes, Deserializer};
pub use error::{Error, Result};
pub use ser::{to_vec, Serializer};

use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct S {
a: (),
b: bool,
c: bool,
d: (),
}
Loading

0 comments on commit 7d2ea51

Please sign in to comment.