简体   繁体   中英

Rust generics: Expected <T> found <Foo>

I'm trying to use generics but I don't master that topic well enough and I get this error:

error: mismatched types:
expected `book::mdbook::MDBook<R>`,
found `book::mdbook::MDBook<renderer::html_handlebars::HtmlHandlebars>`
(expected type parameter,
found struct `renderer::html_handlebars::HtmlHandlebars`) [E0308]

This is the relevant code

pub struct MDBook<R> where R: Renderer {
    title: String,
    author: String,
    config: BookConfig,
    pub content: Vec<BookItem>,
    renderer: R,
}

impl<R> MDBook<R> where R: Renderer {

    pub fn new(path: &PathBuf) -> Self {

        MDBook {
            title: String::from(""),
            author: String::from(""),
            content: vec![],
            config: BookConfig::new()
                        .set_src(path.join("src"))
                        .set_dest(path.join("book")),
            renderer: HtmlHandlebars::new(), // <---- ERROR HERE
        }
    }
}

The Renderer trait is empty at the moment and the implementation for HtmlHandlebars is

pub struct HtmlHandlebars;

impl Renderer for HtmlHandlebars {

}

impl HtmlHandlebars {
    pub fn new() -> Self {
        HtmlHandlebars
    }
}

What am I doing wrong?

impl<R> MDBook<R> where R: Renderer {

    pub fn new(path: &PathBuf) -> Self {

These lines claim that for all types R that implement Renderer , there is a method new(path) that returns MDBook<R> . However, your implementation of the method always returns MDBook<HtmlHandlebars> regardless of what R is.

You could add a trait bound to R (or a method to Renderer ) that allows constructing a value of type R in new . Alternatively, the method could accept the renderer as parameter, ie fn new(path: &Path, renderer: R) -> Self . Either way, you need a way to get your hands on a renderer (ie, a value of type R ) inside new .

If on the other hand you want to support something like this:

let book = MDBook::new(path);
if some_condition {
    book.set_renderer(SomeOtherThing::new());
}

then generics are the wrong tool for the job, since they make the choice of renderer part of the static type of book . You can remove the R type parameter completely, keep your trait and simply store a trait object (likely Box<Renderer> ) in MDBook .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM