typed_arrow/bridge/
strings.rs

1//! `Utf8` and `LargeUtf8` string bindings.
2
3use arrow_array::{
4    builder::{LargeStringBuilder, StringBuilder},
5    LargeStringArray, StringArray,
6};
7use arrow_schema::DataType;
8
9use super::ArrowBinding;
10
11// Utf8/String
12impl ArrowBinding for String {
13    type Builder = StringBuilder;
14    type Array = StringArray;
15    fn data_type() -> DataType {
16        DataType::Utf8
17    }
18    fn new_builder(capacity: usize) -> Self::Builder {
19        StringBuilder::with_capacity(capacity, 0)
20    }
21    fn append_value(b: &mut Self::Builder, v: &Self) {
22        b.append_value(v.as_str());
23    }
24    fn append_null(b: &mut Self::Builder) {
25        b.append_null();
26    }
27    fn finish(mut b: Self::Builder) -> Self::Array {
28        b.finish()
29    }
30}
31
32/// Wrapper denoting Arrow `LargeUtf8` values. Use when individual strings can be
33/// extremely large or when 64-bit offsets are preferred.
34pub struct LargeUtf8(String);
35
36impl LargeUtf8 {
37    /// Construct a new `LargeUtf8` from a `String`.
38    #[inline]
39    #[must_use]
40    pub fn new(value: String) -> Self {
41        Self(value)
42    }
43    /// Return the underlying string slice.
44    #[inline]
45    #[must_use]
46    pub fn as_str(&self) -> &str {
47        self.0.as_str()
48    }
49    /// Consume and return the underlying `String`.
50    #[inline]
51    #[must_use]
52    pub fn into_string(self) -> String {
53        self.0
54    }
55}
56
57impl From<String> for LargeUtf8 {
58    /// Convert a `String` into a `LargeUtf8`.
59    #[inline]
60    fn from(value: String) -> Self {
61        Self::new(value)
62    }
63}
64impl From<&str> for LargeUtf8 {
65    /// Convert a `&str` into a `LargeUtf8` by allocating a `String`.
66    #[inline]
67    fn from(s: &str) -> Self {
68        Self::new(s.to_string())
69    }
70}
71
72impl ArrowBinding for LargeUtf8 {
73    type Builder = LargeStringBuilder;
74    type Array = LargeStringArray;
75    fn data_type() -> DataType {
76        DataType::LargeUtf8
77    }
78    fn new_builder(capacity: usize) -> Self::Builder {
79        LargeStringBuilder::with_capacity(capacity, 0)
80    }
81    fn append_value(b: &mut Self::Builder, v: &Self) {
82        b.append_value(v.0.as_str());
83    }
84    fn append_null(b: &mut Self::Builder) {
85        b.append_null();
86    }
87    fn finish(mut b: Self::Builder) -> Self::Array {
88        b.finish()
89    }
90}