From 2c13a33c517b072b43ef20713ea51dee6836d676 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Tue, 28 Dec 2021 06:17:42 +0000 Subject: [PATCH] Borrow -> AsRef --- src/array/mod.rs | 8 ++++++++ src/columns.rs | 14 +++++++------- src/compute/filter.rs | 6 +++--- src/io/csv/write/mod.rs | 8 ++++---- src/io/json/write/mod.rs | 6 +++--- src/io/json/write/serialize.rs | 4 ++-- src/io/parquet/write/record_batch.rs | 13 ++++--------- src/io/print.rs | 7 ++----- tests/it/io/avro/write.rs | 4 ++-- tests/it/io/json/mod.rs | 2 +- 10 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/array/mod.rs b/src/array/mod.rs index 06dc571d427..b9a83d87225 100644 --- a/src/array/mod.rs +++ b/src/array/mod.rs @@ -338,6 +338,14 @@ pub fn clone(array: &dyn Array) -> Box { } } +// see https://users.rust-lang.org/t/generic-for-dyn-a-or-box-dyn-a-or-arc-dyn-a/69430/3 +// for details +impl<'a> AsRef<(dyn Array + 'a)> for dyn Array { + fn as_ref(&self) -> &(dyn Array + 'a) { + self + } +} + mod binary; mod boolean; mod dictionary; diff --git a/src/columns.rs b/src/columns.rs index 3957049796b..940ab63452f 100644 --- a/src/columns.rs +++ b/src/columns.rs @@ -7,11 +7,11 @@ use crate::error::{ArrowError, Result}; /// A vector of trait objects of [`Array`] where every item has /// the same length, [`Columns::len`]. #[derive(Debug, Clone, PartialEq)] -pub struct Columns> { +pub struct Columns> { arrays: Vec, } -impl> Columns { +impl> Columns { /// Creates a new [`Columns`]. /// # Panic /// Iff the arrays do not have the same length @@ -24,10 +24,10 @@ impl> Columns { /// Iff the arrays do not have the same length pub fn try_new(arrays: Vec) -> Result { if !arrays.is_empty() { - let len = arrays.first().unwrap().borrow().len(); + let len = arrays.first().unwrap().as_ref().len(); if arrays .iter() - .map(|array| array.borrow()) + .map(|array| array.as_ref()) .any(|array| array.len() != len) { return Err(ArrowError::InvalidArgumentError( @@ -52,7 +52,7 @@ impl> Columns { pub fn len(&self) -> usize { self.arrays .first() - .map(|x| x.borrow().len()) + .map(|x| x.as_ref().len()) .unwrap_or_default() } @@ -68,13 +68,13 @@ impl> Columns { } } -impl> From> for Vec { +impl> From> for Vec { fn from(c: Columns) -> Self { c.into_arrays() } } -impl> std::ops::Deref for Columns { +impl> std::ops::Deref for Columns { type Target = [A]; #[inline] diff --git a/src/compute/filter.rs b/src/compute/filter.rs index eab6041e991..262ac2b9add 100644 --- a/src/compute/filter.rs +++ b/src/compute/filter.rs @@ -154,7 +154,7 @@ pub fn filter(array: &dyn Array, filter: &BooleanArray) -> Result /// Returns a new [Columns] with arrays containing only values matching the filter. /// This is a convenience function: filter multiple columns is embarassingly parallel. -pub fn filter_columns>( +pub fn filter_columns>( columns: &Columns, filter_values: &BooleanArray, ) -> Result>> { @@ -164,11 +164,11 @@ pub fn filter_columns>( let filtered_arrays = match num_colums { 1 => { - vec![filter(columns.arrays()[0].borrow(), filter_values)?] + vec![filter(columns.arrays()[0].as_ref(), filter_values)?] } _ => { let filter = build_filter(filter_values)?; - arrays.iter().map(|a| filter(a.borrow())).collect() + arrays.iter().map(|a| filter(a.as_ref())).collect() } }; Columns::try_new(filtered_arrays) diff --git a/src/io/csv/write/mod.rs b/src/io/csv/write/mod.rs index 1e8788f3fa1..ec3225a85b9 100644 --- a/src/io/csv/write/mod.rs +++ b/src/io/csv/write/mod.rs @@ -16,20 +16,20 @@ use crate::error::Result; /// Creates serializers that iterate over each column that serializes each item according /// to `options`. -fn new_serializers<'a, A: std::borrow::Borrow>( +fn new_serializers<'a, A: AsRef>( columns: &'a [A], options: &'a SerializeOptions, ) -> Result + 'a>>> { columns .iter() - .map(|column| new_serializer(column.borrow(), options)) + .map(|column| new_serializer(column.as_ref(), options)) .collect() } /// Serializes [`Columns`] to a vector of `ByteRecord`. /// The vector is guaranteed to have `columns.len()` entries. /// Each `ByteRecord` is guaranteed to have `columns.array().len()` fields. -pub fn serialize>( +pub fn serialize>( columns: &Columns, options: &SerializeOptions, ) -> Result> { @@ -47,7 +47,7 @@ pub fn serialize>( } /// Writes [`Columns`] to `writer` according to the serialization options `options`. -pub fn write_columns>( +pub fn write_columns>( writer: &mut Writer, columns: &Columns, options: &SerializeOptions, diff --git a/src/io/json/write/mod.rs b/src/io/json/write/mod.rs index 3e5cf224859..fa4c7f8c683 100644 --- a/src/io/json/write/mod.rs +++ b/src/io/json/write/mod.rs @@ -35,7 +35,7 @@ where pub struct Serializer where F: JsonFormat, - A: std::borrow::Borrow, + A: AsRef, I: Iterator>>, { batches: I, @@ -47,7 +47,7 @@ where impl Serializer where F: JsonFormat, - A: std::borrow::Borrow, + A: AsRef, I: Iterator>>, { /// Creates a new [`Serializer`]. @@ -64,7 +64,7 @@ where impl FallibleStreamingIterator for Serializer where F: JsonFormat, - A: std::borrow::Borrow, + A: AsRef, I: Iterator>>, { type Item = [u8]; diff --git a/src/io/json/write/serialize.rs b/src/io/json/write/serialize.rs index 2899f2cf874..7a775dd9339 100644 --- a/src/io/json/write/serialize.rs +++ b/src/io/json/write/serialize.rs @@ -202,7 +202,7 @@ fn serialize_item( pub fn serialize(names: &[N], columns: &Columns, format: F, buffer: &mut Vec) where N: AsRef, - A: std::borrow::Borrow, + A: AsRef, F: JsonFormat, { let num_rows = columns.len(); @@ -210,7 +210,7 @@ where let mut serializers: Vec<_> = columns .arrays() .iter() - .map(|array| new_serializer(array.borrow())) + .map(|array| new_serializer(array.as_ref())) .collect(); let mut is_first_row = true; diff --git a/src/io/parquet/write/record_batch.rs b/src/io/parquet/write/record_batch.rs index 832527ee271..fc436faff0b 100644 --- a/src/io/parquet/write/record_batch.rs +++ b/src/io/parquet/write/record_batch.rs @@ -15,19 +15,14 @@ use crate::{ /// An iterator adapter that converts an iterator over [`Columns`] into an iterator /// of row groups. /// Use it to create an iterator consumable by the parquet's API. -pub struct RowGroupIterator< - A: std::borrow::Borrow + 'static, - I: Iterator>>, -> { +pub struct RowGroupIterator + 'static, I: Iterator>>> { iter: I, options: WriteOptions, parquet_schema: SchemaDescriptor, encodings: Vec, } -impl + 'static, I: Iterator>>> - RowGroupIterator -{ +impl + 'static, I: Iterator>>> RowGroupIterator { /// Creates a new [`RowGroupIterator`] from an iterator over [`Columns`]. pub fn try_new( iter: I, @@ -53,7 +48,7 @@ impl + 'static, I: Iterator + 'static, I: Iterator>>> Iterator +impl + 'static, I: Iterator>>> Iterator for RowGroupIterator { type Item = Result>; @@ -71,7 +66,7 @@ impl + 'static, I: Iterator, N: AsRef>( - batches: &[Columns], - names: &[N], -) -> String { +pub fn write, N: AsRef>(batches: &[Columns], names: &[N]) -> String { let mut table = Table::new(); table.load_preset("||--+-++| ++++++"); @@ -26,7 +23,7 @@ pub fn write, N: AsRef>( let displayes = batch .arrays() .iter() - .map(|array| get_display(array.borrow())) + .map(|array| get_display(array.as_ref())) .collect::>(); for row in 0..batch.len() { diff --git a/tests/it/io/avro/write.rs b/tests/it/io/avro/write.rs index bf0cad5e22e..a597ae8b900 100644 --- a/tests/it/io/avro/write.rs +++ b/tests/it/io/avro/write.rs @@ -36,7 +36,7 @@ fn data() -> Columns> { use super::read::read_avro; -fn write_avro>( +fn write_avro>( columns: &Columns, schema: &Schema, compression: Option, @@ -46,7 +46,7 @@ fn write_avro>( let mut serializers = columns .arrays() .iter() - .map(|x| x.borrow()) + .map(|x| x.as_ref()) .zip(avro_fields.iter()) .map(|(array, field)| write::new_serializer(array, &field.schema)) .collect::>(); diff --git a/tests/it/io/json/mod.rs b/tests/it/io/json/mod.rs index 3cb5314a024..fbdcf48120d 100644 --- a/tests/it/io/json/mod.rs +++ b/tests/it/io/json/mod.rs @@ -20,7 +20,7 @@ fn read_batch(data: String, fields: &[Field]) -> Result>> json_read::deserialize(rows, fields) } -fn write_batch>( +fn write_batch>( batch: Columns, names: Vec, ) -> Result> {