From f632b186ef8ac8b1f8b3c7d6f11558a762e91283 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 8 Nov 2022 16:08:07 +0100 Subject: [PATCH] Change the Object::attributes method to return an iterator --- minijinja/src/value/mod.rs | 16 +++++++--------- minijinja/src/value/object.rs | 6 +++--- minijinja/src/vm/loop_object.rs | 27 +++++++++++++++------------ minijinja/src/vm/macro_object.rs | 4 ++-- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/minijinja/src/value/mod.rs b/minijinja/src/value/mod.rs index c063d5ac..689bc6e1 100644 --- a/minijinja/src/value/mod.rs +++ b/minijinja/src/value/mod.rs @@ -552,7 +552,7 @@ impl Value { ValueRepr::String(ref s, _) => Some(s.chars().count()), ValueRepr::Map(ref items, _) => Some(items.len()), ValueRepr::Seq(ref items) => Some(items.len()), - ValueRepr::Dynamic(ref dy) => Some(dy.attributes().len()), + ValueRepr::Dynamic(ref dy) => Some(dy.attributes().count()), _ => None, } } @@ -796,8 +796,7 @@ impl Value { ) as Box>, ValueRepr::Dynamic(ref obj) => Box::new( obj.attributes() - .iter() - .filter_map(move |attr| Some((*attr, some!(obj.get_attr(attr))))), + .filter_map(move |attr| Some((attr, some!(obj.get_attr(attr))))), ) as Box>, _ => Box::new(None.into_iter()) as Box>, } @@ -869,11 +868,10 @@ impl Serialize for Value { } ValueRepr::Dynamic(ref n) => { use serde::ser::SerializeMap; - let fields = n.attributes(); - let mut s = ok!(serializer.serialize_map(Some(fields.len()))); - for k in fields { + let mut s = ok!(serializer.serialize_map(None)); + for k in n.attributes() { let v = n.get_attr(k).unwrap_or(Value::UNDEFINED); - ok!(s.serialize_entry(k, &v)); + ok!(s.serialize_entry(&k, &v)); } s.end() } @@ -983,8 +981,8 @@ fn test_dynamic_object_roundtrip() { } } - fn attributes(&self) -> &'static [&'static str] { - &["value"] + fn attributes(&self) -> Box + '_> { + Box::new(["value"].into_iter()) } } diff --git a/minijinja/src/value/object.rs b/minijinja/src/value/object.rs index e921b7fe..92364fa3 100644 --- a/minijinja/src/value/object.rs +++ b/minijinja/src/value/object.rs @@ -36,12 +36,12 @@ pub trait Object: fmt::Display + fmt::Debug + Any + Sync + Send { /// An enumeration of attributes that are known to exist on this object. /// - /// The default implementation returns an empty slice. If it's not possible + /// The default implementation returns an empty iterator. If it's not possible /// to implement this, it's fine for the implementation to be omitted. The /// enumeration here is used by the `for` loop to iterate over the attributes /// on the value. - fn attributes(&self) -> &[&str] { - &[][..] + fn attributes(&self) -> Box + '_> { + Box::new(None.into_iter()) } /// Called when the engine tries to call a method on the object. diff --git a/minijinja/src/vm/loop_object.rs b/minijinja/src/vm/loop_object.rs index 6e5390f1..95cfd4cb 100644 --- a/minijinja/src/vm/loop_object.rs +++ b/minijinja/src/vm/loop_object.rs @@ -24,18 +24,21 @@ impl fmt::Debug for Loop { } impl Object for Loop { - fn attributes(&self) -> &[&str] { - &[ - "index0", - "index", - "length", - "revindex", - "revindex0", - "first", - "last", - "depth", - "depth0", - ][..] + fn attributes(&self) -> Box + '_> { + Box::new( + [ + "index0", + "index", + "length", + "revindex", + "revindex0", + "first", + "last", + "depth", + "depth0", + ] + .into_iter(), + ) } fn get_attr(&self, name: &str) -> Option { diff --git a/minijinja/src/vm/macro_object.rs b/minijinja/src/vm/macro_object.rs index d99b9474..0735464a 100644 --- a/minijinja/src/vm/macro_object.rs +++ b/minijinja/src/vm/macro_object.rs @@ -41,8 +41,8 @@ impl fmt::Display for Macro { } impl Object for Macro { - fn attributes(&self) -> &[&str] { - &["name", "arguments"][..] + fn attributes(&self) -> Box + '_> { + Box::new(["name", "arguments"].into_iter()) } fn get_attr(&self, name: &str) -> Option {