Skip to main content

typed_arrow/bridge/
option.rs

1//! ArrowBindingView implementation for Option<T>.
2//!
3//! This module provides view support for nullable types, allowing `Option<T>` to have
4//! `View = Option<T::View>` while non-nullable types return errors on null values.
5
6#[cfg(feature = "views")]
7use super::ArrowBindingView;
8
9/// Implement ArrowBindingView for Option<T> where T implements ArrowBindingView.
10///
11/// This allows nullable fields to properly handle null values by returning Ok(None)
12/// instead of Err(UnexpectedNull), making the type system enforce correct null handling.
13#[cfg(feature = "views")]
14impl<T> ArrowBindingView for Option<T>
15where
16    T: ArrowBindingView,
17{
18    type Array = T::Array;
19    type View<'a>
20        = Option<T::View<'a>>
21    where
22        Self: 'a;
23
24    fn get_view(
25        array: &Self::Array,
26        index: usize,
27    ) -> Result<Self::View<'_>, crate::schema::ViewAccessError> {
28        use arrow_array::Array;
29
30        if index >= array.len() {
31            return Err(crate::schema::ViewAccessError::OutOfBounds {
32                index,
33                len: array.len(),
34                field_name: None,
35            });
36        }
37
38        // For nullable types, null is valid data
39        if array.is_null(index) {
40            return Ok(None);
41        }
42
43        // Delegate to the inner type's get_view
44        T::get_view(array, index).map(Some)
45    }
46}