How do I implement Sized, Serialize/Deserialize functions on Any and Send Traits?












4















I got an issue while implement serializing/deserializing and sizing functionalities on a struct that have complex data types like Arc pointers Mutex locks. First I've resolved these Arc and Mutex serialization/deserialization problem using this topic:



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



but now, I got stuck on implementing ser/desr and sizing for Any and Send traits, and I have neither an idea nor a compiling example to solve this issue.



Code is here:



#[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);
}


Here is the code in the Playground



It gives the following errors:



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)`









share|improve this question

























  • Welcome to Stack Overflow! Consider reading this question, I feel it's close to what you are looking for. However, dealing with serialization/deserialization for Any can become particularly tricky.

    – E_net4
    Nov 15 '18 at 9:48











  • yeah it's close I've already read it before asking the question but actually gives no idea about what we did for the solution, thanks by the way.

    – RustGear
    Nov 15 '18 at 10:49
















4















I got an issue while implement serializing/deserializing and sizing functionalities on a struct that have complex data types like Arc pointers Mutex locks. First I've resolved these Arc and Mutex serialization/deserialization problem using this topic:



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



but now, I got stuck on implementing ser/desr and sizing for Any and Send traits, and I have neither an idea nor a compiling example to solve this issue.



Code is here:



#[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);
}


Here is the code in the Playground



It gives the following errors:



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)`









share|improve this question

























  • Welcome to Stack Overflow! Consider reading this question, I feel it's close to what you are looking for. However, dealing with serialization/deserialization for Any can become particularly tricky.

    – E_net4
    Nov 15 '18 at 9:48











  • yeah it's close I've already read it before asking the question but actually gives no idea about what we did for the solution, thanks by the way.

    – RustGear
    Nov 15 '18 at 10:49














4












4








4


2






I got an issue while implement serializing/deserializing and sizing functionalities on a struct that have complex data types like Arc pointers Mutex locks. First I've resolved these Arc and Mutex serialization/deserialization problem using this topic:



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



but now, I got stuck on implementing ser/desr and sizing for Any and Send traits, and I have neither an idea nor a compiling example to solve this issue.



Code is here:



#[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);
}


Here is the code in the Playground



It gives the following errors:



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)`









share|improve this question
















I got an issue while implement serializing/deserializing and sizing functionalities on a struct that have complex data types like Arc pointers Mutex locks. First I've resolved these Arc and Mutex serialization/deserialization problem using this topic:



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



but now, I got stuck on implementing ser/desr and sizing for Any and Send traits, and I have neither an idea nor a compiling example to solve this issue.



Code is here:



#[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);
}


Here is the code in the Playground



It gives the following errors:



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)`






rust serde






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 10:28









Tim Diekmann

3,07791837




3,07791837










asked Nov 15 '18 at 8:14









RustGearRustGear

233




233













  • Welcome to Stack Overflow! Consider reading this question, I feel it's close to what you are looking for. However, dealing with serialization/deserialization for Any can become particularly tricky.

    – E_net4
    Nov 15 '18 at 9:48











  • yeah it's close I've already read it before asking the question but actually gives no idea about what we did for the solution, thanks by the way.

    – RustGear
    Nov 15 '18 at 10:49



















  • Welcome to Stack Overflow! Consider reading this question, I feel it's close to what you are looking for. However, dealing with serialization/deserialization for Any can become particularly tricky.

    – E_net4
    Nov 15 '18 at 9:48











  • yeah it's close I've already read it before asking the question but actually gives no idea about what we did for the solution, thanks by the way.

    – RustGear
    Nov 15 '18 at 10:49

















Welcome to Stack Overflow! Consider reading this question, I feel it's close to what you are looking for. However, dealing with serialization/deserialization for Any can become particularly tricky.

– E_net4
Nov 15 '18 at 9:48





Welcome to Stack Overflow! Consider reading this question, I feel it's close to what you are looking for. However, dealing with serialization/deserialization for Any can become particularly tricky.

– E_net4
Nov 15 '18 at 9:48













yeah it's close I've already read it before asking the question but actually gives no idea about what we did for the solution, thanks by the way.

– RustGear
Nov 15 '18 at 10:49





yeah it's close I've already read it before asking the question but actually gives no idea about what we did for the solution, thanks by the way.

– RustGear
Nov 15 '18 at 10:49












1 Answer
1






active

oldest

votes


















1














You can use this solution as a work around but it should work it out for you i guess.



You have to access the data: Arc<Mutex<Any + Send>> then serialize/deserialze data and id separately.



#[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);

}


Here is the playground.
Playground






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314981%2fhow-do-i-implement-sized-serialize-deserialize-functions-on-any-and-send-traits%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can use this solution as a work around but it should work it out for you i guess.



    You have to access the data: Arc<Mutex<Any + Send>> then serialize/deserialze data and id separately.



    #[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);

    }


    Here is the playground.
    Playground






    share|improve this answer




























      1














      You can use this solution as a work around but it should work it out for you i guess.



      You have to access the data: Arc<Mutex<Any + Send>> then serialize/deserialze data and id separately.



      #[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);

      }


      Here is the playground.
      Playground






      share|improve this answer


























        1












        1








        1







        You can use this solution as a work around but it should work it out for you i guess.



        You have to access the data: Arc<Mutex<Any + Send>> then serialize/deserialze data and id separately.



        #[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);

        }


        Here is the playground.
        Playground






        share|improve this answer













        You can use this solution as a work around but it should work it out for you i guess.



        You have to access the data: Arc<Mutex<Any + Send>> then serialize/deserialze data and id separately.



        #[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);

        }


        Here is the playground.
        Playground







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 13:02









        Alican BeydemirAlican Beydemir

        251412




        251412
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314981%2fhow-do-i-implement-sized-serialize-deserialize-functions-on-any-and-send-traits%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues