Skip to content

Commit 3558a0d

Browse files
committed
Add: Named RawParts
1 parent 4864fa9 commit 3558a0d

1 file changed

Lines changed: 42 additions & 24 deletions

File tree

stringtape.rs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ struct RawTape<Offset: OffsetType, A: Allocator> {
133133
_phantom: PhantomData<Offset>,
134134
}
135135

136+
/// Named raw parts returned by `as_raw_parts` methods.
137+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138+
pub struct RawParts<Offset: OffsetType> {
139+
/// Pointer to the start of the contiguous data buffer.
140+
pub data_ptr: *const u8,
141+
/// Pointer to the start of the offsets buffer.
142+
pub offsets_ptr: *const Offset,
143+
/// Number of bytes of valid data in `data_ptr`.
144+
pub data_len: usize,
145+
/// Number of items stored (strings/bytes entries).
146+
pub items_count: usize,
147+
}
148+
136149
/// UTF-8 string view over `RawTape`.
137150
pub struct StringTape<Offset: OffsetType = i32, A: Allocator = Global> {
138151
inner: RawTape<Offset, A>,
@@ -601,16 +614,16 @@ impl<Offset: OffsetType, A: Allocator> RawTape<Offset, A> {
601614

602615
/// Returns the raw parts of the tape for Apache Arrow compatibility.
603616
///
604-
/// Returns a tuple of:
605-
/// - Data buffer pointer
606-
/// - Offsets buffer pointer
607-
/// - Data length in bytes
608-
/// - Number of strings
617+
/// Returns named fields:
618+
/// - `data_ptr`: Data buffer pointer
619+
/// - `offsets_ptr`: Offsets buffer pointer
620+
/// - `data_len`: Data length in bytes
621+
/// - `items_count`: Number of items
609622
///
610623
/// # Safety
611624
///
612625
/// The returned pointers are valid only as long as the StringTape is not modified.
613-
pub fn as_raw_parts(&self) -> (*const u8, *const Offset, usize, usize) {
626+
pub fn as_raw_parts(&self) -> RawParts<Offset> {
614627
let data_ptr = self
615628
.data
616629
.map(|ptr| ptr.as_ptr().cast::<u8>() as *const u8)
@@ -619,7 +632,12 @@ impl<Offset: OffsetType, A: Allocator> RawTape<Offset, A> {
619632
.offsets
620633
.map(|ptr| ptr.as_ptr().cast::<Offset>() as *const Offset)
621634
.unwrap_or(ptr::null());
622-
(data_ptr, offsets_ptr, self.len_bytes, self.len_items)
635+
RawParts {
636+
data_ptr,
637+
offsets_ptr,
638+
data_len: self.len_bytes,
639+
items_count: self.len_items,
640+
}
623641
}
624642

625643
/// Returns a slice view of the data buffer.
@@ -820,7 +838,7 @@ impl<'a, Offset: OffsetType> RawTapeView<'a, Offset> {
820838
///
821839
/// The caller must ensure that:
822840
/// - `data` contains valid bytes for the lifetime `'a`
823-
/// - `offsets` contains valid offsets with length `items_len + 1`
841+
/// - `offsets` contains valid offsets with length `items_count + 1`
824842
/// - All offsets are within bounds of the data slice
825843
/// - For StringTapeView, data must be valid UTF-8
826844
pub unsafe fn from_raw_parts(data: &'a [u8], offsets: &'a [Offset]) -> Self {
@@ -882,13 +900,13 @@ impl<'a, Offset: OffsetType> RawTapeView<'a, Offset> {
882900
}
883901

884902
/// Returns the raw parts of the view for Apache Arrow compatibility.
885-
pub fn as_raw_parts(&self) -> (*const u8, *const Offset, usize, usize) {
886-
(
887-
self.data.as_ptr(),
888-
self.offsets.as_ptr(),
889-
self.data_len(),
890-
self.len(),
891-
)
903+
pub fn as_raw_parts(&self) -> RawParts<Offset> {
904+
RawParts {
905+
data_ptr: self.data.as_ptr(),
906+
offsets_ptr: self.offsets.as_ptr(),
907+
data_len: self.data_len(),
908+
items_count: self.len(),
909+
}
892910
}
893911
}
894912

@@ -1014,7 +1032,7 @@ impl<'a, Offset: OffsetType> StringTapeView<'a, Offset> {
10141032
}
10151033

10161034
/// Returns the raw parts of the view for Apache Arrow compatibility.
1017-
pub fn as_raw_parts(&self) -> (*const u8, *const Offset, usize, usize) {
1035+
pub fn as_raw_parts(&self) -> RawParts<Offset> {
10181036
self.inner.as_raw_parts()
10191037
}
10201038
}
@@ -1078,7 +1096,7 @@ impl<'a, Offset: OffsetType> BytesTapeView<'a, Offset> {
10781096
}
10791097

10801098
/// Returns the raw parts of the view for Apache Arrow compatibility.
1081-
pub fn as_raw_parts(&self) -> (*const u8, *const Offset, usize, usize) {
1099+
pub fn as_raw_parts(&self) -> RawParts<Offset> {
10821100
self.inner.as_raw_parts()
10831101
}
10841102
}
@@ -1201,7 +1219,7 @@ impl<Offset: OffsetType, A: Allocator> StringTape<Offset, A> {
12011219
}
12021220

12031221
/// Returns the raw parts of the StringTape for Apache Arrow compatibility.
1204-
pub fn as_raw_parts(&self) -> (*const u8, *const Offset, usize, usize) {
1222+
pub fn as_raw_parts(&self) -> RawParts<Offset> {
12051223
self.inner.as_raw_parts()
12061224
}
12071225

@@ -1423,7 +1441,7 @@ impl<Offset: OffsetType, A: Allocator> BytesTape<Offset, A> {
14231441
}
14241442

14251443
/// Returns the raw parts of the tape for Apache Arrow compatibility.
1426-
pub fn as_raw_parts(&self) -> (*const u8, *const Offset, usize, usize) {
1444+
pub fn as_raw_parts(&self) -> RawParts<Offset> {
14271445
self.inner.as_raw_parts()
14281446
}
14291447

@@ -1879,12 +1897,12 @@ mod tests {
18791897
tape.push("data").unwrap();
18801898

18811899
let view = tape.subview(0, 2).unwrap();
1882-
let (data_ptr, offsets_ptr, data_len, items_len) = view.as_raw_parts();
1900+
let parts = view.as_raw_parts();
18831901

1884-
assert!(!data_ptr.is_null());
1885-
assert!(!offsets_ptr.is_null());
1886-
assert_eq!(data_len, 8); // "test" + "data"
1887-
assert_eq!(items_len, 2);
1902+
assert!(!parts.data_ptr.is_null());
1903+
assert!(!parts.offsets_ptr.is_null());
1904+
assert_eq!(parts.data_len, 8); // "test" + "data"
1905+
assert_eq!(parts.items_count, 2);
18881906
}
18891907

18901908
#[test]

0 commit comments

Comments
 (0)