Trait minijinja::value::ObjectExt

source ·
pub trait ObjectExt: Object + Send + Sync + 'static {
    // Provided methods
    fn mapped_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
       where F: for<'a> FnOnce(&'a Self) -> Box<dyn Iterator<Item = Value> + Send + Sync + 'a> + Send + Sync + 'static,
             Self: Sized { ... }
    fn mapped_rev_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
       where F: for<'a> FnOnce(&'a Self) -> Box<dyn DoubleEndedIterator<Item = Value> + Send + Sync + 'a> + Send + Sync + 'static,
             Self: Sized { ... }
    fn try_iter(
        self: &Arc<Self>,
    ) -> Option<Box<dyn Iterator<Item = Value> + Send + Sync>>
       where Self: 'static { ... }
    fn try_iter_pairs(
        self: &Arc<Self>,
    ) -> Option<Box<dyn Iterator<Item = (Value, Value)> + Send + Sync>> { ... }
}
Expand description

Provides utility methods for working with objects.

Provided Methods§

source

fn mapped_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
where F: for<'a> FnOnce(&'a Self) -> Box<dyn Iterator<Item = Value> + Send + Sync + 'a> + Send + Sync + 'static, Self: Sized,

Creates a new iterator enumeration that projects into the given object.

It takes a method that is passed a reference to self and is expected to return an Iterator. This iterator is then wrapped in an Enumerator::Iter. This allows one to create an iterator that borrows out of the object.

§Example
use std::sync::Arc;
use minijinja::value::{Value, Object, ObjectExt, Enumerator};

#[derive(Debug)]
struct CustomMap(HashMap<usize, i64>);

impl Object for CustomMap {
    fn get_value(self: &Arc<Self>, key: &Value) -> Option<Value> {
        self.0.get(&key.as_usize()?).copied().map(Value::from)
    }

    fn enumerate(self: &Arc<Self>) -> Enumerator {
        self.mapped_enumerator(|this| {
            Box::new(this.0.keys().copied().map(Value::from))
        })
    }
}
source

fn mapped_rev_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
where F: for<'a> FnOnce(&'a Self) -> Box<dyn DoubleEndedIterator<Item = Value> + Send + Sync + 'a> + Send + Sync + 'static, Self: Sized,

Creates a new reversible iterator enumeration that projects into the given object.

It takes a method that is passed a reference to self and is expected to return a DoubleEndedIterator. This iterator is then wrapped in an Enumerator::RevIter. This allows one to create an iterator that borrows out of the object and is reversible.

§Example
use std::sync::Arc;
use std::ops::Range;
use minijinja::value::{Value, Object, ObjectExt, ObjectRepr, Enumerator};

#[derive(Debug)]
struct VecView(Vec<usize>);

impl Object for VecView {
    fn repr(self: &Arc<Self>) -> ObjectRepr {
        ObjectRepr::Iterable
    }

    fn enumerate(self: &Arc<Self>) -> Enumerator {
        self.mapped_enumerator(|this| {
            Box::new(this.0.iter().cloned().map(Value::from))
        })
    }
}
source

fn try_iter( self: &Arc<Self>, ) -> Option<Box<dyn Iterator<Item = Value> + Send + Sync>>
where Self: 'static,

Iterates over this object.

If this returns None then the default object iteration as defined by the object’s enumeration is used.

source

fn try_iter_pairs( self: &Arc<Self>, ) -> Option<Box<dyn Iterator<Item = (Value, Value)> + Send + Sync>>

Iterate over key and value at once.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: Object + Send + Sync + 'static> ObjectExt for T