From dd931984197999166d3507efeca72cb18a0b14dc Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Sun, 5 May 2024 06:13:42 -0400 Subject: [PATCH] more --- arrow-data/src/byte_view.rs | 51 +++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/arrow-data/src/byte_view.rs b/arrow-data/src/byte_view.rs index b577ff1a078..4888c88f723 100644 --- a/arrow-data/src/byte_view.rs +++ b/arrow-data/src/byte_view.rs @@ -142,8 +142,10 @@ impl InlineView { /// The length of the data, in bytes #[inline(always)] - pub fn len(&self) -> u32 { - self.0 as u32 + pub fn len(&self) -> usize { + // take first 4 bytes + let len = self.0 as u32; + len as usize } /// Access the value of the data, as bytes @@ -152,8 +154,7 @@ impl InlineView { /// If the length is greater than 12 (aka if this view is invalid) #[inline(always)] pub fn as_bytes(&self) -> &[u8] { - let len = self.len() as usize; - &self.0.to_byte_slice()[4..4 + len] + &self.0.to_byte_slice()[4..4 + self.len()] } /// Is the view zero bytes? @@ -227,6 +228,36 @@ impl ByteView { pub fn as_u128(self) -> u128 { self.into_u128() } + + /// The length of the data, in bytes + #[inline(always)] + pub fn len(&self) -> usize { + self.length as usize + } + + /// If the view is zero bytes (always false) + #[inline(always)] + pub fn is_empty(&self) -> bool { + false + } + + /// The buffer index + #[inline(always)] + pub fn buffer_index(&self) -> u32 { + self.buffer_index + } + + /// The offset into the buffer + #[inline(always)] + pub fn offset(&self) -> usize { + self.offset as usize + } + + /// The prefix of the data (always 4 bytes) + #[inline(always)] + pub fn prefix_as_bytes(&self) -> &[u8] { + self.prefix.to_byte_slice() + } } impl From for ByteView { @@ -358,7 +389,7 @@ mod tests { fn access_small_invalid() { // use invalid length 20 // (7 bytes 0 padding, "hello", 15) - let v = 0x00000000_0000006f_6c6c6568_0000000Fu128; + let v = 0x00000000_0000006f_6c6c6568_0000000fu128; let inline = InlineView(v); inline.as_bytes(); } @@ -379,6 +410,16 @@ mod tests { assert_eq!(view, View::from("hello world here I am")) } + #[test] + fn access_large() { + let View::Byte(bytes) = View::from("hello world here I am") else { + panic!("unexpected view"); + }; + + assert_eq!(bytes.len(), 21); + assert_eq!(bytes.prefix_as_bytes(), "hell".as_bytes()); + } + // test round trip through u128 // Test