Create from_borrowed and from_owned constructors for IceResult and BreadcrumbTreeElement.

In an effort to keep track of how often memory is getting heap allocated, I am implementing constructors for IceResult and BreadcrumbTreeElement. This should limit the places that I call Rc::new and allow me to place tracing code into it later to ensure all code paths doing heap allocation make sense.
This commit is contained in:
Tom Alexander
2020-06-06 20:24:01 -04:00
parent 3c15e35b67
commit 02259b9bd6
4 changed files with 41 additions and 17 deletions

View File

@@ -20,13 +20,25 @@ pub enum BreadcrumbTreeElement<'a> {
Borrowed(&'a dyn IntoContextElement),
}
impl<'a> BreadcrumbTreeElement<'a> {
pub fn from_owned<I: 'a + IntoContextElement>(val: I) -> BreadcrumbTreeElement<'a> {
BreadcrumbTreeElement::Owned(Rc::new(val))
}
pub fn from_borrowed(val: &'a dyn IntoContextElement) -> BreadcrumbTreeElement<'a> {
BreadcrumbTreeElement::Borrowed(val)
}
}
impl<'a> From<&'a IceResult<'a>> for BreadcrumbTreeElement<'a> {
fn from(inp: &'a IceResult<'a>) -> Self {
match inp {
IceResult::Owned(rc_ce) => {
BreadcrumbTreeElement::Borrowed(rc_ce.from_context_element())
BreadcrumbTreeElement::from_borrowed(rc_ce.from_context_element())
}
IceResult::Borrowed(ce) => {
BreadcrumbTreeElement::from_borrowed(ce.from_context_element())
}
IceResult::Borrowed(ce) => BreadcrumbTreeElement::Borrowed(ce.from_context_element()),
}
}
}
@@ -35,7 +47,9 @@ impl<'a> From<IceResult<'a>> for BreadcrumbTreeElement<'a> {
fn from(inp: IceResult<'a>) -> Self {
match inp {
IceResult::Owned(rc_ce) => BreadcrumbTreeElement::Owned(rc_ce.into_rc_ice()),
IceResult::Borrowed(ce) => BreadcrumbTreeElement::Borrowed(ce.from_context_element()),
IceResult::Borrowed(ce) => {
BreadcrumbTreeElement::from_borrowed(ce.from_context_element())
}
}
}
}