0

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?

Mathieu David
  • 4,706
  • 3
  • 18
  • 28

1 Answers1

6
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, i.e. fn new(path: &Path, renderer: R) -> Self. Either way, you need a way to get your hands on a renderer (i.e., 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.

  • I want `new` to always return `MDBook` because it's the default renderer. Should I change the return value from `Self` to `MDBook` then? – Mathieu David Jul 18 '15 at 12:13
  • Actually what I want to achieve is being able to swap out the Renderer for another. And traits seemed to be the best way to do it... Was that a good idea or is there a better way? – Mathieu David Jul 18 '15 at 12:16
  • 1
    @MathieuDavid You can have a method that returns `MDBook`, though it would be more appropriate to put that into an `impl MDBook {}` block. However, if you don't have some other methods that do work with `MDBook` for all `R`, then the generic is pointless. Also the name would have to be different, since there is no overloading here. Perhaps ask yourself why you want to make a special case for this default. –  Jul 18 '15 at 12:17
  • @MathieuDavid If you want to be able to have a `MDBook` object and change dynamically what renderer it uses, then generics are the wrong tool (though *traits* are good for that too). I've edited the answer to cover that as well. –  Jul 18 '15 at 12:17
  • Thank you so much, using `Box` works great. – Mathieu David Jul 18 '15 at 12:31