# \[Rust\] Handle lifetimes with Traits in Tera

**URL:** <https://zola.discourse.group/t/rust-handle-lifetimes-with-traits-in-tera/52>\
**Category:** Uncategorized\
**Created:** [January 17, 2019, 6:46pm UTC](https://zola.discourse.group/t/rust-handle-lifetimes-with-traits-in-tera/52 "2019-01-17T18:46:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [January 17, 2019, 6:46pm UTC](https://zola.discourse.group/t/rust-handle-lifetimes-with-traits-in-tera/52/1 "2019-01-17T18:46:33Z")

</div>

The reason Zola is using so much memory is that the Tera functions get\_page/get\_section/get\_taxonomy clone all the pages/sections to be able to use them in templates.

The next version of Tera (1.0.0) will turn function/filters/tests into traits rather then Fn types. For example the global function trait is:

```auto
/// The global function type definition
pub trait Function: Sync + Send {
    /// The global function type definition
    fn call(&self, args: &HashMap<String, Value>) -> Result<Value>;
}

```

That’s nicer than a user POV but we still need to clone everything.  
My thoughts were to have a lifetime referencing the Tera instance that binds the traits like so:

```auto
pub struct Tera<'tera> {
    #[doc(hidden)]
    pub functions: HashMap<String, Arc<dyn Function + 'tera>>,
}

```

I made an example of what should be working ideally: [https://github.com/Keats/tera/blob/lifetime-issue/examples/borrowed\_trait\_context/main.rs](https://github.com/Keats/tera/blob/lifetime-issue/examples/borrowed_trait_context/main.rs) but you end up having lifetime issues all around.

Does anyone has a good idea on that before I ask on users.rustlang?
