Update the new_breadcrumbs function to return an Option to prevent needlessly cloning the breadcrumbs when no new contexts are to be added.

This commit is contained in:
Tom Alexander 2020-05-25 15:49:30 -04:00
parent 4a21ae5af3
commit d79447e602
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -163,9 +163,15 @@ impl<'a> DustRenderer<'a> {
let val = walk_path(breadcrumbs, &container.path.keys); let val = walk_path(breadcrumbs, &container.path.keys);
match val { match val {
Err(WalkError::CantWalk) => { Err(WalkError::CantWalk) => {
let new_breadcrumbs = Self::new_breadcrumbs(
breadcrumbs,
None,
&container.explicit_context,
None,
);
return self.render_maybe_body( return self.render_maybe_body(
&container.else_contents, &container.else_contents,
breadcrumbs, new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks, blocks,
); );
} }
@ -591,7 +597,14 @@ impl<'a> DustRenderer<'a> {
injected_context: Option<&'b dyn ContextElement>, injected_context: Option<&'b dyn ContextElement>,
explicit_context: &Option<Path<'b>>, explicit_context: &Option<Path<'b>>,
new_context_element: Option<&'b dyn ContextElement>, new_context_element: Option<&'b dyn ContextElement>,
) -> Vec<&'b dyn ContextElement> { ) -> Option<Vec<&'b dyn ContextElement>> {
// If none of the additional contexts are present, return None
// to signal that the original breadcrumbs should be used
// rather than incurring a copy here.
match (injected_context, explicit_context, new_context_element) {
(None, None, None) => return None,
_ => (),
};
let mut new_stack = match explicit_context { let mut new_stack = match explicit_context {
Some(_) => Vec::with_capacity(3), Some(_) => Vec::with_capacity(3),
None => breadcrumbs.clone(), None => breadcrumbs.clone(),
@ -605,7 +618,7 @@ impl<'a> DustRenderer<'a> {
}); });
}); });
new_context_element.map(|ctx| new_stack.push(ctx)); new_context_element.map(|ctx| new_stack.push(ctx));
new_stack Some(new_stack)
} }
} }