diff --git a/arrow/src/array/array_string.rs b/arrow/src/array/array_string.rs index ba285078369b..ec703e28f5ac 100644 --- a/arrow/src/array/array_string.rs +++ b/arrow/src/array/array_string.rs @@ -149,7 +149,10 @@ impl GenericStringArray { Self::from(data) } - pub(crate) fn from_vec(v: Vec<&str>) -> Self { + pub(crate) fn from_vec(v: Vec) -> Self + where + Ptr: AsRef, + { let mut offsets = MutableBuffer::new((v.len() + 1) * std::mem::size_of::()); let mut values = MutableBuffer::new(0); @@ -158,9 +161,9 @@ impl GenericStringArray { offsets.push(length_so_far); for s in &v { - length_so_far += OffsetSize::from_usize(s.len()).unwrap(); + length_so_far += OffsetSize::from_usize(s.as_ref().len()).unwrap(); offsets.push(length_so_far); - values.extend_from_slice(s.as_bytes()); + values.extend_from_slice(s.as_ref().as_bytes()); } let array_data = ArrayData::builder(OffsetSize::DATA_TYPE) .len(v.len()) @@ -327,6 +330,14 @@ impl From> } } +impl From> + for GenericStringArray +{ + fn from(v: Vec) -> Self { + GenericStringArray::::from_vec(v) + } +} + /// An array where each element is a variable-sized sequence of bytes representing a string /// whose maximum length (in bytes) is represented by a i32. /// @@ -530,4 +541,15 @@ mod tests { // but the actual number of items in the array should be 10 assert_eq!(string_array.len(), 10); } + + #[test] + fn test_string_array_from_string_vec() { + let data = vec!["Foo".to_owned(), "Bar".to_owned(), "Baz".to_owned()]; + let array = StringArray::from(data); + + assert_eq!(array.len(), 3); + assert_eq!(array.value(0), "Foo"); + assert_eq!(array.value(1), "Bar"); + assert_eq!(array.value(2), "Baz"); + } }