Trait rocket_dyn_templates::minijinja::value::SeqObject

source ·
pub trait SeqObject: Send + Sync {
    // Required methods
    fn get_item(&self, idx: usize) -> Option<Value>;
    fn item_count(&self) -> usize;
}
Expand description

Provides the behavior of an Object holding sequence of values.

An object holding a sequence of values (tuple, list etc.) can be represented by this trait.

§Simplified Example

For sequences which do not need any special method behavior, the Value type is capable of automatically constructing a wrapper Object by using Value::from_seq_object. In that case only SeqObject needs to be implemented and the value will provide default implementations for stringification and debug printing.

use minijinja::value::{Value, SeqObject};

struct Point(f32, f32, f32);

impl SeqObject for Point {
    fn get_item(&self, idx: usize) -> Option<Value> {
        match idx {
            0 => Some(Value::from(self.0)),
            1 => Some(Value::from(self.1)),
            2 => Some(Value::from(self.2)),
            _ => None,
        }
    }

    fn item_count(&self) -> usize {
        3
    }
}

let value = Value::from_seq_object(Point(1.0, 2.5, 3.0));

§Full Example

This example shows how one can use SeqObject in conjunction with a fully customized Object. Note that in this case not only Object needs to be implemented, but also Debug and Display no longer come for free.

use std::fmt;
use minijinja::value::{Value, Object, ObjectKind, SeqObject};

#[derive(Debug, Clone)]
struct Point(f32, f32, f32);

impl fmt::Display for Point {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {}, {})", self.0, self.1, self.2)
    }
}

impl Object for Point {
    fn kind(&self) -> ObjectKind<'_> {
        ObjectKind::Seq(self)
    }
}

impl SeqObject for Point {
    fn get_item(&self, idx: usize) -> Option<Value> {
        match idx {
            0 => Some(Value::from(self.0)),
            1 => Some(Value::from(self.1)),
            2 => Some(Value::from(self.2)),
            _ => None,
        }
    }

    fn item_count(&self) -> usize {
        3
    }
}

let value = Value::from_object(Point(1.0, 2.5, 3.0));

Required Methods§

source

fn get_item(&self, idx: usize) -> Option<Value>

Looks up an item by index.

Sequences should provide a value for all items in the range of 0..item_count but the engine will assume that items within the range are Undefined if None is returned.

source

fn item_count(&self) -> usize

Returns the number of items in the sequence.

Implementations§

source§

impl dyn SeqObject + '_

source

pub fn iter(&self) -> SeqObjectIter<'_>

Convenient iterator over a SeqObject.

Trait Implementations§

source§

impl<'a> ArgType<'a> for &dyn SeqObject

§

type Output = &'a dyn SeqObject

The output type of this argument.

Implementations on Foreign Types§

source§

impl<'a, T> SeqObject for &'a T
where T: SeqObject + ?Sized,

source§

fn get_item(&self, idx: usize) -> Option<Value>

source§

fn item_count(&self) -> usize

source§

impl<T> SeqObject for [T]
where T: Into<Value> + Send + Sync + Clone,

source§

fn get_item(&self, idx: usize) -> Option<Value>

source§

fn item_count(&self) -> usize

source§

impl<T> SeqObject for Arc<T>
where T: SeqObject,

source§

fn get_item(&self, idx: usize) -> Option<Value>

source§

fn item_count(&self) -> usize

source§

impl<T> SeqObject for Vec<T>
where T: Into<Value> + Send + Sync + Clone,

source§

fn get_item(&self, idx: usize) -> Option<Value>

source§

fn item_count(&self) -> usize

Implementors§