【发布时间】:2019-04-18 07:00:11
【问题描述】:
在具有复杂数据类型(如 Arc 指针 Mutex 锁)的结构上实现序列化/反序列化和调整大小功能时遇到问题。首先,我使用这个主题解决了这些Arc 和Mutex 序列化/反序列化问题:
How do I serialize or deserialize an Arc<T> in Serde?
但是现在,我陷入了实现ser/desr 和调整Any 和Send 特征的问题,我既没有想法也没有编译示例来解决这个问题。
代码在这里:
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
use serde::Serialize;
use std::sync::Mutex;
use std::sync::Arc;
use std::any::Any;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub id: u64,
pub data: Arc<Mutex<Any + Send>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Data {
pub name: String,
}
impl Data {
fn new(name_parameter: String) -> Data {
let data = Data {
name: name_parameter,
};
data
}
}
fn main() {
let msg: Message = Message { id: 23, data: (Arc::new(Mutex::new(Data::new(String::from("TesData"))))) };
let ser_msg = serde_json::to_string(&msg).unwrap();
let des_msg: Message = serde_json::from_str(&ser_msg).unwrap();
println!("{:?}", msg);
println!("{:?}", ser_msg);
println!("{:?}", des_msg);
}
这是Playground中的代码
它给出了以下错误:
error[E0277]: the size for values of type `(dyn std::any::Any + std::marker::Send + 'static)` cannot be known at compilation time
--> src/main.rs:15:5
|
15 | pub data: Arc<Mutex<Any + Send>>,
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::any::Any + std::marker::Send + 'static)`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required because of the requirements on the impl of `serde::Serialize` for `std::sync::Mutex<(dyn std::any::Any + std::marker::Send + 'static)>`
= note: required because of the requirements on the impl of `serde::Serialize` for `std::sync::Arc<std::sync::Mutex<(dyn std::any::Any + std::marker::Send + 'static)>>`
= note: required by `serde::ser::SerializeStruct::serialize_field`
error[E0277]: the trait bound `(dyn std::any::Any + std::marker::Send + 'static): serde::Serialize` is not satisfied
--> src/main.rs:15:5
|
15 | pub data: Arc<Mutex<Any + Send>>,
| ^^^ the trait `serde::Serialize` is not implemented for `(dyn std::any::Any + std::marker::Send + 'static)`
|
= note: required because of the requirements on the impl of `serde::Serialize` for `std::sync::Mutex<(dyn std::any::Any + std::marker::Send + 'static)>`
= note: required because of the requirements on the impl of `serde::Serialize` for `std::sync::Arc<std::sync::Mutex<(dyn std::any::Any + std::marker::Send + 'static)>>`
= note: required by `serde::ser::SerializeStruct::serialize_field`
error[E0277]: the trait bound `(dyn std::any::Any + std::marker::Send + 'static): serde::Deserialize<'_>` is not satisfied
--> src/main.rs:15:5
|
15 | pub data: Arc<Mutex<Any + Send>>,
| ^^^ the trait `serde::Deserialize<'_>` is not implemented for `(dyn std::any::Any + std::marker::Send + 'static)`
【问题讨论】:
-
欢迎来到 Stack Overflow!考虑阅读this question,我觉得它与您正在寻找的内容很接近。但是,处理
Any的序列化/反序列化可以变成particularly tricky。 -
是的,很接近我在提出问题之前已经阅读过它,但实际上并不清楚我们为解决方案做了什么,顺便感谢一下。