Skip to content

Commit dd93198

Browse files
committed
more
1 parent 8ecf433 commit dd93198

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

arrow-data/src/byte_view.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ impl InlineView {
142142

143143
/// The length of the data, in bytes
144144
#[inline(always)]
145-
pub fn len(&self) -> u32 {
146-
self.0 as u32
145+
pub fn len(&self) -> usize {
146+
// take first 4 bytes
147+
let len = self.0 as u32;
148+
len as usize
147149
}
148150

149151
/// Access the value of the data, as bytes
@@ -152,8 +154,7 @@ impl InlineView {
152154
/// If the length is greater than 12 (aka if this view is invalid)
153155
#[inline(always)]
154156
pub fn as_bytes(&self) -> &[u8] {
155-
let len = self.len() as usize;
156-
&self.0.to_byte_slice()[4..4 + len]
157+
&self.0.to_byte_slice()[4..4 + self.len()]
157158
}
158159

159160
/// Is the view zero bytes?
@@ -227,6 +228,36 @@ impl ByteView {
227228
pub fn as_u128(self) -> u128 {
228229
self.into_u128()
229230
}
231+
232+
/// The length of the data, in bytes
233+
#[inline(always)]
234+
pub fn len(&self) -> usize {
235+
self.length as usize
236+
}
237+
238+
/// If the view is zero bytes (always false)
239+
#[inline(always)]
240+
pub fn is_empty(&self) -> bool {
241+
false
242+
}
243+
244+
/// The buffer index
245+
#[inline(always)]
246+
pub fn buffer_index(&self) -> u32 {
247+
self.buffer_index
248+
}
249+
250+
/// The offset into the buffer
251+
#[inline(always)]
252+
pub fn offset(&self) -> usize {
253+
self.offset as usize
254+
}
255+
256+
/// The prefix of the data (always 4 bytes)
257+
#[inline(always)]
258+
pub fn prefix_as_bytes(&self) -> &[u8] {
259+
self.prefix.to_byte_slice()
260+
}
230261
}
231262

232263
impl From<u128> for ByteView {
@@ -358,7 +389,7 @@ mod tests {
358389
fn access_small_invalid() {
359390
// use invalid length 20
360391
// (7 bytes 0 padding, "hello", 15)
361-
let v = 0x00000000_0000006f_6c6c6568_0000000Fu128;
392+
let v = 0x00000000_0000006f_6c6c6568_0000000fu128;
362393
let inline = InlineView(v);
363394
inline.as_bytes();
364395
}
@@ -379,6 +410,16 @@ mod tests {
379410
assert_eq!(view, View::from("hello world here I am"))
380411
}
381412

413+
#[test]
414+
fn access_large() {
415+
let View::Byte(bytes) = View::from("hello world here I am") else {
416+
panic!("unexpected view");
417+
};
418+
419+
assert_eq!(bytes.len(), 21);
420+
assert_eq!(bytes.prefix_as_bytes(), "hell".as_bytes());
421+
}
422+
382423
// test round trip through u128
383424

384425
// Test

0 commit comments

Comments
 (0)