Skip to content

Commit

Permalink
Better names for counts on maps and sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Nov 19, 2022
1 parent 4271a73 commit 7f994a0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
21 changes: 11 additions & 10 deletions minijinja/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ impl Value {
ValueRepr::Map(ref x, _) => !x.is_empty(),
ValueRepr::Dynamic(ref x) => match x.kind() {
ObjectKind::Basic => true,
ObjectKind::Seq(s) => s.seq_len() != 0,
ObjectKind::Struct(s) => s.struct_size() != 0,
ObjectKind::Seq(s) => s.item_count() != 0,
ObjectKind::Struct(s) => s.field_count() != 0,
},
}
}
Expand Down Expand Up @@ -652,8 +652,8 @@ impl Value {
ValueRepr::Seq(ref items) => Some(items.len()),
ValueRepr::Dynamic(ref dy) => match dy.kind() {
ObjectKind::Basic => None,
ObjectKind::Seq(s) => Some(s.seq_len()),
ObjectKind::Struct(s) => Some(s.struct_size()),
ObjectKind::Seq(s) => Some(s.item_count()),
ObjectKind::Struct(s) => Some(s.field_count()),
},
_ => None,
}
Expand Down Expand Up @@ -821,7 +821,7 @@ impl Value {
if let Key::I64(idx) = key {
let idx = some!(isize::try_from(idx).ok());
let idx = if idx < 0 {
some!(seq.seq_len().checked_sub(-idx as usize))
some!(seq.item_count().checked_sub(-idx as usize))
} else {
idx as usize
};
Expand Down Expand Up @@ -932,15 +932,16 @@ impl Value {
ValueRepr::Dynamic(ref obj) => {
match obj.kind() {
ObjectKind::Basic => (ValueIteratorState::Empty, 0),
ObjectKind::Seq(s) => {
(ValueIteratorState::DynSeq(0, Arc::clone(obj)), s.seq_len())
}
ObjectKind::Seq(s) => (
ValueIteratorState::DynSeq(0, Arc::clone(obj)),
s.item_count(),
),
ObjectKind::Struct(s) => {
// the assumption is that structs don't have excessive field counts
// and that most iterations go over all fields, so creating a
// temporary vector here is acceptable.
let attrs = s.fields().map(Value::from).collect::<Vec<_>>();
let attr_count = s.struct_size();
let attr_count = s.field_count();
(ValueIteratorState::Seq(0, Arc::new(attrs)), attr_count)
}
}
Expand Down Expand Up @@ -996,7 +997,7 @@ impl Serialize for Value {
ObjectKind::Basic => serializer.serialize_str(&dy.to_string()),
ObjectKind::Seq(s) => {
use serde::ser::SerializeSeq;
let mut seq = ok!(serializer.serialize_seq(Some(s.seq_len())));
let mut seq = ok!(serializer.serialize_seq(Some(s.item_count())));
for item in s.iter() {
ok!(seq.serialize_element(&item));
}
Expand Down
14 changes: 7 additions & 7 deletions minijinja/src/value/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub enum ObjectKind<'a> {
/// }
/// }
///
/// fn seq_len(&self) -> usize {
/// fn item_count(&self) -> usize {
/// 3
/// }
/// }
Expand All @@ -166,21 +166,21 @@ pub enum ObjectKind<'a> {
pub trait SeqObject {
/// Looks up an item by index.
///
/// Sequences should provide a value for all items in the range of `0..seq_len`
/// 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.
fn get_item(&self, idx: usize) -> Option<Value>;

/// Returns the number of items in the sequence.
fn seq_len(&self) -> usize;
fn item_count(&self) -> usize;
}

impl dyn SeqObject + '_ {
/// Convenient iterator over a [`SeqObject`].
pub fn iter(&self) -> SeqObjectIter<'_> {
SeqObjectIter {
seq: self,
range: 0..self.seq_len(),
range: 0..self.item_count(),
}
}
}
Expand All @@ -192,7 +192,7 @@ impl<'a> SeqObject for &'a [Value] {
}

#[inline(always)]
fn seq_len(&self) -> usize {
fn item_count(&self) -> usize {
self.len()
}
}
Expand All @@ -204,7 +204,7 @@ impl SeqObject for Vec<Value> {
}

#[inline(always)]
fn seq_len(&self) -> usize {
fn item_count(&self) -> usize {
self.len()
}
}
Expand Down Expand Up @@ -311,7 +311,7 @@ pub trait StructObject {
/// Returns the number of fields in the struct.
///
/// The default implementation returns the number of fields.
fn struct_size(&self) -> usize {
fn field_count(&self) -> usize {
self.fields().count()
}
}
2 changes: 1 addition & 1 deletion minijinja/src/value/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn slice(value: Value, start: Value, stop: Value, step: Value) -> Result<Val

match maybe_seq {
Some(seq) => {
let (start, len) = get_offset_and_len(start, stop, || seq.seq_len());
let (start, len) = get_offset_and_len(start, stop, || seq.item_count());
Ok(Value::from(
seq.iter()
.skip(start)
Expand Down
4 changes: 2 additions & 2 deletions minijinja/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,13 +804,13 @@ impl<'env> Vm<'env> {
let seq = ok!(top
.as_seq()
.ok_or_else(|| Error::new(ErrorKind::CannotUnpack, "not a sequence")));
if seq.seq_len() != *count {
if seq.item_count() != *count {
return Err(Error::new(
ErrorKind::CannotUnpack,
format!(
"sequence of wrong length (expected {}, got {})",
*count,
seq.seq_len()
seq.item_count()
),
));
}
Expand Down
2 changes: 1 addition & 1 deletion minijinja/tests/test_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn test_seq_object_iteration_and_indexing() {
}
}

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

0 comments on commit 7f994a0

Please sign in to comment.