trait T {}
struct S;
impl T for S {}
fn use_trait_obj(o: Box<T>) -> Box<T> {
// does some stuff ...
o
}
fn main(){
let s = S;
let t: Box<T> = use_trait_obj(Box::new(s));
let s: Box<S> = ...; // ?
}
Can I revert a trait object to its concrete type?
In my use-case, T
is a fixed trait, S
is a special implementation for testing purposes, and after calling the function I wish to check the internal fields of the object as part of the test.
I tried adapting T
to "extend" the special trait std::any::Any
but still couldn't figure out how to do this.