I'm trying to implement an Octree in Rust. The Octree is generic over a type with a constraint that it should implement a generic trait:
pub trait Generable<U> {
fn generate_children(&self, data: &U) -> Vec<Option<Self>>;
}
pub enum Octree<T, U>
where
T: Generable<U>,
{
Node {
data: T,
children: Vec<Box<Octree<T, U>>>,
},
Empty,
Uninitialized,
}
Here is a simplified example reproducing the issue on the Playground
This generates an error:
error[E0392]: parameter `U` is never used
--> src/main.rs:5:20
|
5 | pub enum Octree<T, U>
| ^ unused type parameter
|
= help: consider removing `U` or using a marker such as `std::marker::PhantomData`
Removing the U
from the signature results in "undeclared type name 'U'".
Am I doing something wrong or is it a bug? How to do this properly?
I don't believe you want another generic here, you want an associated type:
As an aside,
Vec<Box<Octree<T>>>
is probably one level extra of indirection — you can just useVec<Octree<T>>
.