Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed May 6, 2024
1 parent 653065b commit 8656ea8
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions arrow-data/src/byte_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ impl<'a> From<&'a u128> for View<'a> {
}
}

/// Owned variant of [`View`]
///
/// This structure is used to construct a [`View`] from a string or byte slice.
/// Owned variant of [`View`] for constructing views from a string or byte slice.
///
/// # Example
/// ```
Expand Down Expand Up @@ -198,7 +196,10 @@ impl From<u128> for OwnedView {
/// A view for data where the variable length data is less than or equal to 12.
/// See documentation on [`View`] for details.
///
/// # Notes
/// Note there is no validation done when converting to/from u128
///
/// Equality is based on the bitwise value of the view, not the data it logically points to
#[derive(Copy, Clone, PartialEq)]
pub struct InlineView<'a>(&'a u128);

Expand All @@ -216,27 +217,33 @@ impl<'a> InlineView<'a> {
Self(v)
}

/// return a reference to the u128
/// Return a reference to the u128
pub fn as_u128(self) -> &'a u128 {
self.0
}

/// convert this inline view to a u128
/// Convert this inline view to a u128
pub fn into_u128(self) -> u128 {
*self.0
}

/// The length of the data, in bytes
/// Return the length of the data, in bytes
#[inline(always)]
pub fn len(&self) -> usize {
// take first 4 bytes
let len = *self.0 as u32;
len as usize
}

/// Return true of the length of the data is zero
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Access the value of the data, as bytes
///
/// # Panic
/// # Panics
/// If the length is greater than 12 (aka if this view is invalid)
#[inline(always)]
pub fn as_bytes(&self) -> &[u8] {
Expand All @@ -249,26 +256,20 @@ impl<'a> InlineView<'a> {
/// Undefined behavior if the length is greater than 12
#[inline(always)]
pub unsafe fn as_bytes_unchecked(&self) -> &[u8] {
self.0.to_byte_slice().get_unchecked(4..4 + self.len())
self.get_bytes_unchecked(self.0)
}

/// Access the value of the data, as bytes, unchecked described by this view
/// Access the value of `v`, as bytes, unchecked described by this view
///
/// This is used to retrieve the bytes described by this view directly from
/// a reference to the underlying `u128`.
/// This method can be used to access the inlined bytes described by this
/// view directly from a reference to the underlying `u128`.
///
/// # Safety
/// Undefined behavior if the length is greater than 12
#[inline(always)]
pub unsafe fn get_bytes_unchecked<'b>(&self, v: &'b u128) -> &'b [u8] {
v.to_byte_slice().get_unchecked(4..4 + self.len())
}

/// Is the view zero bytes?
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

impl<'a> From<&'a u128> for InlineView<'a> {
Expand Down Expand Up @@ -302,7 +303,7 @@ impl<'a> From<InlineView<'a>> for u128 {
///
/// # See Also
/// * [`View`] to determine the correct view type for a given `u128`
/// * [`OwnedViewBuilder`] for modifying the buffer index and offset of an `OffsetView`
/// * [`OffsetViewBuilder`] for modifying the buffer index and offset of an `OffsetView`
#[derive(Copy, Clone, PartialEq)]
pub struct OffsetView<'a>(&'a u128);

Expand Down

0 comments on commit 8656ea8

Please sign in to comment.