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§
sourcefn mapped_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
fn mapped_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
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))
})
}
}
sourcefn mapped_rev_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
fn mapped_rev_enumerator<F>(self: &Arc<Self>, maker: F) -> Enumerator
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))
})
}
}
Object Safety§
This trait is not object safe.