I want the target family to control whether to grant the #[wasm_bindgen] attribute in Rust.

Asked 1 months ago, Updated 1 months ago, 4 views

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

2022-09-30 12:08

1 Answers

#[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


2022-09-30 12:08

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.