pub struct VariantObject<'m, 'v> {
pub metadata: VariantMetadata<'m>,
pub value: &'v [u8],
header: VariantObjectHeader,
num_elements: u32,
first_field_offset_byte: u32,
first_value_byte: u32,
validated: bool,
}
Expand description
A Variant
Object (struct with named fields).
See the Variant spec file for more information.
§Validation
Every instance of variant object is either valid or invalid. depending on whether the underlying bytes are a valid encoding of a variant object subtype (see below).
Instances produced by Self::try_new
or Self::with_full_validation
are fully (and recursively)
validated. They always contain valid data, and infallible accesses such as iteration and
indexing are panic-free. The validation cost is linear in the number of underlying bytes.
Instances produced by Self::new
are unvalidated and so they may contain either valid or
invalid data. Infallible accesses such as iteration and indexing will panic if the underlying
bytes are invalid, and fallible alternatives such as Self::iter_try
and Self::get
are
provided as panic-free alternatives. Self::with_full_validation
can also be used to validate an
unvalidated instance, if desired.
Unvalidated instances can be constructed in constant time. They can be useful if the caller knows the underlying bytes were already validated previously, or if the caller intends to perform a small number of (fallible) field accesses against a large object.
A validated instance guarantees that:
- header byte is valid
- num_elements is in bounds
- field id array is in bounds
- field offset array is in bounds
- field value array is in bounds
- all field ids are valid metadata dictionary entries (*)
- field ids are lexically ordered according by their corresponding string values (*)
- all field offsets are in bounds (*)
- all field values are (recursively) valid variant values (*)
- the associated variant metadata is valid (*)
NOTE: Self::new
only skips expensive (non-constant cost) validation checks (marked by (*)
in the list above); it panics any of the other checks fails.
§Safety
Even an invalid variant object instance is still safe to use in the Rust sense. Accessing it with infallible methods may cause panics but will never lead to undefined behavior.
Fields§
§metadata: VariantMetadata<'m>
§value: &'v [u8]
§header: VariantObjectHeader
§num_elements: u32
§first_field_offset_byte: u32
§first_value_byte: u32
§validated: bool
Implementations§
Source§impl<'m, 'v> VariantObject<'m, 'v>
impl<'m, 'v> VariantObject<'m, 'v>
pub fn new(metadata: VariantMetadata<'m>, value: &'v [u8]) -> Self
Sourcepub fn try_new(
metadata: VariantMetadata<'m>,
value: &'v [u8],
) -> Result<Self, ArrowError>
pub fn try_new( metadata: VariantMetadata<'m>, value: &'v [u8], ) -> Result<Self, ArrowError>
Attempts to interpet metadata
and value
as a variant object.
§Validation
This constructor verifies that value
points to a valid variant object value. In
particular, that all field ids exist in metadata
, and all offsets are in-bounds and point
to valid objects.
Sourcepub(crate) fn try_new_with_shallow_validation(
metadata: VariantMetadata<'m>,
value: &'v [u8],
) -> Result<Self, ArrowError>
pub(crate) fn try_new_with_shallow_validation( metadata: VariantMetadata<'m>, value: &'v [u8], ) -> Result<Self, ArrowError>
Attempts to interpet metadata
and value
as a variant object, performing only basic
(constant-cost) validation.
Sourcepub fn is_fully_validated(&self) -> bool
pub fn is_fully_validated(&self) -> bool
True if this instance is fully validated for panic-free infallible accesses.
Sourcepub fn with_full_validation(self) -> Result<Self, ArrowError>
pub fn with_full_validation(self) -> Result<Self, ArrowError>
Performs a full validation of this variant object.
Sourcepub fn try_field(&self, i: usize) -> Result<Variant<'m, 'v>, ArrowError>
pub fn try_field(&self, i: usize) -> Result<Variant<'m, 'v>, ArrowError>
Fallible version of field
. Returns field value by index, capturing validation errors
fn try_field_with_shallow_validation( &self, i: usize, ) -> Result<Variant<'m, 'v>, ArrowError>
fn get_offset(&self, i: usize) -> Result<u32, ArrowError>
Sourcepub fn field_name(&self, i: usize) -> Option<&'m str>
pub fn field_name(&self, i: usize) -> Option<&'m str>
Get a field’s name by index in 0..self.len()
§Panics
If the variant object is corrupted (e.g., invalid offsets or field IDs). This should never happen since the constructor validates all data upfront.
Sourcefn try_field_name(&self, i: usize) -> Result<&'m str, ArrowError>
fn try_field_name(&self, i: usize) -> Result<&'m str, ArrowError>
Fallible version of field_name
. Returns field name by index, capturing validation errors
Sourcepub fn iter(&self) -> impl Iterator<Item = (&'m str, Variant<'m, 'v>)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&'m str, Variant<'m, 'v>)> + '_
Returns an iterator of (name, value) pairs over the fields of this object.
Sourcepub fn iter_try(
&self,
) -> impl Iterator<Item = Result<(&'m str, Variant<'m, 'v>), ArrowError>> + '_
pub fn iter_try( &self, ) -> impl Iterator<Item = Result<(&'m str, Variant<'m, 'v>), ArrowError>> + '_
Fallible iteration over the fields of this object.
fn iter_try_with_shallow_validation( &self, ) -> impl Iterator<Item = Result<(&'m str, Variant<'m, 'v>), ArrowError>> + '_
Trait Implementations§
Source§impl<'m, 'v> Clone for VariantObject<'m, 'v>
impl<'m, 'v> Clone for VariantObject<'m, 'v>
Source§fn clone(&self) -> VariantObject<'m, 'v>
fn clone(&self) -> VariantObject<'m, 'v>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more