Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for methods ? #40

Open
Vexcited opened this issue Jul 28, 2024 · 3 comments
Open

Support for methods ? #40

Vexcited opened this issue Jul 28, 2024 · 3 comments

Comments

@Vexcited
Copy link

Exactly the same issue as in madonoharu#39. Will this fork support it ?

@siefkenj
Copy link
Owner

I believe wasm_bindgen exports types for struct methods already. Is there a reason you don't use its types?

@Vexcited
Copy link
Author

I find tsify exporting types better than wasm_bindgen, for example in the following rust code :

use crate::models::PaymentMethod;
use serde::{Serialize, Deserialize, Deserializer};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

pub fn parse_payment_methods<'de, D>(deserializer: D) -> Result<Vec<PaymentMethod>, D::Error>
where
  D: Deserializer<'de>,
{
  let s = Vec::<serde_json::Value>::deserialize(deserializer)?;
  Ok(
    s.iter()
      .map(|v| PaymentMethod::from_api(v["name"].as_str().unwrap()))
      .collect(),
  )
}

#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter_with_clone))]
pub struct Restaurant {
  // ...
  #[serde(
    rename(deserialize = "payment"),
    deserialize_with = "parse_payment_methods"
  )]
  pub payment_methods: Vec<PaymentMethod>,
  // ...
}

I get this .d.ts output :

export class Restaurant {
  // ...
  payment_methods: any[];
  // ...
}

Every other properties are correct except this one and there's no reason why. I tried to run the exact same code with tsify and it outputted the type correctly, except all the methods got stripped away.

Also I like the fact that you can console.log(any_object_from_rust) directly and it shows you the object properties instead of only the class name.

@mplanchard
Copy link

I believe wasm_bindgen exports types for struct methods already. Is there a reason you don't use its types?

It does, usually, but it does not for structs that are tsified. For example, the following pure wasm_bindgen approach leads to what you'd expect:

#[wasm_bindgen]
pub struct Foo {}
#[wasm_bindgen]
impl Foo {
    pub fn foo(&self) -> JsString {
        todo!()
    }
}
export class Foo {
  free(): void;
  /**
   * @returns {string}
   */
  bar(): string;
}

But the equivalent using Tsify does not:

#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Bar {}
#[wasm_bindgen]
impl Bar {
    pub fn bar(&self) -> JsString {
        todo!()
    }
}
export interface Bar {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants