I would like to share the domain(entity) logic with the code cli and wasm in Rust.
However, since we have to give #[wasm_bindgen]
to the wasm function, we control which functions are generated by whether or not the target is wasm as follows:
Excerpts from domain.rs
#[cfg(target_family="wasm")]
# [wasm_bindgen]
pubfn add(a:i32, b:i32) - > i32 {
a+b
}
# [cfg(not(target_family="wasm")]
pubfn add(a:i32, b:i32) - > i32 {
a+b
}
https://github.com/iranika/chimey
If true, I would like to control whether or not #[wasm_bindgen]
is given to centralize functions, but #[cfg(target_family="wasm")]
should not be able to control the attribute alone.
(Please let me know if there is a way I can do it)
Is there a good way?
rust webassembly
#[cfg_attr(a,b)]
is likely to work.
# If [cfg(a)] is enabled, it will do #[b], so I think it matches the case of the question.
#[cfg_attr(target_family="wasm", wasm_bindgen)]
pubfn add(a:i32, b:i32) - > i32 {
a+b
}
Materials (in English...)
https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute
© 2024 OneMinuteCode. All rights reserved.