serde_traitobject 0.1.0

Serializable trait objects. This library enables the serialization of trait objects such that they can be sent between other processes running the same binary.
docs.rs failed to build serde_traitobject-0.1.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: serde_traitobject-0.2.8

serde_traitobject

Crates.io Apache-2.0 licensed Build Status Build Status Build Status

Docs

Serializable trait objects.

This library enables the serialization of trait objects such that they can be sent between other processes running the same binary.

For example, if you have multiple forks of a process, or the same binary running on each of a cluster of machines, this library would help you to send trait objects between them.

The heart of this crate is the Serialize and Deserialize traits. They are automatically implemented for all T: serde::Serialize and all T: serde::de::DeserializeOwned respectively.

Any trait can be made (de)serializable when made into a trait object by simply adding them as supertraits:

#[macro_use]
extern crate serde_derive;
extern crate serde_traitobject;

trait MyTrait: serde_traitobject::Serialize + serde_traitobject::Deserialize {
	fn my_method(&self);
}

#[derive(Serialize, Deserialize)]
struct Message(#[serde(with = "serde_traitobject")] Box<dyn MyTrait>);

// Woohoo, `Message` is now serializable!

There are two ways to use serde_traitobject to handle the (de)serialization:

  • #[serde(with = "serde_traitobject")] field attribute on a boxed trait object, which instructs serde to use the serialize and deserialize functions;
  • The Box, Rc and Arc structs, which are simple wrappers around their stdlib counterparts that automatically handle (de)serialization without needing the above annotation;

Additionally, there are several convenience traits implemented that extend their stdlib counterparts:

These are automatically implemented on all (de)serializable implementors of their stdlib counterparts:

#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate serde_traitobject as s;

use std::any::Any;

#[derive(Serialize, Deserialize, Debug)]
struct MyStruct {
	foo: String,
	bar: usize,
}

let my_struct = MyStruct {
	foo: String::from("abc"),
	bar: 123,
};

let erased: s::Box<dyn s::Any> = s::Box::new(my_struct);

let serialized = serde_json::to_string(&erased).unwrap();
let deserialized: s::Box<dyn s::Any> = serde_json::from_str(&serialized).unwrap();

let downcast: Box<MyStruct> = Box::<dyn Any>::downcast(deserialized.into_any()).unwrap();

println!("{:?}", downcast);
// MyStruct { foo: "abc", bar: 123 }

Note

This crate works by wrapping the vtable pointer with relative::Pointer such that it can safely be sent between processes.

This currently requires Rust nightly.

License

Licensed under Apache License, Version 2.0, (LICENSE.txt or http://www.apache.org/licenses/LICENSE-2.0).

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.