Reputation: 5268
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?
Upvotes: 0
Views: 671
Reputation:
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
.
Upvotes: 6