Struct cbor_data::CborOwned

source ·
pub struct CborOwned(/* private fields */);
Expand description

Wrapper around a vector of bytes, for parsing as CBOR.

For details on the format see RFC 8949.

When interpreting CBOR messages from the outside (e.g. from the network) it is advisable to ingest those using the canonical constructor. In case the message was encoded for example using CborBuilder it is sufficient to use the trusting constructor.

Canonicalisation rqeuires an intermediary data buffer, which can be supplied (and reused) by the caller to save on allocations.

Implementations§

source§

impl CborOwned

source

pub fn unchecked(bytes: impl Into<SmallVec<[u8; 16]>>) -> Self

Copy the bytes and wrap for indexing.

No checks on the integrity are made, indexing methods may panic if encoded lengths are out of bound. If you want to carefully treat data obtained from unreliable sources, prefer canonical().

source

pub fn canonical(bytes: impl AsRef<[u8]>) -> Result<Self, ParseError>

Copy the bytes while checking for integrity and replacing indefinite (byte) strings with definite ones.

This constructor will go through and decode the whole provided CBOR bytes and write them into a vector, thereby

  • writing large arrays and dicts using indefinite size format
  • writing numbers in their smallest form

For more configuration options like reusing a scratch space or preferring definite size encoding see CborBuilder.

source

pub fn into_vec(self) -> Vec<u8>

Hand out the underlying SmallVec as a Vec

Will only allocate if the item is at most 16 bytes long.

Methods from Deref<Target = Cbor>§

source

pub fn as_slice(&self) -> &[u8]

A view onto the underlying bytes

source

pub fn decode(&self) -> CborValue<'_>

Interpret the CBOR item at a higher level

While kind gives you precise information on how the item is encoded, this method interprets the tag-based encoding according to the standard, adding for example big integers, decimals, and floats, or turning base64-encoded text strings into binary strings.

source

pub fn tags(&self) -> Tags<'_>

An iterator over the tags present on this item, from outermost to innermost

source

pub fn kind(&self) -> ItemKind<'_>

The low-level encoding of this item, without its tags

source

pub fn tagged_item(&self) -> TaggedItem<'_>

The low-level encoding of this item with its tags

source

pub fn try_null(&self) -> Result<(), TypeError>

More efficient shortcut for .decode().is_null() with error reporting.

source

pub fn try_bool(&self) -> Result<bool, TypeError>

More efficient shortcut for .decode().as_bool() with error reporting.

source

pub fn try_number(&self) -> Result<Number<'_>, TypeError>

More efficient shortcut for .decode().to_number() with error reporting.

source

pub fn try_timestamp(&self) -> Result<Timestamp, TypeError>

More efficient shortcut for .decode().as_timestamp() with error reporting.

source

pub fn try_bytes(&self) -> Result<Cow<'_, [u8]>, TypeError>

More efficient shortcut for .decode().to_bytes() with error reporting.

source

pub fn try_str(&self) -> Result<Cow<'_, str>, TypeError>

More efficient shortcut for .decode().to_str() with error reporting.

source

pub fn try_array(&self) -> Result<Vec<Cow<'_, Cbor>>, TypeError>

More efficient shortcut for .decode().to_array() with error reporting.

source

pub fn try_dict( &self ) -> Result<BTreeMap<Cow<'_, Cbor>, Cow<'_, Cbor>>, TypeError>

More efficient shortcut for .decode().to_dict() with error reporting.

source

pub fn index<'a, 'b>( &'a self, path: impl IntoIterator<Item = PathElement<'b>> ) -> Option<Cow<'a, Cbor>>

Extract a value by indexing into arrays and dicts, with path elements yielded by an iterator.

Returns None if an index doesn’t exist or the indexed object is neither an array nor a dict. When the object under consideration is an array, the next path element must represent an integer number.

Providing an empty iterator will yield the current Cbor item.

Returns a borrowed Cbor unless the traversal entered a TAG_CBOR_ITEM byte string with indefinite encoding (in which case the bytes need to be assembled into a Vec before continuing). This cannot happen if the item being indexed stems from CborOwned::canonical.

source

pub fn index_borrowed<'a, 'b>( &'a self, path: impl IntoIterator<Item = PathElement<'b>> ) -> Option<&'a Cbor>

Extract a value by indexing into arrays and dicts, with path elements yielded by an iterator.

Returns None if an index doesn’t exist or the indexed object is neither an array nor a dict. When the object under consideration is an array, the next path element must represent an integer number.

Providing an empty iterator will yield the current Cbor item.

§Panics

Panics if this CBOR item contains a TAG_CBOR_ITEM byte string that has been index into by this path traversal. Use CborOwned::canonical to ensure that this cannot happen.

source

pub fn visit<'a, 'b, Err, V: Visitor<'a, Err> + 'b>( &'a self, visitor: &'b mut V ) -> Result<(), Err>

Visit the interesting parts of this CBOR item as guided by the given Visitor.

Returns false if the visit was not even begun due to invalid or non-canonical CBOR.

Trait Implementations§

source§

impl AsRef<[u8]> for CborOwned

source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<Cbor> for CborOwned

source§

fn as_ref(&self) -> &Cbor

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<Cbor> for CborOwned

source§

fn borrow(&self) -> &Cbor

Immutably borrows from an owned value. Read more
source§

impl Clone for CborOwned

source§

fn clone(&self) -> CborOwned

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for CborOwned

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for CborOwned

§

type Target = Cbor

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Display for CborOwned

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for CborOwned

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for CborOwned

source§

fn cmp(&self, other: &CborOwned) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for CborOwned

source§

fn eq(&self, other: &CborOwned) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for CborOwned

source§

fn partial_cmp(&self, other: &CborOwned) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl ReadCbor for CborOwned

source§

fn fmt(f: &mut impl Write) -> Result

source§

fn read_cbor_impl(cbor: &Cbor) -> Result<Self>
where Self: Sized,

source§

fn name() -> String

source§

fn read_cbor(cbor: &Cbor) -> Result<Self>
where Self: Sized,

source§

impl TryFrom<&[u8]> for CborOwned

§

type Error = ParseError

The type returned in the event of a conversion error.
source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl WriteCbor for CborOwned

source§

fn write_cbor<W: Writer>(&self, w: W) -> W::Output

source§

impl Eq for CborOwned

source§

impl StructuralPartialEq for CborOwned

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.