Skip to content

Support defining getters in Rust #222

Closed
@alexcrichton

Description

@alexcrichton

Currently when you export a struct in Rust to JS it only exports public methods, but we should enable the ability to define getters and setters for properties too, for example:

#[wasm_bindgen]
struct Foo { /* ... */ }

#[wasm_bindgen]
impl Foo {
    pub fn new() -> Foo { ... }

    #[wasm_bindgen(getter)]
    pub fn field1(&self) -> u32 { ... }
    #[wasm_bindgen(setter)]
    pub fn set_field1(&mut self, f: u32) { ... }

    #[wasm_bindgen(getter = anotherName)]
    pub fn another_name(&self) { ... }
    #[wasm_bindgen(setter = anotherName)]
    pub fn set_another_name(&mut self, param: u32) { ... }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions