Macro neon::declare_types [−][src]
macro_rules! declare_types { { $(#[$attr:meta])* pub class $cls:ident for $typ:ident { $($body:tt)* } $($rest:tt)* } => { ... }; { $(#[$attr:meta])* class $cls:ident for $typ:ident { $($body:tt)* } $($rest:tt)* } => { ... }; { $(#[$attr:meta])* pub class $cls:ident as $cname:ident for $typ:ty { $($body:tt)* } $($rest:tt)* } => { ... }; { $(#[$attr:meta])* class $cls:ident as $cname:ident for $typ:ty { $($body:tt)* } $($rest:tt)* } => { ... }; { } => { ... }; }
Declare custom native JavaScript types with Rust implementations.
Example:
pub struct Greeter { greeting: String } declare_types! { /// A class for generating greeting strings. pub class JsGreeter for Greeter { init(mut cx) { let greeting = cx.argument::<JsString>(0)?.to_string(&mut cx)?.value(); Ok(Greeter { greeting: greeting }) } method hello(mut cx) { let name = cx.argument::<JsString>(0)?.to_string(&mut cx)?.value(); let this = cx.this(); let msg = { let guard = cx.lock(); let greeter = this.borrow(&guard); format!("{}, {}!", greeter.greeting, name) }; Ok(cx.string(&msg[..]).upcast()) } } }