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

master
Tom Alexander 4 years ago
parent 4a21ae5af3
commit d79447e602
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -163,9 +163,15 @@ impl<'a> DustRenderer<'a> {
let val = walk_path(breadcrumbs, &container.path.keys);
match val {
Err(WalkError::CantWalk) => {
let new_breadcrumbs = Self::new_breadcrumbs(
breadcrumbs,
None,
&container.explicit_context,
None,
);
return self.render_maybe_body(
&container.else_contents,
breadcrumbs,
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
blocks,
);
}
@ -591,7 +597,14 @@ impl<'a> DustRenderer<'a> {
injected_context: Option<&'b dyn ContextElement>,
explicit_context: &Option<Path<'b>>,
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 {
Some(_) => Vec::with_capacity(3),
None => breadcrumbs.clone(),
@ -605,7 +618,7 @@ impl<'a> DustRenderer<'a> {
});
});
new_context_element.map(|ctx| new_stack.push(ctx));
new_stack
Some(new_stack)
}
}

Loading…
Cancel
Save