Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Light cloning of templates #413

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn no_escape(data: &str) -> String {
/// It maintains compiled templates and registered helpers.
#[derive(Clone)]
pub struct Registry<'reg> {
templates: HashMap<String, Template>,
templates: HashMap<String, Arc<Template>>,

helpers: HashMap<String, Arc<dyn HelperDef + Send + Sync + 'reg>>,
decorators: HashMap<String, Arc<dyn DecoratorDef + Send + Sync + 'reg>>,
Expand Down Expand Up @@ -196,7 +196,7 @@ impl<'reg> Registry<'reg> {
/// insert cannot fail. If there is an existing template with this name it
/// will be overwritten.
pub fn register_template(&mut self, name: &str, tpl: Template) {
self.templates.insert(name.to_string(), tpl);
self.templates.insert(name.to_string(), Arc::new(tpl));
}

/// Register a template string
Expand Down Expand Up @@ -398,7 +398,7 @@ impl<'reg> Registry<'reg> {

/// Return a registered template,
pub fn get_template(&self, name: &str) -> Option<&Template> {
self.templates.get(name)
self.templates.get(name).map(|x| &**x)
}

#[inline]
Expand All @@ -413,6 +413,7 @@ impl<'reg> Registry<'reg> {
} else {
self.templates
.get(name)
.map(|x| &**x)
.map(Cow::Borrowed)
.ok_or_else(|| RenderError::new(format!("Template not found: {}", name)))
}
Expand Down Expand Up @@ -454,7 +455,7 @@ impl<'reg> Registry<'reg> {
}

/// Return all templates registered
pub fn get_templates(&self) -> &HashMap<String, Template> {
pub fn get_templates(&self) -> &HashMap<String, Arc<Template>> {
&self.templates
}

Expand Down