tustvold commented on code in PR #3965: URL: https://github.com/apache/arrow-rs/pull/3965#discussion_r1149772137
########## arrow-schema/src/fields.rs: ########## @@ -0,0 +1,139 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::{Field, FieldRef}; +use std::ops::Deref; +use std::sync::Arc; + +/// A cheaply cloneable, owned slice of [`FieldRef`] +/// +/// Similar to `Arc<Vec<FieldPtr>>` or `Arc<[FieldPtr]>` +/// +/// Can be constructed in a number of ways +/// +/// ``` +/// # use std::sync::Arc; +/// # use arrow_schema::{DataType, Field, Fields}; +/// // Can be constructed from Vec<Field> +/// Fields::from(vec![Field::new("a", DataType::Boolean, false)]); +/// // Can be constructed from Vec<FieldRef> +/// Fields::from(vec![Arc::new(Field::new("a", DataType::Boolean, false))]); +/// // Can be constructed from an iterator of Field +/// std::iter::once(Field::new("a", DataType::Boolean, false)).collect::<Fields>(); +/// // Can be constructed from an iterator of FieldRef +/// std::iter::once(Arc::new(Field::new("a", DataType::Boolean, false))).collect::<Fields>(); +/// ``` +/// +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub struct Fields(Arc<[FieldRef]>); + +impl std::fmt::Debug for Fields { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.as_ref().fmt(f) + } +} + +impl Fields { + /// Returns a new empty [`Fields`] + pub fn new() -> Self { + Self(Arc::new([])) + } + + /// Return size of this instance in bytes. + pub fn size(&self) -> usize { + self.iter().map(|field| field.size()).sum() + } +} + +impl Default for Fields { + fn default() -> Self { + Self::new() + } +} + +impl FromIterator<Field> for Fields { + fn from_iter<T: IntoIterator<Item = Field>>(iter: T) -> Self { + iter.into_iter().map(Arc::new).collect() + } +} + +impl FromIterator<FieldRef> for Fields { + fn from_iter<T: IntoIterator<Item = FieldRef>>(iter: T) -> Self { + Self(iter.into_iter().collect()) + } +} + +impl From<Vec<Field>> for Fields { + fn from(value: Vec<Field>) -> Self { + value.into_iter().collect() + } +} + +impl From<Vec<FieldRef>> for Fields { + fn from(value: Vec<FieldRef>) -> Self { + Self(value.into()) Review Comment: It is perhaps worth highlighting that this is implemented as a memove, it cannot reuse the allocation -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org