Tag

Struct Tag 

Source
pub struct Tag { /* private fields */ }
Expand description

Represents a CBOR tag (major type 6) with optional associated name.

Tags in CBOR provide semantic information about the tagged data item. They are used to indicate that the data item has some additional semantics beyond its basic CBOR type. For example, a tag might indicate that a string should be interpreted as a date, or that a byte string contains embedded CBOR.

This implementation supports both the numeric tag value and an optional human-readable name to improve code clarity and debugging.

§Tag equality and comparison

Tags are considered equal if their numeric values are equal, regardless of their names. This matches the CBOR specification behavior where the tag value (not the name) determines the semantic meaning.

§Deterministic encoding

Tags, like all other CBOR types, must follow deterministic encoding rules in dCBOR. The tag value is encoded according to the general integer encoding rules (shortest form possible), and the tagged content itself must also follow deterministic encoding rules.

§Examples

use dcbor::prelude::*;

// Create a tag with a name
let epoch_time_tag = Tag::new(1, "epoch-time");
assert_eq!(epoch_time_tag.value(), 1);
assert_eq!(epoch_time_tag.name(), Some("epoch-time".to_string()));

// Create a tag without a name
let unnamed_tag = Tag::with_value(42);
assert_eq!(unnamed_tag.value(), 42);
assert_eq!(unnamed_tag.name(), None);

// Create a tag at compile time with a static name
const REGISTERED_TAG: Tag = Tag::with_static_name(32, "uri");
assert_eq!(REGISTERED_TAG.value(), 32);
assert_eq!(REGISTERED_TAG.name(), Some("uri".to_string()));

// Tags with the same value are equal, even with different names
let tag1 = Tag::new(42, "meaning");
let tag2 = Tag::with_value(42);
assert_eq!(tag1, tag2);

Implementations§

Source§

impl Tag

Source

pub fn new(value: u64, name: impl Into<String>) -> Tag

Creates a new CBOR tag with the given value and associated name.

This constructor allocates memory for the tag name, storing it as a dynamic string. If the tag name is known at compile time, consider using with_static_name instead.

§Parameters
  • value - The numeric tag value, typically registered with IANA
  • name - A human-readable name for the tag, improving code readability
§Examples
use dcbor::prelude::*;

let date_tag = Tag::new(12345, "This is a tagged string.");
assert_eq!(date_tag.value(), 12345);
assert_eq!(
    date_tag.name(),
    Some("This is a tagged string.".to_string())
);
Source

pub const fn with_value(value: u64) -> Tag

Creates a new CBOR tag with the given value and no name.

This constructor creates an unnamed tag, which is sufficient for many use cases but may be less readable in code compared to named tags.

§Parameters
  • value - The numeric tag value, typically registered with IANA
§Examples
use dcbor::prelude::*;

let tag = Tag::with_value(42);
assert_eq!(tag.value(), 42);
assert_eq!(tag.name(), None);
Source

pub const fn with_static_name(value: u64, name: &'static str) -> Tag

Creates a new CBOR tag at compile time with the given value and associated name.

This constructor is optimized for cases where the tag name is known at compile time, avoiding runtime allocations for the name string. It can be used in const contexts.

§Parameters
  • value - The numeric tag value, typically registered with IANA
  • name - A static string literal as the human-readable name for the tag
§Examples
use dcbor::prelude::*;

const DATE_TAG: Tag = Tag::with_static_name(0, "date-time-string");
assert_eq!(DATE_TAG.value(), 0);
assert_eq!(DATE_TAG.name(), Some("date-time-string".to_string()));
Source

pub fn value(&self) -> u64

Returns the numeric value of the tag.

The tag value is the primary identifier for a tag in CBOR and determines its semantic meaning according to the IANA CBOR Tags registry.

§Examples
use dcbor::prelude::*;

let tag = Tag::new(18, "cbor-data-item");
assert_eq!(tag.value(), 18);
Source

pub fn name(&self) -> Option<String>

Returns the tag’s associated human-readable name, if any.

§Returns
  • Some(String) - The tag’s name if it has one
  • None - If the tag was created without a name
§Examples
use dcbor::prelude::*;

let named_tag = Tag::new(32, "uri");
assert_eq!(named_tag.name(), Some("uri".to_string()));

let unnamed_tag = Tag::with_value(32);
assert_eq!(unnamed_tag.name(), None);

Trait Implementations§

Source§

impl Clone for Tag

Source§

fn clone(&self) -> Tag

Returns a duplicate 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 Tag

Source§

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

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

impl Display for Tag

Formats a tag for display, preferring the name if available.

If the tag has a name, the name will be displayed. Otherwise, the numeric value will be displayed.

§Examples

use dcbor::prelude::*;

let named_tag = Tag::new(32, "uri");
let unnamed_tag = Tag::with_value(42);

assert_eq!(named_tag.to_string(), "uri");
assert_eq!(unnamed_tag.to_string(), "42");
Source§

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

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

impl From<&Tag> for Tag

Converts a reference to a Tag into an owned Tag instance.

Source§

fn from(tag: &Tag) -> Tag

Converts to this type from the input type.
Source§

impl From<i32> for Tag

Converts an i32 integer to a Tag instance.

This provides a convenient way to create unnamed tags from 32-bit signed integers. Note that the value will be converted to an unsigned 64-bit integer internally.

§Examples

use dcbor::prelude::*;

let tag: Tag = 42i32.into();
assert_eq!(tag.value(), 42);
Source§

fn from(value: i32) -> Tag

Converts to this type from the input type.
Source§

impl From<u64> for Tag

Converts a raw tag value to a Tag instance.

This provides a convenient way to create unnamed tags directly from numeric values.

§Examples

use dcbor::prelude::*;

// These are equivalent:
let tag1: Tag = 42.into();
let tag2 = Tag::with_value(42);

assert_eq!(tag1, tag2);
Source§

fn from(value: u64) -> Tag

Converts to this type from the input type.
Source§

impl From<usize> for Tag

Converts a usize to a Tag instance.

This provides a convenient way to create unnamed tags from platform-specific sized unsigned integers. Note that on platforms where usize is larger than 64 bits, values that don’t fit in 64 bits will be truncated.

§Examples

use dcbor::prelude::*;

let tag: Tag = 42usize.into();
assert_eq!(tag.value(), 42);
Source§

fn from(value: usize) -> Tag

Converts to this type from the input type.
Source§

impl Hash for Tag

Implements hashing for Tag based solely on the numeric tag value.

This implementation ensures that two tags with the same value but different names will hash to the same value, which is consistent with the equality implementation. This allows tags to be used as keys in hash-based collections.

Source§

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

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 PartialEq for Tag

Compares tags for equality based on their numeric values.

Tags are considered equal if they have the same numeric value, regardless of whether they have different names or if one has a name and the other doesn’t. This matches the CBOR standard behavior where the tag value, not its human-readable name, determines its semantic meaning.

§Examples

use dcbor::prelude::*;

let tag1 = Tag::new(32, "uri");
let tag2 = Tag::with_value(32);
let tag3 = Tag::new(32, "different-name");
let tag4 = Tag::with_value(42);

assert_eq!(tag1, tag2); // Same value, one named and one unnamed
assert_eq!(tag1, tag3); // Same value, different names
assert_ne!(tag1, tag4); // Different values
Source§

fn eq(&self, other: &Tag) -> 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 Eq for Tag

Confirms Tag implements full equality, not just partial equality.

This is required for Tag to be used in collections like HashSet and as keys in HashMap along with the Hash implementation.

Auto Trait Implementations§

§

impl Freeze for Tag

§

impl RefUnwindSafe for Tag

§

impl Send for Tag

§

impl Sync for Tag

§

impl Unpin for Tag

§

impl UnwindSafe for Tag

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, dest: *mut u8)

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

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

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V