Skip to content

Commit

Permalink
Include a serde no-std example (relates #148)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed May 17, 2024
1 parent 8cef5ca commit 6ea7b2f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ jobs:
fail-fast: false
matrix:
os: ['windows-latest', 'ubuntu-latest']
example: ['json']
example: ['json', 'serde']
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- run: cargo build
- run: cargo +nightly run -p no-std --example ${{matrix.example}}

fuzz:
Expand Down
3 changes: 2 additions & 1 deletion no-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ edition = "2021"
publish = false

[dependencies]
musli = { path = "../crates/musli", default-features = false, features = ["json", "parse-full"] }
musli = { path = "../crates/musli", default-features = false, features = ["json", "parse-full", "serde"] }
serde = { version = "1.0.202", default-features = false, features = ["derive"] }

[target.'cfg(unix)'.dependencies]
compiler_builtins = { git = "https://github.com/rust-lang/compiler-builtins", features = ["mem"] }
30 changes: 3 additions & 27 deletions no-std/examples/json.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
#![no_std]
#![allow(internal_features)]
#![feature(alloc_error_handler, start, core_intrinsics, lang_items, link_cfg)]
#![feature(start, core_intrinsics, lang_items, link_cfg)]

mod prelude;

use musli::allocator::{Stack, StackBuffer};
use musli::context::StackContext;
use musli::{Decode, Encode};

#[cfg(all(windows, target_env = "msvc"))]
#[link(name = "msvcrt")]
extern "C" {}

#[cfg(unix)]
#[link(name = "c")]
extern "C" {}

#[alloc_error_handler]
fn err_handler(_: core::alloc::Layout) -> ! {
core::intrinsics::abort();
}

#[panic_handler]
#[lang = "panic_impl"]
fn rust_begin_panic(_: &core::panic::PanicInfo) -> ! {
core::intrinsics::abort();
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[cfg(unix)]
#[no_mangle]
pub extern "C" fn _Unwind_Resume() {}

#[derive(Debug, Encode, Decode)]
struct Value<'a> {
name: &'a str,
Expand Down
23 changes: 23 additions & 0 deletions no-std/examples/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Helper module to set up everything you need in a no-std environment withotu
//! alloc support.
#[cfg(all(windows, target_env = "msvc"))]
#[link(name = "msvcrt")]
extern "C" {}

#[cfg(unix)]
#[link(name = "c")]
extern "C" {}

#[panic_handler]
#[lang = "panic_impl"]
fn rust_begin_panic(_: &core::panic::PanicInfo) -> ! {
core::intrinsics::abort();
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[cfg(unix)]
#[no_mangle]
pub extern "C" fn _Unwind_Resume() {}
74 changes: 74 additions & 0 deletions no-std/examples/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![no_std]
#![allow(internal_features)]
#![feature(start, core_intrinsics, lang_items, link_cfg)]

mod prelude;

use musli::allocator::{Stack, StackBuffer};
use musli::context::StackContext;
use musli::{Decode, Encode};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Serde {
field: u32,
}

#[derive(Debug, Encode, Decode)]
struct Value<'a> {
name: &'a str,
age: u32,
#[musli(with = musli::serde)]
serde: Serde,
}

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let mut buf = StackBuffer::<1024>::new();
let alloc = Stack::new(&mut buf);
let cx = StackContext::new(&alloc);

let encoding = musli::json::Encoding::new();

let mut buf = [0u8; 1024];

let value = Value {
name: "Aristotle",
age: 61,
serde: Serde { field: 42 },
};

let mut w = &mut buf[..];

let Ok(..) = encoding.encode_with(&cx, &mut w, &value) else {
for _error in cx.errors() {
// report error
}

return 1;
};

let written = 1024 - w.len();

let Ok(value): Result<Value, _> = encoding.from_slice_with(&cx, &buf[..written]) else {
for _error in cx.errors() {
// report error
}

return 2;
};

if value.name != "Aristotle" {
return 3;
}

if value.age != 61 {
return 4;
}

if value.serde.field != 42 {
return 5;
}

0
}

0 comments on commit 6ea7b2f

Please sign in to comment.