【问题标题】:How do I implement Sized, Serialize/Deserialize functions on Any and Send Traits?如何在 Any 和 Send Traits 上实现 Sized、Serialize/Deserialize 函数?
【发布时间】:2019-04-18 07:00:11
【问题描述】:

在具有复杂数据类型(如 Arc 指针 Mutex 锁)的结构上实现序列化/反序列化和调整大小功能时遇到问题。首先,我使用这个主题解决了这些ArcMutex 序列化/反序列化问题:

How do I serialize or deserialize an Arc<T> in Serde?

但是现在,我陷入了实现ser/desr 和调整AnySend 特征的问题,我既没有想法也没有编译示例来解决这个问题。

代码在这里:

#[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
  • 是的,很接近我在提出问题之前已经阅读过它,但实际上并不清楚我们为解决方案做了什么,顺便感谢一下。

标签: rust serde


【解决方案1】:

您可以将此解决方案用作解决方法,但我猜它应该可以解决您的问题。

您必须访问data: Arc&lt;Mutex&lt;Any + Send&gt;&gt;,然后分别序列化/反序列化数据和id。

#[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)]
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 _id = msg.id;
    let guard = msg.data.lock().unwrap();
    let msg_data: Option<&Data> = guard.downcast_ref::<Data>();
    let ser_msg_data = serde_json::to_string(&msg_data).unwrap();
    let des_msg_data: Data = serde_json::from_str(&ser_msg_data).unwrap();
    println!("{:?}", des_msg_data);
    let des_msg:Message = Message {id : _id, data: Arc::new(Mutex::new(des_msg_data))};
    println!("{:?}", msg);
    println!("{:?}", ser_msg_data);
    println!("{:?}", des_msg);

}

这里是游乐场。 Playground

【讨论】:

    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2020-05-19
    • 1970-01-01
    相关资源
    最近更新 更多