pub struct CompiledTemplates { /* private fields */ }
Expand description
CompiledTemplates is useful when you have a set of templates
that you will expand frequently in a tight loop.
Because the underlying crate returns only references to Template
s,
it is a covariant, self-referential structure that needs to be
constructed like this:
fn get_templates<'b>(
engine: &'b TemplateEngine
) -> anyhow::Result<TemplateList<'b>> {
let mut templates = vec![];
templates.push(engine.get_template("something")?);
Ok(templates)
}
let engine = TemplateEngine::new();
engine.add_template("something", "some text")?;
let compiled = CompiledTemplates::try_new(engine, |engine| {
get_templates(engine)
});
Implementations§
source§impl CompiledTemplates
impl CompiledTemplates
sourcepub fn new(
owner: TemplateEngine,
dependent_builder: impl for<'_q> FnOnce(&'_q TemplateEngine) -> TemplateList<'_q>,
) -> Self
pub fn new( owner: TemplateEngine, dependent_builder: impl for<'_q> FnOnce(&'_q TemplateEngine) -> TemplateList<'_q>, ) -> Self
Constructs a new self-referential struct.
The provided owner
will be moved into a heap allocated box.
Followed by construction of the dependent value, by calling
dependent_builder
with a shared reference to the owner that
remains valid for the lifetime of the constructed struct.
sourcepub fn try_new<Err>(
owner: TemplateEngine,
dependent_builder: impl for<'_q> FnOnce(&'_q TemplateEngine) -> Result<TemplateList<'_q>, Err>,
) -> Result<Self, Err>
pub fn try_new<Err>( owner: TemplateEngine, dependent_builder: impl for<'_q> FnOnce(&'_q TemplateEngine) -> Result<TemplateList<'_q>, Err>, ) -> Result<Self, Err>
Tries to create a new structure with a given dependent builder.
Consumes owner on error.
sourcepub fn try_new_or_recover<Err>(
owner: TemplateEngine,
dependent_builder: impl for<'_q> FnOnce(&'_q TemplateEngine) -> Result<TemplateList<'_q>, Err>,
) -> Result<Self, (TemplateEngine, Err)>
pub fn try_new_or_recover<Err>( owner: TemplateEngine, dependent_builder: impl for<'_q> FnOnce(&'_q TemplateEngine) -> Result<TemplateList<'_q>, Err>, ) -> Result<Self, (TemplateEngine, Err)>
Tries to create a new structure with a given dependent builder.
Returns owner on error.
sourcepub fn borrow_owner<'_q>(&'_q self) -> &'_q TemplateEngine
pub fn borrow_owner<'_q>(&'_q self) -> &'_q TemplateEngine
Borrows owner.
sourcepub fn with_dependent<'outer_fn, Ret>(
&'outer_fn self,
func: impl for<'_q> FnOnce(&'_q TemplateEngine, &'outer_fn TemplateList<'_q>) -> Ret,
) -> Ret
pub fn with_dependent<'outer_fn, Ret>( &'outer_fn self, func: impl for<'_q> FnOnce(&'_q TemplateEngine, &'outer_fn TemplateList<'_q>) -> Ret, ) -> Ret
Calls given closure func
with a shared reference to dependent.
sourcepub fn with_dependent_mut<'outer_fn, Ret>(
&'outer_fn mut self,
func: impl for<'_q> FnOnce(&'_q TemplateEngine, &'outer_fn mut TemplateList<'_q>) -> Ret,
) -> Ret
pub fn with_dependent_mut<'outer_fn, Ret>( &'outer_fn mut self, func: impl for<'_q> FnOnce(&'_q TemplateEngine, &'outer_fn mut TemplateList<'_q>) -> Ret, ) -> Ret
Calls given closure func
with an unique reference to dependent.
sourcepub fn borrow_dependent<'_q>(&'_q self) -> &'_q TemplateList<'_q>
pub fn borrow_dependent<'_q>(&'_q self) -> &'_q TemplateList<'_q>
Borrows dependent.
sourcepub fn into_owner(self) -> TemplateEngine
pub fn into_owner(self) -> TemplateEngine
Consumes self
and returns the the owner.