kumo_template

Struct CompiledTemplates

source
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 Templates, 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

source

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.

source

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.

source

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.

source

pub fn borrow_owner<'_q>(&'_q self) -> &'_q TemplateEngine

Borrows owner.

source

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.

source

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.

source

pub fn borrow_dependent<'_q>(&'_q self) -> &'_q TemplateList<'_q>

Borrows dependent.

source

pub fn into_owner(self) -> TemplateEngine

Consumes self and returns the the owner.

Trait Implementations§

source§

impl Drop for CompiledTemplates

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.