Derive Macro rquickjs_macro::IntoJs [−][src]
#[derive(IntoJs)] { // Attributes available to this derive: #[quickjs] }
A macro to derive IntoJs
for an arbitrary structured types
Supported attributes
Macro attributes
Attribute | Description |
---|---|
rename_all = "<rule>" | Renames variants and fields by applying renaming rule ("lowercase", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE", "kebab-case") |
bound = "T: Bound" | Overrides type paremters bounds |
tag , tag = "type" | Turns enum representation to internally tagged |
content , content = "data" | With tag turns enum representation to adjacently tagged |
untagged | Turns enum representation to untagged |
crate = "rquickjs" | Allows rename rquickjs crate |
The default enum
representation is externally tagged.
Variant attributes
Attribute | Description |
---|---|
rename = "new_name" | Renames a variant |
skip | Skips this variant |
If a enum
representation is untagged
the variants with discriminant
will be represented as a numbers.
Field attributes
Attribute | Description |
---|---|
rename = "new_name" | Renames a field |
default , default = "path" | Sets the default for a field |
skip_default | Skip named field when default value is set |
skip | Skips this field |
Examples
Unit struct
use rquickjs::IntoJs; #[derive(IntoJs)] struct MyUnit;
Tuple struct
#[derive(IntoJs)] struct MyTuple(i32, String);
Struct with fields
#[derive(IntoJs)] struct MyStruct { int: i32, text: String, }
Struct with fields with default values
#[derive(IntoJs)] struct MyStruct { #[quickjs(skip_default)] int: i32, #[quickjs(default = "default_text", skip_default)] text: String, } fn default_text() -> String { "hello".into() }
Untagged unit enum
#[derive(IntoJs)] #[quickjs(untagged)] enum MyEnum { Foo, Bar, }
Untagged unit enum with discriminant
#[derive(IntoJs)] #[quickjs(untagged)] #[repr(i32)] enum MyEnum { Foo = 1, Bar = 2, }
Externally tagged tuple enum
#[derive(IntoJs)] enum MyEnum { Foo(f64, f64), Bar(String), }
Adjacently tagged tuple enum
#[derive(IntoJs)] #[quickjs(tag, content = "data")] enum MyEnum { Foo(f64, f64), Bar(String), }
Untagged tuple enum
#[derive(IntoJs)] #[quickjs(untagged)] enum MyEnum { Foo(f64, f64), Bar(String), }
Internally tagged enum with fields
#[derive(IntoJs)] #[quickjs(tag = "$")] enum MyEnum { Foo { x: f64, y: f64 }, Bar { msg: String }, }
Internally tagged enum with fields with defaults
#[derive(IntoJs)] #[quickjs(tag = "$")] enum MyEnum { Foo { x: f64, #[quickjs(skip_default)] y: f64, }, Bar { #[quickjs(default = "default_msg", skip_default)] msg: String, }, } fn default_msg() -> String { "my message".into() }
Untagged enum with fields
#[derive(IntoJs)] #[quickjs(untagged)] enum MyEnum { Foo { x: f64, y: f64 }, Bar { msg: String }, }
Generic newtype-like struct
#[derive(IntoJs)] struct Newtype<T>(pub T);
Generic struct with fields
#[derive(IntoJs)] struct MyStruct<T, V> { pub tag: T, pub value: V, }
Generic enum
#[derive(IntoJs)] enum MyEnum<T, V> { Foo(V), Bar { value: V, tag: T }, }