From 577e030db2f83f30b944381129a4b536bbbf920a Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 21 Mar 2024 16:41:47 +0900 Subject: [PATCH] templater: remove unused context parameter from Template IntoTemplate will be cleaned up later. Perhaps, the lifetime parameter can be removed at this point, but I'm planning to remove the IntoTemplate trait at all. --- cli/src/commit_templater.rs | 20 +-- cli/src/generic_templater.rs | 2 +- cli/src/operation_templater.rs | 6 +- cli/src/template_builder.rs | 55 ++++--- cli/src/templater.rs | 252 ++++++++++++++++----------------- 5 files changed, 159 insertions(+), 176 deletions(-) diff --git a/cli/src/commit_templater.rs b/cli/src/commit_templater.rs index c997f08f2..66887dd4e 100644 --- a/cli/src/commit_templater.rs +++ b/cli/src/commit_templater.rs @@ -283,7 +283,7 @@ impl<'repo> IntoTemplateProperty<'repo> for CommitTemplatePropertyKind<'repo> { } } - fn try_into_template(self) -> Option + 'repo>> { + fn try_into_template(self) -> Option> { match self { CommitTemplatePropertyKind::Core(property) => property.try_into_template(), CommitTemplatePropertyKind::Commit(_) => None, @@ -651,8 +651,8 @@ impl RefName { } } -impl Template<()> for RefName { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for RefName { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter.labeled("name"), "{}", self.name)?; if let Some(remote) = &self.remote { write!(formatter, "@")?; @@ -669,9 +669,9 @@ impl Template<()> for RefName { } } -impl Template<()> for Vec { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { - templater::format_joined(&(), formatter, self, " ") +impl Template for Vec { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { + templater::format_joined(formatter, self, " ") } } @@ -823,8 +823,8 @@ impl CommitOrChangeId { } } -impl Template<()> for CommitOrChangeId { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for CommitOrChangeId { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter, "{}", self.hex()) } } @@ -865,8 +865,8 @@ pub struct ShortestIdPrefix { pub rest: String, } -impl Template<()> for ShortestIdPrefix { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for ShortestIdPrefix { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter.labeled("prefix"), "{}", self.prefix)?; write!(formatter.labeled("rest"), "{}", self.rest)?; Ok(()) diff --git a/cli/src/generic_templater.rs b/cli/src/generic_templater.rs index 00026a087..2fbc35c57 100644 --- a/cli/src/generic_templater.rs +++ b/cli/src/generic_templater.rs @@ -145,7 +145,7 @@ impl<'a, C: 'a> IntoTemplateProperty<'a> for GenericTemplatePropertyKind<'a, C> } } - fn try_into_template(self) -> Option + 'a>> { + fn try_into_template(self) -> Option> { match self { GenericTemplatePropertyKind::Core(property) => property.try_into_template(), GenericTemplatePropertyKind::Self_(_) => None, diff --git a/cli/src/operation_templater.rs b/cli/src/operation_templater.rs index 8bd06b6f6..e592adce8 100644 --- a/cli/src/operation_templater.rs +++ b/cli/src/operation_templater.rs @@ -160,7 +160,7 @@ impl IntoTemplateProperty<'static> for OperationTemplatePropertyKind { } } - fn try_into_template(self) -> Option>> { + fn try_into_template(self) -> Option> { match self { OperationTemplatePropertyKind::Core(property) => property.try_into_template(), OperationTemplatePropertyKind::Operation(_) => None, @@ -275,8 +275,8 @@ fn builtin_operation_methods() -> OperationTemplateBuildMethodFnMap { map } -impl Template<()> for OperationId { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for OperationId { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter, "{}", self.hex()) } } diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index d8acf85b5..03b942699 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -45,8 +45,8 @@ pub trait TemplateLanguage<'a> { property: impl TemplateProperty + 'a, ) -> Self::Property; - fn wrap_template(template: Box + 'a>) -> Self::Property; - fn wrap_list_template(template: Box + 'a>) -> Self::Property; + fn wrap_template(template: Box) -> Self::Property; + fn wrap_list_template(template: Box) -> Self::Property; /// Translates the given global `function` call to a property. /// @@ -87,13 +87,13 @@ macro_rules! impl_core_wrap_property_fns { } ); fn wrap_template( - template: Box + $a>, + template: Box, ) -> Self::Property { use $crate::template_builder::CoreTemplatePropertyKind as Kind; $outer(Kind::Template(template)) } fn wrap_list_template( - template: Box + $a>, + template: Box, ) -> Self::Property { use $crate::template_builder::CoreTemplatePropertyKind as Kind; $outer(Kind::ListTemplate(template)) @@ -122,7 +122,7 @@ pub trait IntoTemplateProperty<'a> { fn try_into_integer(self) -> Option + 'a>>; fn try_into_plain_text(self) -> Option + 'a>>; - fn try_into_template(self) -> Option + 'a>>; + fn try_into_template(self) -> Option>; } pub enum CoreTemplatePropertyKind<'a> { @@ -139,8 +139,8 @@ pub enum CoreTemplatePropertyKind<'a> { // Similar to `TemplateProperty + 'a>`, but doesn't // capture `I` to produce `Template<()>`. The context `I` would have to be cloned // to convert `Template` to `Template<()>`. - Template(Box + 'a>), - ListTemplate(Box + 'a>), + Template(Box), + ListTemplate(Box), } impl<'a> IntoTemplateProperty<'a> for CoreTemplatePropertyKind<'a> { @@ -183,14 +183,14 @@ impl<'a> IntoTemplateProperty<'a> for CoreTemplatePropertyKind<'a> { // 1. stringify property without using Template type (works only for // non-template expressions) // 2. add flag to propagate property error as io::Error::other() (e.g. - // Template::format(context, formatter, propagate_err)) + // Template::format(formatter, propagate_err)) let template = self.try_into_template()?; Some(Box::new(PlainTextFormattedProperty::new(template))) } } } - fn try_into_template(self) -> Option + 'a>> { + fn try_into_template(self) -> Option> { match self { CoreTemplatePropertyKind::String(property) => Some(property.into_template()), CoreTemplatePropertyKind::StringList(property) => Some(property.into_template()), @@ -400,7 +400,7 @@ impl<'a, P: IntoTemplateProperty<'a>> Expression

{ self.property.try_into_plain_text() } - pub fn try_into_template(self) -> Option + 'a>> { + pub fn try_into_template(self) -> Option> { let template = self.property.try_into_template()?; if self.labels.is_empty() { Some(template) @@ -750,7 +750,7 @@ fn builtin_timestamp_range_methods<'a, L: TemplateLanguage<'a> + ?Sized>( fn build_list_template_method<'a, L: TemplateLanguage<'a> + ?Sized>( language: &L, build_ctx: &BuildContext, - self_template: Box + 'a>, + self_template: Box, function: &FunctionCallNode, ) -> TemplateParseResult { let property = match function.name { @@ -776,7 +776,7 @@ pub fn build_formattable_list_method<'a, L, O>( ) -> TemplateParseResult where L: TemplateLanguage<'a> + ?Sized, - O: Template<()> + Clone + 'a, + O: Template + Clone + 'a, { let property = match function.name { "len" => { @@ -788,8 +788,8 @@ where let [separator_node] = template_parser::expect_exact_arguments(function)?; let separator = expect_template_expression(language, build_ctx, separator_node)?; let template = - ListPropertyTemplate::new(self_property, separator, |_, formatter, item| { - item.format(&(), formatter) + ListPropertyTemplate::new(self_property, separator, |formatter, item| { + item.format(formatter) }); L::wrap_template(Box::new(template)) } @@ -864,8 +864,8 @@ where let list_template = ListPropertyTemplate::new( self_property, Literal(" "), // separator - move |context, formatter, item| { - item_placeholder.with_value(item, || item_template.format(context, formatter)) + move |formatter, item| { + item_placeholder.with_value(item, || item_template.format(formatter)) }, ); Ok(L::wrap_list_template(Box::new(list_template))) @@ -879,22 +879,19 @@ fn builtin_functions<'a, L: TemplateLanguage<'a> + ?Sized>() -> TemplateBuildFun let [width_node, content_node] = template_parser::expect_exact_arguments(function)?; let width = expect_usize_expression(language, build_ctx, width_node)?; let content = expect_template_expression(language, build_ctx, content_node)?; - let template = ReformatTemplate::new(content, move |_context, formatter, recorded| { - match width.extract() { + let template = + ReformatTemplate::new(content, move |formatter, recorded| match width.extract() { Ok(width) => text_util::write_wrapped(formatter, recorded, width), - Err(err) => err.format(&(), formatter), - } - }); + Err(err) => err.format(formatter), + }); Ok(L::wrap_template(Box::new(template))) }); map.insert("indent", |language, build_ctx, function| { let [prefix_node, content_node] = template_parser::expect_exact_arguments(function)?; let prefix = expect_template_expression(language, build_ctx, prefix_node)?; let content = expect_template_expression(language, build_ctx, content_node)?; - let template = ReformatTemplate::new(content, move |context, formatter, recorded| { - text_util::write_indented(formatter, recorded, |formatter| { - prefix.format(context, formatter) - }) + let template = ReformatTemplate::new(content, move |formatter, recorded| { + text_util::write_indented(formatter, recorded, |formatter| prefix.format(formatter)) }); Ok(L::wrap_template(Box::new(template))) }); @@ -944,13 +941,13 @@ fn builtin_functions<'a, L: TemplateLanguage<'a> + ?Sized>() -> TemplateBuildFun let prefix = expect_template_expression(language, build_ctx, prefix_node)?; let suffix = expect_template_expression(language, build_ctx, suffix_node)?; let content = expect_template_expression(language, build_ctx, content_node)?; - let template = ReformatTemplate::new(content, move |context, formatter, recorded| { + let template = ReformatTemplate::new(content, move |formatter, recorded| { if recorded.data().is_empty() { return Ok(()); } - prefix.format(context, formatter)?; + prefix.format(formatter)?; recorded.replay(formatter)?; - suffix.format(context, formatter)?; + suffix.format(formatter)?; Ok(()) }); Ok(L::wrap_template(Box::new(template))) @@ -1121,7 +1118,7 @@ pub fn expect_template_expression<'a, L: TemplateLanguage<'a> + ?Sized>( language: &L, build_ctx: &BuildContext, node: &ExpressionNode, -) -> TemplateParseResult + 'a>> { +) -> TemplateParseResult> { build_expression(language, build_ctx, node)? .try_into_template() .ok_or_else(|| TemplateParseError::expected_type("Template", node.span)) diff --git a/cli/src/templater.rs b/cli/src/templater.rs index 39cfa4fab..d69f3f4cf 100644 --- a/cli/src/templater.rs +++ b/cli/src/templater.rs @@ -21,42 +21,42 @@ use jj_lib::backend::{Signature, Timestamp}; use crate::formatter::{FormatRecorder, Formatter, PlainTextFormatter}; use crate::time_util; -pub trait Template { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()>; +/// Represents printable type or compiled template containing placeholder value. +pub trait Template { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()>; } /// Template that supports list-like behavior. -pub trait ListTemplate: Template { +pub trait ListTemplate: Template { /// Concatenates items with the given separator. - fn join<'a>(self: Box, separator: Box + 'a>) -> Box + 'a> + fn join<'a>(self: Box, separator: Box) -> Box where - Self: 'a, - C: 'a; + Self: 'a; /// Upcasts to the template type. - fn into_template<'a>(self: Box) -> Box + 'a> + fn into_template<'a>(self: Box) -> Box where Self: 'a; } -pub trait IntoTemplate<'a, C> { - fn into_template(self) -> Box + 'a>; +pub trait IntoTemplate<'a> { + fn into_template(self) -> Box; } -impl + ?Sized> Template for &T { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { - >::format(self, context, formatter) +impl Template for &T { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { + ::format(self, formatter) } } -impl + ?Sized> Template for Box { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { - >::format(self, context, formatter) +impl Template for Box { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { + ::format(self, formatter) } } -impl Template<()> for Signature { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for Signature { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter.labeled("name"), "{}", self.name)?; if !self.name.is_empty() && !self.email.is_empty() { write!(formatter, " ")?; @@ -70,20 +70,20 @@ impl Template<()> for Signature { } } -impl Template<()> for String { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for String { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter, "{self}") } } -impl Template<()> for &str { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for &str { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter, "{self}") } } -impl Template<()> for Timestamp { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for Timestamp { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { match time_util::format_absolute_timestamp(self) { Ok(formatted) => write!(formatter, "{formatted}"), Err(err) => format_error_inline(formatter, &err), @@ -112,30 +112,30 @@ impl TimestampRange { } } -impl Template<()> for TimestampRange { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { - self.start.format(&(), formatter)?; +impl Template for TimestampRange { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { + self.start.format(formatter)?; write!(formatter, " - ")?; - self.end.format(&(), formatter)?; + self.end.format(formatter)?; Ok(()) } } -impl Template<()> for Vec { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { - format_joined(&(), formatter, self, " ") +impl Template for Vec { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { + format_joined(formatter, self, " ") } } -impl Template<()> for bool { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for bool { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { let repr = if *self { "true" } else { "false" }; write!(formatter, "{repr}") } } -impl Template<()> for i64 { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for i64 { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { write!(formatter, "{self}") } } @@ -146,29 +146,29 @@ pub struct LabelTemplate { } impl LabelTemplate { - pub fn new(content: T, labels: L) -> Self + pub fn new(content: T, labels: L) -> Self where - T: Template, + T: Template, L: TemplateProperty>, { LabelTemplate { content, labels } } } -impl Template for LabelTemplate +impl Template for LabelTemplate where - T: Template, + T: Template, L: TemplateProperty>, { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { let labels = match self.labels.extract() { Ok(labels) => labels, - Err(err) => return err.format(&(), formatter), + Err(err) => return err.format(formatter), }; for label in &labels { formatter.push_label(label)?; } - self.content.format(context, formatter)?; + self.content.format(formatter)?; for _label in &labels { formatter.pop_label()?; } @@ -178,10 +178,10 @@ where pub struct ConcatTemplate(pub Vec); -impl> Template for ConcatTemplate { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for ConcatTemplate { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { for template in &self.0 { - template.format(context, formatter)? + template.format(formatter)? } Ok(()) } @@ -194,24 +194,24 @@ pub struct ReformatTemplate { } impl ReformatTemplate { - pub fn new(content: T, reformat: F) -> Self + pub fn new(content: T, reformat: F) -> Self where - T: Template, - F: Fn(&C, &mut dyn Formatter, &FormatRecorder) -> io::Result<()>, + T: Template, + F: Fn(&mut dyn Formatter, &FormatRecorder) -> io::Result<()>, { ReformatTemplate { content, reformat } } } -impl Template for ReformatTemplate +impl Template for ReformatTemplate where - T: Template, - F: Fn(&C, &mut dyn Formatter, &FormatRecorder) -> io::Result<()>, + T: Template, + F: Fn(&mut dyn Formatter, &FormatRecorder) -> io::Result<()>, { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { let mut recorder = FormatRecorder::new(); - self.content.format(context, &mut recorder)?; - (self.reformat)(context, formatter, &recorder) + self.content.format(&mut recorder)?; + (self.reformat)(formatter, &recorder) } } @@ -222,10 +222,10 @@ pub struct SeparateTemplate { } impl SeparateTemplate { - pub fn new(separator: S, contents: Vec) -> Self + pub fn new(separator: S, contents: Vec) -> Self where - S: Template, - T: Template, + S: Template, + T: Template, { SeparateTemplate { separator, @@ -234,18 +234,18 @@ impl SeparateTemplate { } } -impl Template for SeparateTemplate +impl Template for SeparateTemplate where - S: Template, - T: Template, + S: Template, + T: Template, { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { let mut content_recorders = self .contents .iter() .filter_map(|template| { let mut recorder = FormatRecorder::new(); - match template.format(context, &mut recorder) { + match template.format(&mut recorder) { Ok(()) if recorder.data().is_empty() => None, // omit empty content Ok(()) => Some(Ok(recorder)), Err(e) => Some(Err(e)), @@ -256,7 +256,7 @@ where recorder?.replay(formatter)?; } for recorder in content_recorders { - self.separator.format(context, formatter)?; + self.separator.format(formatter)?; recorder?.replay(formatter)?; } Ok(()) @@ -280,8 +280,8 @@ where } /// Prints the evaluation error as inline template output. -impl Template<()> for TemplatePropertyError { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { +impl Template for TemplatePropertyError { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { format_error_inline(formatter, &*self.0) } } @@ -355,12 +355,12 @@ pub trait TemplatePropertyExt: TemplateProperty { impl TemplatePropertyExt for P {} -/// Adapter to drop template context. +/// Adapter that wraps literal value in `TemplateProperty`. pub struct Literal(pub O); -impl> Template for Literal { - fn format(&self, _context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { - self.0.format(&(), formatter) +impl Template for Literal { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { + self.0.format(formatter) } } @@ -372,7 +372,7 @@ impl TemplateProperty for Literal { } } -/// Adapter to extract context-less template value from property for displaying. +/// Adapter to extract template value from property for displaying. pub struct FormattablePropertyTemplate

{ property: P, } @@ -381,30 +381,30 @@ impl

FormattablePropertyTemplate

{ pub fn new(property: P) -> Self where P: TemplateProperty, - P::Output: Template<()>, + P::Output: Template, { FormattablePropertyTemplate { property } } } -impl

Template<()> for FormattablePropertyTemplate

+impl

Template for FormattablePropertyTemplate

where P: TemplateProperty, - P::Output: Template<()>, + P::Output: Template, { - fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { match self.property.extract() { - Ok(template) => template.format(&(), formatter), - Err(err) => err.format(&(), formatter), + Ok(template) => template.format(formatter), + Err(err) => err.format(formatter), } } } -impl<'a, O> IntoTemplate<'a, ()> for Box + 'a> +impl<'a, O> IntoTemplate<'a> for Box + 'a> where - O: Template<()> + 'a, + O: Template + 'a, { - fn into_template(self) -> Box + 'a> { + fn into_template(self) -> Box { Box::new(FormattablePropertyTemplate::new(self)) } } @@ -420,13 +420,13 @@ impl PlainTextFormattedProperty { } } -impl> TemplateProperty for PlainTextFormattedProperty { +impl TemplateProperty for PlainTextFormattedProperty { type Output = String; fn extract(&self) -> Result { let mut output = vec![]; self.template - .format(&(), &mut PlainTextFormatter::new(&mut output)) + .format(&mut PlainTextFormatter::new(&mut output)) .expect("write() to PlainTextFormatter should never fail"); Ok(String::from_utf8(output).map_err(|err| err.utf8_error())?) } @@ -435,7 +435,6 @@ impl> TemplateProperty for PlainTextFormattedProperty { /// Renders template property of list type with the given separator. /// /// Each list item will be formatted by the given `format_item()` function. -/// The separator takes a context of type `C`. pub struct ListPropertyTemplate { property: P, separator: S, @@ -443,12 +442,12 @@ pub struct ListPropertyTemplate { } impl ListPropertyTemplate { - pub fn new(property: P, separator: S, format_item: F) -> Self + pub fn new(property: P, separator: S, format_item: F) -> Self where P: TemplateProperty, P::Output: IntoIterator, - S: Template, - F: Fn(&C, &mut dyn Formatter, O) -> io::Result<()>, + S: Template, + F: Fn(&mut dyn Formatter, O) -> io::Result<()>, { ListPropertyTemplate { property, @@ -458,39 +457,32 @@ impl ListPropertyTemplate { } } -impl Template for ListPropertyTemplate +impl Template for ListPropertyTemplate where P: TemplateProperty, P::Output: IntoIterator, - S: Template, - F: Fn(&C, &mut dyn Formatter, O) -> io::Result<()>, + S: Template, + F: Fn(&mut dyn Formatter, O) -> io::Result<()>, { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { let contents = match self.property.extract() { Ok(contents) => contents, - Err(err) => return err.format(&(), formatter), + Err(err) => return err.format(formatter), }; - format_joined_with( - context, - formatter, - contents, - &self.separator, - &self.format_item, - ) + format_joined_with(formatter, contents, &self.separator, &self.format_item) } } -impl ListTemplate for ListPropertyTemplate +impl ListTemplate for ListPropertyTemplate where P: TemplateProperty, P::Output: IntoIterator, - S: Template, - F: Fn(&C, &mut dyn Formatter, O) -> io::Result<()>, + S: Template, + F: Fn(&mut dyn Formatter, O) -> io::Result<()>, { - fn join<'a>(self: Box, separator: Box + 'a>) -> Box + 'a> + fn join<'a>(self: Box, separator: Box) -> Box where Self: 'a, - C: 'a, { // Once join()-ed, list-like API should be dropped. This is guaranteed by // the return type. @@ -501,7 +493,7 @@ where )) } - fn into_template<'a>(self: Box) -> Box + 'a> + fn into_template<'a>(self: Box) -> Box where Self: 'a, { @@ -516,11 +508,11 @@ pub struct ConditionalTemplate { } impl ConditionalTemplate { - pub fn new(condition: P, true_template: T, false_template: Option) -> Self + pub fn new(condition: P, true_template: T, false_template: Option) -> Self where P: TemplateProperty, - T: Template, - U: Template, + T: Template, + U: Template, { ConditionalTemplate { condition, @@ -530,21 +522,21 @@ impl ConditionalTemplate { } } -impl Template for ConditionalTemplate +impl Template for ConditionalTemplate where P: TemplateProperty, - T: Template, - U: Template, + T: Template, + U: Template, { - fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { + fn format(&self, formatter: &mut dyn Formatter) -> io::Result<()> { let condition = match self.condition.extract() { Ok(condition) => condition, - Err(err) => return err.format(&(), formatter), + Err(err) => return err.format(formatter), }; if condition { - self.true_template.format(context, formatter)?; + self.true_template.format(formatter)?; } else if let Some(false_template) = &self.false_template { - false_template.format(context, formatter)?; + false_template.format(formatter)?; } Ok(()) } @@ -630,12 +622,12 @@ impl TemplateProperty for PropertyPlaceholder { /// Adapter that renders compiled `template` with the `placeholder` value set. pub struct TemplateRenderer<'a, C> { - template: Box + 'a>, + template: Box, placeholder: PropertyPlaceholder, } impl<'a, C: Clone> TemplateRenderer<'a, C> { - pub fn new(template: Box + 'a>, placeholder: PropertyPlaceholder) -> Self { + pub fn new(template: Box, placeholder: PropertyPlaceholder) -> Self { TemplateRenderer { template, placeholder, @@ -644,32 +636,26 @@ impl<'a, C: Clone> TemplateRenderer<'a, C> { pub fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { self.placeholder - .with_value(context.clone(), || self.template.format(&(), formatter)) + .with_value(context.clone(), || self.template.format(formatter)) } } -pub fn format_joined( - context: &C, +pub fn format_joined( formatter: &mut dyn Formatter, contents: I, separator: S, ) -> io::Result<()> where I: IntoIterator, - I::Item: Template, - S: Template, + I::Item: Template, + S: Template, { - format_joined_with( - context, - formatter, - contents, - separator, - |context, formatter, item| item.format(context, formatter), - ) + format_joined_with(formatter, contents, separator, |formatter, item| { + item.format(formatter) + }) } -fn format_joined_with( - context: &C, +fn format_joined_with( formatter: &mut dyn Formatter, contents: I, separator: S, @@ -677,16 +663,16 @@ fn format_joined_with( ) -> io::Result<()> where I: IntoIterator, - S: Template, - F: FnMut(&C, &mut dyn Formatter, I::Item) -> io::Result<()>, + S: Template, + F: FnMut(&mut dyn Formatter, I::Item) -> io::Result<()>, { let mut contents_iter = contents.into_iter().fuse(); if let Some(item) = contents_iter.next() { - format_item(context, formatter, item)?; + format_item(formatter, item)?; } for item in contents_iter { - separator.format(context, formatter)?; - format_item(context, formatter, item)?; + separator.format(formatter)?; + format_item(formatter, item)?; } Ok(()) }