Derive Macro rquickjs_macro::IntoJsByRef
source · [−]#[derive(IntoJsByRef)]
{
// Attributes available to this derive:
#[quickjs]
}
Expand description
A macro to derive IntoJs
for an arbitrary structured types when it used by reference
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 = "for<'a> &'a 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::IntoJsByRef;
#[derive(IntoJsByRef)]
struct MyUnit;
Tuple struct
#[derive(IntoJsByRef)]
struct MyTuple(i32, String);
Struct with fields
#[derive(IntoJsByRef)]
struct MyStruct {
int: i32,
text: String,
}
Struct with fields with default values
#[derive(IntoJsByRef)]
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(IntoJsByRef)]
#[quickjs(untagged)]
enum MyEnum {
Foo,
Bar,
}
Untagged unit enum with discriminant
#[derive(IntoJsByRef)]
#[quickjs(untagged)]
#[repr(i32)]
enum MyEnum {
Foo = 1,
Bar = 2,
}
Externally tagged tuple enum
#[derive(IntoJsByRef)]
enum MyEnum {
Foo(f64, f64),
Bar(String),
}
Adjacently tagged tuple enum
#[derive(IntoJsByRef)]
#[quickjs(tag, content = "data")]
enum MyEnum {
Foo(f64, f64),
Bar(String),
}
Untagged tuple enum
#[derive(IntoJsByRef)]
#[quickjs(untagged)]
enum MyEnum {
Foo(f64, f64),
Bar(String),
}
Internally tagged enum with fields
#[derive(IntoJsByRef)]
#[quickjs(tag = "$")]
enum MyEnum {
Foo { x: f64, y: f64 },
Bar { msg: String },
}
Internally tagged enum with fields with defaults
#[derive(IntoJsByRef)]
#[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(IntoJsByRef)]
#[quickjs(untagged)]
enum MyEnum {
Foo { x: f64, y: f64 },
Bar { msg: String },
}
Generic newtype-like struct
#[derive(IntoJsByRef)]
struct Newtype<T>(pub T);
Generic struct with fields
#[derive(IntoJsByRef)]
struct MyStruct<T, V> {
pub tag: T,
pub value: V,
}
Generic enum
#[derive(IntoJsByRef)]
enum MyEnum<T, V> {
Foo(V),
Bar { value: V, tag: T },
}