I started a new project, where I want to be as modular as possible, by that I mean that I would like to be able to replace some parts with others in the future. This seems to be a perfect use for traits
, at the moment I have this code:
mod parser;
mod renderer;
mod renderers;
use parser::MarkParser;
use renderer::MarkRenderer;
struct Rustmark <P: MarkParser, R: MarkRenderer> {
parser: P,
renderer: R,
}
impl <P: MarkParser, R: MarkRenderer> Rustmark <P, R> {
fn new() -> Rustmark <P, R> {
Rustmark {
parser: parser::DefaultParser::new(),
renderer: renderers::HTMLRenderer::new(),
}
}
fn render(&self, input: &str) -> &str {
self.renderer.render(self.parser.parse(input))
}
}
But I get a couple of errors, mainly this one:
error: mismatched types:
expected Rustmark<P, R>
,
found Rustmark<parser::DefaultParser, renderers::html::HTMLRenderer>
(expected type parameter,
found struct parser::DefaultParser
) [E0308]
And a couple of lifetime errors like this one:
error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
help: consider using an explicit lifetime parameter as shown: fn parse<'a>(&'a self, input: &'a str) -> &str
I have tried multiple tweaks to make it work, but none of them seemed to appease the compiler. So I wanted to know if this is the right approach and what I could do to to make it work.
First error: you create a Rustmark
object with the field parser
of type DefaultParser
and the field renderer
of type HTMLRenderer
, but the function is expected to return Rustmark <P, R>
. In general P is not of type DefaultParser
and R is not of type HTMLRenderer
, so it will never compile. A good solution if you want have default values of the right type is to bound P
and R
to implement the Default
trait
, this way:
use std::default:Default;
impl <P: MarkParser + Default, R: MarkRenderer + Default> Rustmark <P, R> {
fn new() -> Rustmark <P, R> {
Rustmark {
parser: P::default(),
renderer: R:default(),
}
}
}
Second error: that main problem is that you return a reference of something that probably will die inside the render
method (the String
you allocate in the inner render
method probably). The compiler is telling you that it doesn't know the lifetime of the object that is pointed by that reference, so it cannot guarantee that the reference is valid. You can specify a lifetime parameter but in your case probably the best solution is to return the String
object itself, not a reference.
Following the answer of Andrea P I looked up the default
trait in std. Which is defined like this:
pub trait Default {
fn default() -> Self;
}
And what I ended up doing was not use the default
trait but add a constructor new
in my MarkParser
and MarkRenderer
traits like this:
pub trait MarkParser {
fn new() -> Self;
fn parse(&self, input: &str) -> &str;
}
The key piece that I didn't know about was the Self
keyword
and in this way I can write my implementation like this:
impl <P: MarkParser, R: MarkRenderer> Rustmark <P, R> {
fn new() -> Rustmark <P, R> {
Rustmark {
parser: P::new(),
renderer: R::new(),
}
}
fn render(&self, input: &str) -> &str {
self.renderer.render(self.parser.parse(input))
}
}
This is exactly the same thing as implementing the Default
trait, except that I get to use new
instead of default
which I prefer and I don't have to add the Default
trait to the impl
of RustMark.
impl <P: MarkParser, R: MarkRenderer> Rustmark <P, R> {
instead of
impl <P: MarkParser + Default, R: MarkRenderer + Default> Rustmark <P, R> {
Big thanks to Andrea P for pointing me in the right direction.