Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions rayforce/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ impl Value {
/// `to_vec::<Option<T>>()` maps it to `None` either way. Use `set_null` to
/// null an element.
pub fn is_null_at(&self, idx: usize) -> bool {
if !self.is_vec() || idx >= self.len() {
return false;
}
unsafe { sys::ray_vec_is_null(self.as_ptr(), idx as i64) }
}

Expand All @@ -220,6 +223,12 @@ impl Value {
/// [`Value::is_atom_null`] is true for it and `Option<String>` extraction
/// yields `None`, while plain `String` extraction still succeeds.
pub fn get(&self, idx: usize) -> Result<Value> {
if !self.is_vec() && self.type_code() != sys::RAY_LIST as i8 {
return Err(RayError::binding(format!(
"get: value is not a vector or list (type tag {})",
self.type_code()
)));
}
let n = self.len();
if idx >= n {
return Err(RayError::binding(format!(
Expand Down
11 changes: 11 additions & 0 deletions rayforce/tests/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ fn vector_get_and_iter() {
.unwrap();
}

#[test]
fn collection_access_rejects_atoms() {
Runtime::scope(|_rt| {
let atom = Value::i64(42);
assert!(atom.get(0).is_err());
assert!(!atom.is_null_at(0));
Ok(())
})
.unwrap();
}

#[test]
fn vector_mutation() {
Runtime::scope(|_rt| {
Expand Down