Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

rust - Why am I getting "parameter is never used [E0392]"?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...