Going to try switching to boxed owned values to remove the template.

This commit is contained in:
Tom Alexander 2020-05-31 17:25:41 -04:00
parent 92dca74505
commit 2b532e7eb4
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 52 additions and 58 deletions

View File

@ -7,17 +7,17 @@ pub trait BreadcrumbTree {
fn get_parent(&self) -> Option<&dyn BreadcrumbTree>; fn get_parent(&self) -> Option<&dyn BreadcrumbTree>;
} }
pub struct BreadcrumbTreeNode<'a, C: IntoContextElement> { pub struct BreadcrumbTreeNode<'a> {
parent: Option<&'a dyn BreadcrumbTree>, parent: Option<&'a dyn BreadcrumbTree>,
element: BreadcrumbTreeNodeElement<'a, C>, element: BreadcrumbTreeNodeElement<'a>,
} }
pub enum BreadcrumbTreeNodeElement<'a, C: IntoContextElement> { pub enum BreadcrumbTreeNodeElement<'a> {
Owned(C), Owned(Box<dyn IntoContextElement>),
Borrowed(&'a C), Borrowed(&'a dyn IntoContextElement),
} }
impl<'a, C: IntoContextElement> BreadcrumbTreeNode<'a, C> { impl<'a> BreadcrumbTreeNode<'a> {
pub fn ice_iter(&'a self) -> impl Iterator<Item = &dyn IntoContextElement> { pub fn ice_iter(&'a self) -> impl Iterator<Item = &dyn IntoContextElement> {
self.breadcrumb_iter().map(|b| b.get_ice()) self.breadcrumb_iter().map(|b| b.get_ice())
} }
@ -27,18 +27,16 @@ impl<'a, C: IntoContextElement> BreadcrumbTreeNode<'a, C> {
} }
} }
impl<'a, C: IntoContextElement> Borrow<dyn IntoContextElement + 'a> impl<'a> Borrow<dyn IntoContextElement + 'a> for BreadcrumbTreeNodeElement<'a> {
for BreadcrumbTreeNodeElement<'a, C>
{
fn borrow(&self) -> &(dyn IntoContextElement + 'a) { fn borrow(&self) -> &(dyn IntoContextElement + 'a) {
match self { match self {
BreadcrumbTreeNodeElement::Owned(ice) => ice, BreadcrumbTreeNodeElement::Owned(ice) => ice.as_ref(),
BreadcrumbTreeNodeElement::Borrowed(ice) => *ice, BreadcrumbTreeNodeElement::Borrowed(ice) => *ice,
} }
} }
} }
impl<'a, C: IntoContextElement> BreadcrumbTree for BreadcrumbTreeNode<'a, C> { impl<'a> BreadcrumbTree for BreadcrumbTreeNode<'a> {
fn get_ice(&self) -> &dyn IntoContextElement { fn get_ice(&self) -> &dyn IntoContextElement {
self.element.borrow() self.element.borrow()
} }
@ -48,7 +46,7 @@ impl<'a, C: IntoContextElement> BreadcrumbTree for BreadcrumbTreeNode<'a, C> {
} }
} }
impl<'a, C: IntoContextElement> IntoIterator for &'a BreadcrumbTreeNode<'a, C> { impl<'a> IntoIterator for &'a BreadcrumbTreeNode<'a> {
type Item = &'a dyn BreadcrumbTree; type Item = &'a dyn BreadcrumbTree;
type IntoIter = BreadcrumbTreeIterator<'a>; type IntoIter = BreadcrumbTreeIterator<'a>;

View File

@ -44,44 +44,44 @@ impl<'a> DustRenderer<'a> {
.insert(template.name.clone(), &template.template); .insert(template.name.clone(), &template.template);
} }
fn new_breadcrumbs_section<'b, B>( // fn new_breadcrumbs_section<'b, B>(
&self, // &self,
maybe_breadcrumbs: Option<&'a BreadcrumbTreeNode<B>>, // maybe_breadcrumbs: Option<&'a BreadcrumbTreeNode<B>>,
index_context: Option<&'b dyn IntoContextElement>, // index_context: Option<&'b dyn IntoContextElement>,
injected_context: Option<&'b dyn IntoContextElement>, // injected_context: Option<&'b dyn IntoContextElement>,
explicit_context: &Option<Path<'b>>, // explicit_context: &Option<Path<'b>>,
new_context_element: Option<&'b dyn ContextElement>, // new_context_element: Option<&'b dyn ContextElement>,
) -> Option<&'a BreadcrumbTreeNode<B>> // ) -> Option<&'a BreadcrumbTreeNode<B>>
where // where
B: IntoContextElement, // B: IntoContextElement,
{ // {
// If there is no new content, return the original breadcrumbs // // If there is no new content, return the original breadcrumbs
match ( // match (
index_context, // index_context,
injected_context, // injected_context,
explicit_context, // explicit_context,
new_context_element, // new_context_element,
) { // ) {
(None, None, None, None) => return maybe_breadcrumbs, // (None, None, None, None) => return maybe_breadcrumbs,
_ => (), // _ => (),
} // }
// If there is an explicit context, then drop all the current // // If there is an explicit context, then drop all the current
// context // // context
let mut new_stack = match explicit_context { // let mut new_stack = match explicit_context {
Some(_) => None, // Some(_) => None,
None => maybe_breadcrumbs, // None => maybe_breadcrumbs,
}; // };
// TODO: Explicit context // // TODO: Explicit context
injected_context.map(|ctx| { // // injected_context.map(|ctx| {
new_stack = Some(BreadcrumbTreeNode { // // new_stack = Some(BreadcrumbTreeNode {
parent: new_stack.map(|b| b as _), // // parent: new_stack.map(|b| b as _),
element: BreadcrumbTreeNodeElement::Borrowed(ctx), // // element: BreadcrumbTreeNodeElement::Borrowed(ctx),
}); // // });
// TODO // // // TODO
}); // // });
None // None
} // }
} }

View File

@ -38,24 +38,20 @@ where
WalkResult::FullyWalked(output) WalkResult::FullyWalked(output)
} }
fn get_first_non_pseudo_element<'a, B>( fn get_first_non_pseudo_element<'a>(
breadcrumbs: &'a BreadcrumbTreeNode<B>, breadcrumbs: &'a BreadcrumbTreeNode,
) -> Option<&'a dyn BreadcrumbTree> ) -> Option<&'a dyn BreadcrumbTree> {
where
B: IntoContextElement,
{
breadcrumbs breadcrumbs
.breadcrumb_iter() .breadcrumb_iter()
.filter(|b| b.get_ice().is_pseudo_element()) .filter(|b| b.get_ice().is_pseudo_element())
.next() .next()
} }
pub fn walk_path<'a, B, P>( pub fn walk_path<'a, P>(
maybe_breadcrumbs: Option<&'a BreadcrumbTreeNode<B>>, maybe_breadcrumbs: Option<&'a BreadcrumbTreeNode>,
path: &Vec<P>, path: &Vec<P>,
) -> Result<&'a dyn IntoContextElement, WalkError> ) -> Result<&'a dyn IntoContextElement, WalkError>
where where
B: IntoContextElement,
P: Borrow<str>, P: Borrow<str>,
{ {
match (maybe_breadcrumbs, path.first()) { match (maybe_breadcrumbs, path.first()) {