Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

MutableListArray from an existing MutableArray #1502

Open
cjermain opened this issue Jun 5, 2023 · 1 comment
Open

MutableListArray from an existing MutableArray #1502

cjermain opened this issue Jun 5, 2023 · 1 comment

Comments

@cjermain
Copy link
Contributor

cjermain commented Jun 5, 2023

Is there a way to directly nest a non-empty MutableArray in a MutableListArray?

In the majority of MutableArray implementations there is a from_trusted_len_iter method that allows the array to be constructed directly from an iterator. In the case of a MutableStructArray, the new method takes a vector of the inner MutableArrays, so you can directly construct it. Is there an analogy for MutableListArray?

It looks like the new_from has an assert that prevents non-empty MutableArrays from being passed through:

assert_eq!(values.len(), 0);

Along with the MutableArray I also have all of the lengths in a Vec<Option<usize>>. I had been hoping to use the following, but am failing on the assert during runtime:

let mut array = MutableListArray::<i32, _>::new_with_capacity(inner_array, lengths.len());
array.try_extend_from_lengths(lengths).unwrap();

I'm trying to accomplish the same thing in this function, without the pre-allocation of an empty inner MutableArray ahead of time:

fn deserialize_list_into<'a, O: Offset, A: Borrow<Value<'a>>>(
target: &mut MutableListArray<O, Box<dyn MutableArray>>,
rows: &[A],
) {
let empty = vec![];
let inner: Vec<_> = rows
.iter()
.flat_map(|row| match row.borrow() {
Value::Array(value) => value.iter(),
_ => empty.iter(),
})
.collect();
deserialize_into(target.mut_values(), &inner);
let lengths = rows.iter().map(|row| match row.borrow() {
Value::Array(value) => Some(value.len()),
_ => None,
});
target
.try_extend_from_lengths(lengths)
.expect("Offsets overflow");
}

@cjermain
Copy link
Contributor Author

cjermain commented Jun 8, 2023

@jorgecarleitao, what do you think?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant