Trait object serialization for rkyv.
You may be looking for:
rkyv_dyn in action
```rust
use rkyv::{
Aligned,
Archive,
ArchiveBuffer,
Archived,
archivedvalue,
WriteExt,
};
use rkyvdyn::archivedyn;
use rkyvtypename::TypeName;
[archive_dyn]
trait ExampleTrait {
fn value(&self) -> String;
}
[derive(Archive, TypeName)]
struct StringStruct(String);
[archive_dyn]
impl ExampleTrait for StringStruct {
fn value(&self) -> String {
self.0.clone()
}
}
impl ExampleTrait for Archived {
fn value(&self) -> String {
self.0.asstr().tostring()
}
}
[derive(Archive, TypeName)]
struct IntStruct(i32);
[archive_dyn]
impl ExampleTrait for IntStruct {
fn value(&self) -> String {
format!("{}", self.0)
}
}
impl ExampleTrait for Archived {
fn value(&self) -> String {
format!("{}", self.0)
}
}
fn main() {
let boxedint = Box::new(IntStruct(42)) as Box;
let boxedstring = Box::new(StringStruct("hello world".tostring())) as Box;
let mut writer = ArchiveBuffer::new(Aligned([0u8; 256]));
let intpos = writer.archive(&boxedint)
.expect("failed to archive boxed int");
let stringpos = writer.archive(&boxedstring)
.expect("failed to archive boxed string");
let buf = writer.intoinner();
let archivedint = unsafe { archivedvalue::>(buf.asref(), intpos) };
let archivedstring = unsafe { archivedvalue::>(buf.asref(), stringpos) };
asserteq!(archivedint.value(), "42");
asserteq!(archivedstring.value(), "hello world");
}
```