我试图导出以下结构体:
#[wasm_bindgen]
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum TokenType {
KeywordLiteral,
NumberLiteral,
Operator,
Separator,
StringLiteral,
}
#[wasm_bindgen]
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct Token {
pub typ: TokenType,
pub val: String,
}
但我得到的是:
error[E0277]: the trait bound `token::TokenType: std::marker::Copy` is not satisfied
--> src\tokenizer\token.rs:17:14
|
14 | #[wasm_bindgen]
| --------------- required by this bound in `__wbg_get_token_typ::assert_copy`
...
17 | pub typ: TokenType,
| ^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `token::TokenType`
以及:
error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
--> src\tokenizer\token.rs:18:14
|
14 | #[wasm_bindgen]
| --------------- required by this bound in `__wbg_get_token_val::assert_copy`
...
18 | pub val: String,
我可以给TokenType
添加#[derive(Copy)]
,但无法给String
添加。
我是Rust的新手,所以需要帮助。感谢您的支持。
String
是不可能的吗? - Eliaspub
。 - Mathieu ReneString
暴露给JavaScript? - EliasBox<[JsValue]>
是支持的,您可以使用Vec::into_boxed_slice
将其从Vec<JsValue>
转换过来,您可以通过类似.map(JsValue::from)
的方式使用 wasm-bindgen 生成的 From impl 获取它。 - Mathieu Rene