#[derive(FromWXF)]
{
// Attributes available to this derive:
#[wolfram]
}
Expand description
Derive FromWXF for a struct or enum.
The lifetime parameter 'de is the input buffer lifetime. Owned types
(no reference fields) work for any 'de; structs with &'de str or
&'de [u8] fields borrow zero-copy from the input buffer.
§Structs
Named-field structs decode from a WL Association. Missing Option<T>
fields default to None; all other fields must be present.
use wolfram_serialize::{ToWXF, FromWXF, to_wxf, from_wxf};
#[derive(ToWXF, FromWXF, PartialEq, Debug)]
struct Point {
x: f64,
y: f64,
}
let bytes = to_wxf(&Point { x: 1.0, y: 2.0 }, None).unwrap();
let p: Point = from_wxf(&bytes).unwrap();
assert_eq!(p, Point { x: 1.0, y: 2.0 });§Zero-copy borrowed fields
Struct fields of type &'de str or &'de [u8] borrow directly from the
input buffer — no heap allocation for the string data. Because the borrow
is tied to the input, read them inside a read_wxf closure rather than
returning them:
use wolfram_serialize::{ToWXF, FromWXF, to_wxf, read_wxf};
#[derive(ToWXF)]
struct Owned { name: String }
#[derive(FromWXF)]
struct Borrowed<'a> { name: &'a str }
let bytes = to_wxf(&Owned { name: "hello".into() }, None).unwrap();
read_wxf(&bytes, |r| {
let b = Borrowed::from_wxf(r)?;
assert_eq!(b.name, "hello"); // points into `bytes`, no alloc
Ok(())
}).unwrap();§Enums
Enums decode from <|"Enum" -> "VariantName", "Data" -> {…}|> (the same
shape ToWXF emits):
use wolfram_serialize::{ToWXF, FromWXF, to_wxf, from_wxf};
#[derive(ToWXF, FromWXF, PartialEq, Debug)]
enum Status {
Ok,
Err(String),
}
let bytes = to_wxf(&Status::Err("oops".into()), None).unwrap();
let s: Status = from_wxf(&bytes).unwrap();
assert_eq!(s, Status::Err("oops".into()));§Numeric widening
Integer and real fields accept wider WXF types: an i32 field will accept
a WXF Integer64, and an f32 field will accept a WXF Real64.