refactor(render): transform boxed extensions to references

This commit is contained in:
Federico Terzi 2021-04-21 20:49:25 +02:00
parent 1e65d2ead6
commit 4af0cde3ed
4 changed files with 15 additions and 14 deletions

View File

@ -24,18 +24,18 @@ pub trait ClipboardProvider {
fn get_text(&self) -> Option<String>;
}
pub struct ClipboardExtension {
provider: Box<dyn ClipboardProvider>,
pub struct ClipboardExtension<'a> {
provider: &'a dyn ClipboardProvider,
}
#[allow(clippy::new_without_default)]
impl ClipboardExtension {
pub fn new(provider: Box<dyn ClipboardProvider>) -> Self {
impl <'a> ClipboardExtension<'a> {
pub fn new(provider: &'a dyn ClipboardProvider) -> Self {
Self { provider }
}
}
impl Extension for ClipboardExtension {
impl <'a> Extension for ClipboardExtension<'a> {
fn name(&self) -> &str {
"clipboard"
}
@ -76,7 +76,7 @@ mod tests {
#[test]
fn clipboard_works_correctly() {
let provider = MockClipboardProvider { return_none: false };
let extension = ClipboardExtension::new(Box::new(provider));
let extension = ClipboardExtension::new(&provider);
assert_eq!(
extension
@ -90,7 +90,7 @@ mod tests {
#[test]
fn none_clipboard_produces_error() {
let provider = MockClipboardProvider { return_none: true };
let extension = ClipboardExtension::new(Box::new(provider));
let extension = ClipboardExtension::new(&provider);
assert!(matches!(
extension.calculate(&Default::default(), &Default::default(), &Params::new()),

View File

@ -179,6 +179,7 @@ pub enum ScriptExtensionError {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(target_os = "windows"))]
use crate::Scope;
use std::iter::FromIterator;

View File

@ -31,7 +31,7 @@ pub trait Renderer {
-> RenderResult;
}
pub fn create(extensions: Vec<Box<dyn Extension>>) -> impl Renderer {
pub fn create<'a>(extensions: Vec<&'a dyn Extension>) -> impl Renderer + 'a {
renderer::DefaultRenderer::new(extensions)
}

View File

@ -36,12 +36,12 @@ lazy_static! {
static ref WORD_REGEX: Regex = Regex::new(r"(\w+)").unwrap();
}
pub(crate) struct DefaultRenderer {
extensions: HashMap<String, Box<dyn Extension>>,
pub(crate) struct DefaultRenderer<'a> {
extensions: HashMap<String, &'a dyn Extension>,
}
impl DefaultRenderer {
pub fn new(extensions: Vec<Box<dyn Extension>>) -> Self {
impl <'a> DefaultRenderer<'a> {
pub fn new(extensions: Vec<&'a dyn Extension>) -> Self {
let extensions = extensions
.into_iter()
.map(|ext| (ext.name().to_string(), ext))
@ -50,7 +50,7 @@ impl DefaultRenderer {
}
}
impl Renderer for DefaultRenderer {
impl <'a> Renderer for DefaultRenderer<'a> {
fn render(
&self,
template: &Template,
@ -287,7 +287,7 @@ mod tests {
}
pub fn get_renderer() -> impl Renderer {
DefaultRenderer::new(vec![Box::new(MockExtension {})])
DefaultRenderer::new(vec![&MockExtension {}])
}
pub fn template_for_str(str: &str) -> Template {