typed_arrow/lib.rs
1#![deny(missing_docs)]
2//! typed-arrow core: compile-time Arrow schema traits and primitive markers.
3
4pub mod bridge;
5pub mod error;
6pub mod schema;
7
8/// Prelude exporting the most common traits and markers.
9pub mod prelude {
10 #[cfg(feature = "views")]
11 pub use crate::error::ViewAccessError;
12 #[cfg(feature = "views")]
13 pub use crate::schema::{FromRecordBatch, ViewResultIteratorExt};
14 #[cfg(feature = "views")]
15 pub use crate::AsViewsIterator;
16 pub use crate::{
17 error::SchemaError,
18 schema::{BuildRows, ColAt, ColumnVisitor, FieldMeta, ForEachCol, Record},
19 };
20}
21
22// Re-export the derive macro when enabled
23// Re-export Arrow crates so derives can reference a stable path
24// and downstream users don't need to depend on Arrow directly.
25pub use arrow_array;
26pub use arrow_buffer;
27pub use arrow_schema;
28#[cfg(feature = "derive")]
29pub use typed_arrow_derive::{Record, Union};
30
31// Public re-exports for convenience
32pub use crate::bridge::{
33 Date32, Date64, Decimal128, Decimal256, Dictionary, Duration, FixedSizeList,
34 FixedSizeListNullable, IntervalDayTime, IntervalMonthDayNano, IntervalYearMonth, LargeBinary,
35 LargeList, LargeUtf8, List, Map, Microsecond, Millisecond, Nanosecond, Null, OrderedMap,
36 Second, Time32, Time64, TimeZoneSpec, Timestamp, TimestampTz, Utc,
37};
38
39/// Extension trait for creating typed view iterators from `RecordBatch`.
40#[cfg(feature = "views")]
41pub trait AsViewsIterator {
42 /// Iterate over typed views of rows in this RecordBatch.
43 ///
44 /// This provides zero-copy access to the data as borrowed references.
45 ///
46 /// # Errors
47 /// Returns `SchemaError` if the RecordBatch schema doesn't match the expected Record type.
48 ///
49 /// # Example
50 /// ```
51 /// use typed_arrow::prelude::*;
52 ///
53 /// #[derive(typed_arrow::Record)]
54 /// struct Row {
55 /// id: i32,
56 /// name: String,
57 /// }
58 ///
59 /// // Build a RecordBatch
60 /// let rows = vec![
61 /// Row {
62 /// id: 1,
63 /// name: "Alice".to_string(),
64 /// },
65 /// Row {
66 /// id: 2,
67 /// name: "Bob".to_string(),
68 /// },
69 /// ];
70 /// let mut b = <Row as BuildRows>::new_builders(rows.len());
71 /// b.append_rows(rows);
72 /// let arrays = b.finish();
73 /// let batch = arrays.into_record_batch();
74 ///
75 /// // Iterate with zero-copy views (using convenience method to handle errors)
76 /// let views = batch.iter_views::<Row>()?.try_flatten()?;
77 /// for row in views {
78 /// println!("{}: {}", row.id, row.name);
79 /// }
80 /// # Ok::<_, typed_arrow::error::SchemaError>(())
81 /// ```
82 fn iter_views<T: schema::FromRecordBatch>(&self) -> Result<T::Views<'_>, error::SchemaError>;
83}
84
85#[cfg(feature = "views")]
86impl AsViewsIterator for arrow_array::RecordBatch {
87 fn iter_views<T: schema::FromRecordBatch>(&self) -> Result<T::Views<'_>, error::SchemaError> {
88 T::from_record_batch(self)
89 }
90}