Skip to content
Merged
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
141 changes: 103 additions & 38 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,25 +1026,7 @@ impl IArray {
/// a large number of items.
pub fn insert(&mut self, index: usize, item: impl Into<IValue>) -> Result<(), IJsonError> {
let item = item.into();
let current_tag = self.header().type_tag();
let len = self.len();

if len == 0 {
let desired_tag = ArrayTag::from_ivalue(&item);
if desired_tag != ArrayTag::Heterogeneous {
if self.is_static() {
*self = IArray::with_capacity_and_tag(4, desired_tag)?;
} else {
self.promote_to_type(desired_tag)?;
}
}
} else if !(current_tag == ArrayTag::Heterogeneous
|| self.can_push_directly(&item, current_tag))
{
self.promote_to_type(current_tag.promote_with(ArrayTag::from_ivalue(&item)))?;
}

self.reserve(1)?;
self.prepare_for_item(&item)?;

unsafe {
// Safety: cannot be static after calling `reserve`
Expand Down Expand Up @@ -1193,31 +1175,36 @@ impl IArray {
/// Pushes a new item onto the back of the array.
pub fn push(&mut self, item: impl Into<IValue>) -> Result<(), IJsonError> {
let item = item.into();
let current_tag = self.header().type_tag();
let len = self.len();

if len == 0 {
let desired_tag = ArrayTag::from_ivalue(&item);
if desired_tag != ArrayTag::Heterogeneous {
if self.is_static() {
*self = IArray::with_capacity_and_tag(4, desired_tag)?;
} else {
self.promote_to_type(desired_tag)?;
}
}
} else if !(current_tag == ArrayTag::Heterogeneous
|| self.can_push_directly(&item, current_tag))
{
self.promote_to_type(current_tag.promote_with(ArrayTag::from_ivalue(&item)))?;
}

self.reserve(1)?;
self.prepare_for_item(&item)?;
unsafe {
self.header_mut().push(item);
}
Ok(())
}

/// Retags or promotes the array so `item` can be stored in it, then reserves

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment explaining the type transitions for an empty array:

  • Numeric → numeric: choose the numeric type that fits the new value, possibly narrowing it (e.g. i64 → i8).
  • Numeric → non-numeric: switch to Heterogeneous.
  • Heterogeneous → numeric: switch to the appropriate numeric type.
  • Heterogeneous → non-numeric: remain Heterogeneous.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Narrow? we never narrow.
And I think such comment is not needed + will also confuse since we never Heterogeneous → numeric(unless the array is empty, and therefore the code speaks for itself

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring only to empty arrays, where the type is selected again from the incoming value—for example, an emptied i64 array becomes i8 when inserting 7, as covered by the new test. I didn’t mean that we narrow non-empty arrays.

/// space for one more item.
fn prepare_for_item(&mut self, item: &IValue) -> Result<(), IJsonError> {
if self.len() == 0 {
// Empty: tag follows the item only (may narrow, drops any FPHA type);
// non-numeric item -> Heterogeneous.
let desired_tag = ArrayTag::from_ivalue(item);
if !self.is_static() {
self.promote_to_type(desired_tag)?;
} else if desired_tag != ArrayTag::Heterogeneous {
*self = IArray::with_capacity_and_tag(4, desired_tag)?;
}
} else {
let current_tag = self.header().type_tag();
if !(current_tag == ArrayTag::Heterogeneous
|| self.can_push_directly(item, current_tag))
{
self.promote_to_type(current_tag.promote_with(ArrayTag::from_ivalue(item)))?;
}
}
self.reserve(1)
}

/// Check if an item can be pushed directly to a typed array without promotion
/// This is a simple check: can the value fit in the array type?
fn can_push_directly(&self, item: &IValue, array_tag: ArrayTag) -> bool {
Expand Down Expand Up @@ -2158,6 +2145,7 @@ impl Default for IArray {
#[cfg(test)]
mod tests {
use super::*;
use crate::IObject;
use serde::Deserialize;

#[test]
Expand Down Expand Up @@ -2523,6 +2511,83 @@ mod tests {
}
}

/// One single-element array per typed tag.
fn singleton_typed_arrays() -> Vec<IArray> {
vec![
IArray::try_from(vec![1i8]).unwrap(),
IArray::try_from(vec![1u8]).unwrap(),
IArray::try_from(vec![1i16]).unwrap(),
IArray::try_from(vec![1u16]).unwrap(),
IArray::try_from(vec![f16::from_f32(1.5)]).unwrap(),
IArray::try_from(vec![bf16::from_f32(1.5)]).unwrap(),
IArray::try_from(vec![1i32]).unwrap(),
IArray::try_from(vec![1u32]).unwrap(),
IArray::try_from(vec![1.5f32]).unwrap(),
IArray::try_from(vec![1i64]).unwrap(),
IArray::try_from(vec![1u64]).unwrap(),
IArray::try_from(vec![1.5f64]).unwrap(),
]
}

#[test]
fn test_emptied_typed_array_accepts_non_numeric_items() {
let non_numeric = [
IValue::NULL,
IValue::TRUE,
IValue::from("s"),
IValue::from(IArray::new()),
IValue::from(IObject::new()),
];
let empty_ways: [fn(&mut IArray); 3] =
[|a| drop(a.remove(0)), |a| drop(a.pop()), |a| a.clear()];
for empty in empty_ways {
for item in &non_numeric {
for mut array in singleton_typed_arrays() {
let tag = array.header().type_tag();
assert_ne!(tag, ArrayTag::Heterogeneous);
empty(&mut array);
array.insert(0, item.clone()).unwrap();
assert_eq!(
array.header().type_tag(),
ArrayTag::Heterogeneous,
"{tag:?}"
);
assert_eq!(array.len(), 1);
assert_eq!(array[0], *item);
}
for mut array in singleton_typed_arrays() {
empty(&mut array);
array.push(item.clone()).unwrap();
array.push(1).unwrap();
assert_eq!(array.len(), 2);
assert_eq!(array[0], *item);
assert_eq!(array[1], IValue::from(1));
}
}
}
}

#[test]
fn test_emptied_typed_array_retags_to_numeric_item() {
for mut array in singleton_typed_arrays() {
array.remove(0).unwrap();
array.insert(0, 7).unwrap();
assert_eq!(array.header().type_tag(), ArrayTag::I8);
let items: Vec<IValue> = array.iter().map(|v| v.into_owned()).collect();
assert_eq!(items, vec![IValue::from(7)]);
}
}

#[test]
fn test_emptied_heterogeneous_array_retags_to_numeric_item() {
let mut array = IArray::new();
array.push(IValue::from("s")).unwrap();
array.pop().unwrap();
assert_eq!(array.header().type_tag(), ArrayTag::Heterogeneous);
array.push(7).unwrap();
assert_eq!(array.header().type_tag(), ArrayTag::I8);
}

#[test]
fn test_typed_array_type_promotion() {
let mut i8_array = IArray::try_from(vec![-5i8, 10i8]).unwrap();
Expand Down
Loading