Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed May 5, 2024
1 parent 8ecf433 commit dd93198
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions arrow-data/src/byte_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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<u128> for ByteView {
Expand Down Expand Up @@ -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();
}
Expand All @@ -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
Expand Down

0 comments on commit dd93198

Please sign in to comment.