Module plonk_wasm::wasm_ocaml_serde
source · Expand description
This module constructs a serde serializer (and deserializer) to convert Rust structures to (and from) Js types expected by js-of-ocaml. js-of-ocaml expects arrays of values instead of objects, so a Rust structure like:
ⓘ
{ a: F, b: Vec<F>, c: SomeType }
must be converted to an array that looks like this:
ⓘ
// notice the leading 0, which is an artifact of OCaml's memory layout and how js-of-ocaml is implemented.
[0, a, b, c]
See the following example on how to use it:
#[derive(serde::Serialize, serde::Deserialize)]
struct Thing { a: usize, b: u32 }
let serializer = crate::wasm_ocaml_serde::ser::Serializer::new();
let thing = Thing { a: 5, b: 6 };
let js_value = serde::Serialize::serialize(thing, &mut serializer).unwrap();
assert_eq!(format!("{}", js_value), "[0, 5, 6]");
Modules
Structs
- A newtype that represents Serde errors as JavaScript exceptions.