Here's the minimum reproducible example:
trait TestTrait {
fn as_any(&self) -> &dyn Any;
}
pub fn test() {
let test_vector : Vec<Box<dyn TestTrait>> = Vec::new();
// populate the vector
test_generic(test_vector.get(0).unwrap());
}
fn test_generic<T : 'static + TestTrait>( x:&Box<T> ) -> bool {
let test_vector : Vec<Box<dyn TestTrait>> = Vec::new();
// populate the vector
for value in test_vector.iter() {
match value.as_any().downcast_ref::<T>() {
None => {}
Some(_) => {return true;}
}
}
false
}
This gives this error:
error[E0277]: the size for values of type `dyn TestTrait` cannot be known at compilation time
--> src\main.rs:37:18
|
37 | test_generic(test_vector.get(0).unwrap());
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `dyn TestTrait`
So what do we have here:
There are two vectors of
Boxes
of items implementingTestTrait
test_generic
has access to one of the vectors, andtest
to anotherI want to know if there are any items in the second vector that have the same type as a specific item from the first vector
Is it possible, or is it gibberish?
I particularly don't understand why it needs to know the size of the type at compile time. It's a Box, isn't it? Its entire shtick is that it can point to a position on the heap that might be arbitrarily sized. I think I misunderstand something fundamental here. The Rust book even gives this exact scenario as a use-case for Boxes:
When you have a type whose size can’t be known at compile time and you want to use a value of that type in a context that requires an exact size