From 1509e7d389edfba73a1d27763a116014f48516ad Mon Sep 17 00:00:00 2001 From: Mikkel ALMONTE--RINGAUD Date: Mon, 30 Dec 2024 23:10:43 +0100 Subject: [PATCH] docs: `export` method --- wasm/examples/export.rs | 40 ++++++++++++++++++++++++++++++++++++++++ wasm/src/lib.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 wasm/examples/export.rs diff --git a/wasm/examples/export.rs b/wasm/examples/export.rs new file mode 100644 index 0000000..e9a1210 --- /dev/null +++ b/wasm/examples/export.rs @@ -0,0 +1,40 @@ +// the method will be renamed `retrieveCas` in the generated bindings +#[wasm::export] +pub fn retrieve_cas() { +} + +// the method will be still called `update` in the generated bindings +#[wasm::export] +pub fn update() { +} + +#[wasm::export] +pub struct Session { + instance_url: String, + php_sess_id: String, +} + +#[wasm::export] +impl Session { + #[wasm_bindgen(constructor)] + pub fn new(instance_url: String, php_sess_id: String) -> Self { + Self { + instance_url, + php_sess_id, + } + } + + #[wasm_bindgen(getter)] + pub fn instance_url(&self) -> String { + self.instance_url.clone() + } + + #[wasm_bindgen(getter)] + pub fn php_sess_id(&self) -> String { + self.php_sess_id.clone() + } +} + +fn main() { + println!("Hello, world!"); +} diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index ee877e1..e193405 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -10,7 +10,7 @@ use quote::quote; /// If applied to a function, it will also rename the /// name to camel case using `js_name`. /// -/// # Usages +/// ## Usages /// /// ```rust /// // The method will be called `retrieveCas` in the generated bindings. @@ -28,6 +28,31 @@ use quote::quote; /// impl Session { /// // ... /// } +/// ``` +/// +/// ## Notes +/// +/// In `impl`s, you can use the `#[wasm_bindgen]` attribute +/// wherever you want since the one on the `impl` itself +/// defines it directly. +/// +/// ```rust +/// #[wasm::export] +/// impl Session { +/// #[wasm_bindgen(constructor)] +/// pub fn new () -> Self { +/// // ... +/// } +/// +/// #[wasm_bindgen(getter = instanceUrl)] +/// pub fn instance_url(&self) -> String { +/// // ... +/// } +/// } +/// ``` +/// +/// Sadly, we can't override those to make `wasm::getter` and +/// `wasm::constructor` exist, but anyway, it's not a big deal. /// #[proc_macro_attribute] pub fn export(_args: TokenStream, input: TokenStream) -> TokenStream {