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:
pub trait Generable {
type From;
fn generate_children(&self, data: &Self::From) -> Vec<Option<Self>>
where
Self: Sized;
}
pub enum Octree<T>
where
T: Generable,
{
Node {
data: T,
children: Vec<Box<Octree<T>>>,
},
Empty,
Uninitialized,
}
fn main() {}
As an aside, Vec<Box<Octree<T>>>
is probably one level extra of indirection — you can just use Vec<Octree<T>>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With