cbor

Struct CborTagEncode

Source
pub struct CborTagEncode<'a, T: 'a> { /* private fields */ }
Expand description

A special type that can be used to encode CBOR tags.

This is a “special” type because its use is hard-coded into the implementation of the encoder. When encoded, the encoder will make sure that it is properly represented as a CBOR tag.

§Example

This example shows how to encode and decode a custom data type as a CBOR tag:

use cbor::CborTagEncode;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};

struct MyDataStructure {
    data: Vec<u32>,
}

impl Encodable for MyDataStructure {
    fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
        // See a list of tags here:
        // http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml
        //
        // It is OK to choose your own tag number, but it's probably
        // best to choose one that is unassigned in the IANA registry.
        CborTagEncode::new(100_000, &self.data).encode(e)
    }
}

// Note that the special type `CborTagEncode` isn't needed when decoding.
// You can decode into your `MyDataStructure` type directly:
impl Decodable for MyDataStructure {
    fn decode<D: Decoder>(d: &mut D) -> Result<MyDataStructure, D::Error> {
        // Read the tag number and throw it away. YOU MUST DO THIS!
        try!(d.read_u64());
        // The *next* data item is the actual data.
        Ok(MyDataStructure { data: try!(Decodable::decode(d)) })
    }
}

Implementations§

Source§

impl<'a, T> CborTagEncode<'a, T>

Source

pub fn new(tag: u64, data: &'a T) -> CborTagEncode<'a, T>

Create a new value that is encodable as a CBOR tag.

You can see a list of currently assigned tag numbers here: http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml

Note that it is OK to choose your own tag number for your own application specific purpose, but it should probably be one that is currently unassigned in the IANA registry.

tag is the tag number and data is the actual data item to encode.

Trait Implementations§

Source§

impl<'a, T: Clone + 'a> Clone for CborTagEncode<'a, T>

Source§

fn clone(&self) -> CborTagEncode<'a, T>

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<'a, T: Debug + 'a> Debug for CborTagEncode<'a, T>

Source§

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

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

impl<'a, T: Encodable + 'a> Encodable for CborTagEncode<'a, T>

Source§

fn encode<__S: Encoder>(&self, s: &mut __S) -> Result<(), __S::Error>

Serialize a value using an Encoder.
Source§

impl<'a, T: PartialEq + 'a> PartialEq for CborTagEncode<'a, T>

Source§

fn eq(&self, other: &CborTagEncode<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: 'a> StructuralPartialEq for CborTagEncode<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for CborTagEncode<'a, T>

§

impl<'a, T> RefUnwindSafe for CborTagEncode<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for CborTagEncode<'a, T>
where T: Sync,

§

impl<'a, T> Sync for CborTagEncode<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for CborTagEncode<'a, T>

§

impl<'a, T> UnwindSafe for CborTagEncode<'a, T>
where T: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.