我收到错误:
expected enum `std::result::Result<_, wasm_bindgen::JsValue>`
found enum `std::result::Result<_, serde_wasm_bindgen::error::Error>`
当我通过实现 Serialize 来序列化一个结构然后将其传递给 时serde_wasm_bindgen
,它使用此处
的示例代码
是最小可重现代码:
use wasm_bindgen::prelude::*;
use serde::ser::{Serialize, SerializeStruct, Serializer};
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
impl Serialize for Person {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("Person", 3)?;
s.serialize_field("name", &self.name)?;
s.serialize_field("age", &self.age)?;
s.serialize_field("phones", &self.phones)?;
s.end()
}
}
#[wasm_bindgen]
pub fn pass_value_to_js() -> Result<JsValue, JsValue> {
let p = Person {
name: String::from("Hello"),
age: 56,
phones: vec![String::from("phone")],
};
serde_wasm_bindgen::to_value(p) // error here
}
货运.toml
serde-wasm-bindgen = "0.1.3"
serde = "1.0.114"