If I have an "outer" trait with a univeral generic type (Collection
, below), then I can create an instance of that with an "inner" trait type (Collection<&dyn Stringable>
). I can then use any values with that instance as long as those values implement the nested trait (Stringable
):
fn main() {
let mut vs: &mut Collection<&dyn Stringable> = &mut vec![];
vs.add(&1);
vs.add(&true);
vs.add(&3);
for v in vs.get_all() {
println!("{}", v.string());
}
}
trait Collection<T> {
fn add(&mut self, v: T);
fn get_all(&self) -> &Vec<T>;
}
impl<T> Collection<T> for Vec<T> {
fn add(&mut self, v: T) {
self.push(v)
}
fn get_all(&self) -> &Vec<T> {
&self
}
}
trait Stringable {
fn string(&self) -> String;
}
impl Stringable for i8 {
fn string(&self) -> String {
format!("int({})", self)
}
}
impl Stringable for bool {
fn string(&self) -> String {
format!("bool({})", self)
}
}
However, if I implement both the outer and inner trait for a type (Collection<i8>
), a value of that type can't be put in a Collection<&dyn Stringable>
variable, even though the inner type of the first (i8
) implements the second (Stringable
). The code below gives the following error:
the trait `Collection<&dyn Stringable>` is not implemented for `StaticCollection`
Code:
fn main() {
let mut vs: &mut Collection<&dyn Stringable> = &mut StaticCollection{};
for v in vs.get_all() {
println!("{}", v.string());
}
}
struct StaticCollection {}
impl Collection<i8> for StaticCollection {
fn add(&mut self, v: i8) {}
fn get_all(&self) -> &Vec<i8> {
&vec![1, 2, 3]
}
}
Is it possible, for example, to write something like impl Collection<&dyn Stringable> for StaticCollection
, so that the StaticCollection
can be stored in the generic Collection<&dyn Stringable>
variable?