I have the following traits and types:
trait MyTrait1 {}
trait MyTrait2 {}
trait MyTrait3 {}
trait MyTrait4 {}
struct MyStruct1<T>(T);
struct MyStruct2<T>(T);
struct MyStruct3<T>(T);
struct MyStruct4<T>(T);
impl<T> MyTrait1 for MyStruct1<T> {}
impl<T: MyTrait1> MyTrait2 for MyStruct2<T> {}
impl<T: MyTrait2> MyTrait3 for MyStruct3<T> {}
impl<T: MyTrait3> MyTrait4 for MyStruct4<T> {}
I try to save the Box variable my_box
:
let my_struct1 = MyStruct1(1);
let my_struct2 = MyStruct2(my_struct1);
let my_strcut3 = MyStruct3(my_struct2);
let my_struct4 = MyStruct4(my_struct4);
let my_box: Box<dyn MyTrait4> = Box::new(my_struct4);
I tried to get to this in these ways:
-
serde
+typetag
:
#[typetag::serde(tag = "MyTrait1")]
trait MyTrait1 {}
#[derive(Serialize, Deserialize)]
struct MyStruct1<T>(T);
#[typetag::serde]
impl<T> MyTrait1 for MyStruct1<T> {}
But it has the compile error:
error: deserialization of generic impls is not supported yet; use #[typetag::serialize] to generate serialization only
And I need the types to have generic type parameters, so it seems typetag
can not work it out.
-
I save the
my_box
as aString
to a local .txt file, and when I need to load it, I open the file and clone the text then paste it to where it is needed. It works fine in simple example. But when I have aVec
ofBox<dyn MyTrait>
, the file is too large, approximattly 60000 lines. If have paste the 60000 lines codes to the project, then the compile process take a long time. And besides, the project just can not compile, throw the error likemain overflow stack.
-
Save the
my_box
as aString
to file, and eval the string when it is needed. But in Rust, there is no way to eval a string like in Java.
I have struggled for this for a month, and it seems it is very hard to do this in Rust. I thought the reason maybe that Rust have no runtime and no reflection (I'm new to programming, so the terms maybe wrong). So is there any other way that can help?