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§
sourcefn get_item(&self, idx: usize) -> Option<Value>
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.
sourcefn item_count(&self) -> usize
fn item_count(&self) -> usize
Returns the number of items in the sequence.