rust - 如何为外部类型实现 serde 的反序列化和序列化?
问题描述
我正在尝试为外部枚举实现Serialize
& ,但我不知道如何。Deserialize
它有From<u64>
所以我想做的就是让那个对象序列化。
#[derive(Serialize, Deserialize)]
pub struct ImageBinds {
binds: HashMap<KeybdKey, Vec<u8>>, // KeybdKey doesn't implement serialize
}
// won't compile with this:
// only traits defined in the current crate can be implemented for arbitrary types
// impl doesn't use only types from inside the current crate
// I understand the orphan rule and all that but I have no idea how else I'd implement it
// I've looked at serde remote stuff and I cannot work out how to do it with an enum without
// literally just copying the whole crate into my scope which is stupid
impl Serialize for KeybdKey {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u64(u64::from(*self))
}
}
KeybdKey
来自inputbot
板条箱。
解决方案
推荐阅读
- apache-spark - 如何在pyspark的循环中合并数据帧
- sql-server - 使用不同的 Windows 用户帐户登录 SQL Server
- java - 为什么我的 Button 的背景色在运行应用程序时变为灰色?
- javascript - CryptoJS AES 加密到字符串方法和密文到 Base64
- javascript - 带有 javascript 或 php 的 HTML 日志系统
- xcode - 情节提要'未设置指定的入口点'
- tensorflow2.0 - 无法在转换组件 (TFX) 中使用句子嵌入
- java - 用 list.add(index, value) 修改后 ListNode 代码不显示列表;
- postgresql - postgresql initdb.exe 选项标志在命令行中不起作用
- python - 抓取包含“#”的链接时出现问题