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

View File

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

View File

@ -31,7 +31,7 @@ pub trait Renderer {
-> RenderResult; -> 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) renderer::DefaultRenderer::new(extensions)
} }

View File

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