Skip to content

Commit 5e0f47b

Browse files
authored
Merge pull request #3802 from froydnj/froydnj-constant-id-list-len
[rust] add `len()`/`is_empty()` to `ConstantList`
2 parents cf8316d + 30dc6d7 commit 5e0f47b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

rust/ruby-prism/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ impl<'pr> ConstantList<'pr> {
255255
marker: PhantomData,
256256
}
257257
}
258+
259+
/// Returns the length of the list.
260+
#[must_use]
261+
pub const fn len(&self) -> usize {
262+
unsafe { self.pointer.as_ref().size }
263+
}
264+
265+
/// Returns whether the list is empty.
266+
#[must_use]
267+
pub const fn is_empty(&self) -> bool {
268+
self.len() == 0
269+
}
258270
}
259271

260272
impl<'pr> IntoIterator for &ConstantList<'pr> {
@@ -811,11 +823,27 @@ mod tests {
811823
assert!(!node.as_program_node().unwrap().statements().body().is_empty());
812824
let module = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
813825
let module = module.as_module_node().unwrap();
826+
827+
assert_eq!(module.locals().len(), 1);
828+
assert!(!module.locals().is_empty());
829+
814830
let locals = module.locals().iter().collect::<Vec<_>>();
815831

816832
assert_eq!(locals.len(), 1);
817833

818834
assert_eq!(locals[0].as_slice(), b"x");
835+
836+
let source = "module Foo; end";
837+
let result = parse(source.as_ref());
838+
839+
let node = result.node();
840+
assert_eq!(node.as_program_node().unwrap().statements().body().len(), 1);
841+
assert!(!node.as_program_node().unwrap().statements().body().is_empty());
842+
let module = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
843+
let module = module.as_module_node().unwrap();
844+
845+
assert_eq!(module.locals().len(), 0);
846+
assert!(module.locals().is_empty());
819847
}
820848

821849
#[test]

0 commit comments

Comments
 (0)