Skip to content
Open
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
41 changes: 41 additions & 0 deletions arrow-schema/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ impl std::fmt::Debug for UnionFields {
}
}

/// Allows direct indexing into [`UnionFields`] to access fields by position.
///
/// # Panics
///
/// Panics if the index is out of bounds. Note that [`UnionFields`] supports
/// a maximum of 128 fields, as type IDs are represented as `i8` values.
///
/// For a non-panicking alternative, use [`UnionFields::get`].
impl std::ops::Index<usize> for UnionFields {
type Output = (i8, FieldRef);

fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}

impl UnionFields {
/// Create a new [`UnionFields`] with no fields
pub fn empty() -> Self {
Expand Down Expand Up @@ -396,6 +412,31 @@ impl UnionFields {
self.0.iter().map(|(id, f)| (*id, f))
}

/// Returns a reference to the field at the given index, or `None` if out of bounds.
///
/// This is a safe alternative to direct indexing via `[]`.
///
/// # Example
///
/// ```
/// use arrow_schema::{DataType, Field, UnionFields};
///
/// let fields = UnionFields::new(
/// vec![1, 3],
/// vec![
/// Field::new("field1", DataType::UInt8, false),
/// Field::new("field3", DataType::Utf8, false),
/// ],
/// );
///
/// assert!(fields.get(0).is_some());
/// assert!(fields.get(1).is_some());
/// assert!(fields.get(2).is_none());
/// ```
pub fn get(&self, index: usize) -> Option<&(i8, FieldRef)> {
self.0.get(index)
}

/// Searches for a field by its type id, returning the type id and field reference if found.
/// Returns `None` if no field with the given type id exists.
pub fn find_by_type_id(&self, type_id: i8) -> Option<(i8, &FieldRef)> {
Expand Down
Loading