0

I've got some core functionality, primarily an algorithm that i'm trying to reuse in web assembly and java.

I'm using wasm-bindgen and serde, I notice that wasm-bindgen and serde is tightly coupled to my algorithm via the use of attributes. i.e; #[wasm_bindgen] and #[derive(Serialize, Deserialize)] -- i'm quite new to rust so i'm wondering how I can decouple these attributes from my function and struct so that I can reuse my functions and structs in my implementation that will interface with java. At the moment because they're tightly coupled if I was to try and use these functions on a platform that isn't wasm it'll throw a panic.

Grant
  • 446
  • 6
  • 24

1 Answers1

1

Serialize and Deserialize are not specific to wasm and are available on other platforms.

For wasm_bindgen, you can use the cfg_attr attribute to only define it for the wasm platform if you replace #[wasm_bindgen with #[cfg_attr (wasm, wasm_bindgen)].

Jmb
  • 18,893
  • 2
  • 28
  • 55
  • Thanks, I was looking at cfg_attr, a quick question regarding cfg_attr. If I have a cfg_attr that evaluates to false does that mean I can still call the underlying function that the cfg_attr is for, it just won't have the attribute applied? – Grant Nov 18 '20 at 13:04
  • 1
    Yes `cfg_attr` is for conditioning the attributes themselves. If you want to completely enable or disable the function, you have to use the `cfg` attribute. – Jmb Nov 18 '20 at 13:40