Enum minijinja::value::Enumerator

source ·
#[non_exhaustive]
pub enum Enumerator { NonEnumerable, Empty, Str(&'static [&'static str]), Iter(Box<dyn Iterator<Item = Value> + Send + Sync>), RevIter(Box<dyn DoubleEndedIterator<Item = Value> + Send + Sync>), Seq(usize), Values(Vec<Value>), }
Expand description

Enumerators help define iteration behavior for Objects.

When Jinja wants to know the length of an object, if it’s empty or not or if it wants to iterate over it, it will ask the Object to enumerate itself with the enumerate method. The returned enumerator has enough information so that the object can be iterated over, but it does not necessarily mean that iteration actually starts or that it has the data to yield the right values.

In fact, you should never inspect an enumerator. You can create it or forward it. For actual iteration use ObjectExt::try_iter etc.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

NonEnumerable

Marks non enumerable objects.

Such objects cannot be iterated over, the length is unknown which means they are not considered empty by the engine. This is a good choice for plain objects.

IterableLength
nounknown
§

Empty

The empty enumerator. It yields no elements.

IterableLength
yesknown (0)
§

Str(&'static [&'static str])

A slice of static strings.

This is a useful enumerator to enumerate the attributes of an object or the keys in a string hash map.

IterableLength
yesknown
§

Iter(Box<dyn Iterator<Item = Value> + Send + Sync>)

A dynamic iterator over values.

The length is known if the Iterator::size_hint has matching lower and upper bounds. The logic used by the engine is the following:

let len = match iter.size_hint() {
    (lower, Some(upper)) if lower == upper => Some(lower),
    _ => None
};

Because the engine prefers repeatable iteration, it will keep creating new enumerators every time the iteration should restart. Sometimes that might not always be possible (eg: you stream data in) in which case

IterableLength
yessometimes known
§

RevIter(Box<dyn DoubleEndedIterator<Item = Value> + Send + Sync>)

Like Iter but supports efficient reversing.

This means that the iterator has to be of type DoubleEndedIterator.

IterableLength
yessometimes known
§

Seq(usize)

Indicates sequential iteration.

This instructs the engine to iterate over an object by enumerating it from 0 to n by calling Object::get_value. This is essentially the way sequences are supposed to be enumerated.

IterableLength
yesknown
§

Values(Vec<Value>)

A vector of known values to iterate over.

The iterator will yield each value in the vector one after another.

IterableLength
yesknown

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

§

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

§

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.