Skip to main content

Value

Struct Value 

Source
pub struct Value(/* private fields */);
Expand description

The CBOR data structure.

Implementations§

Source§

impl Value

Source

pub fn unsigned(int: u64) -> Value

Creates a CBOR unsigned value.

Source

pub fn integer(int: i64) -> Value

Create an appropriate CBOR integer value (uint/nint). For simplicity, this only takes i64. Construct directly for the last bit.

Source

pub fn byte_string(bytes: Vec<u8>) -> Value

Creates a CBOR byte string value.

Source

pub fn text_string(text: String) -> Value

Creates a CBOR text string value.

Source

pub fn array(a: Vec<Value>) -> Value

Create a CBOR array value.

Examples found in repository?
examples/cbor.rs (line 38)
32fn main() {
33    // Build a CBOR object with various different types included. Note that this
34    // object is not built in canonical order.
35    let manual_object = Value::map(vec![
36        (
37            Value::from(1),
38            Value::array(vec![Value::from(2), Value::from(3)]),
39        ),
40        (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41        (Value::from(-2), Value::null_value()),
42        (Value::from(3), Value::bool_value(true)),
43    ]);
44
45    // Build the same object using the crate's convenience macros.
46    let macro_object = cbor_map! {
47        1 => cbor_array![2, 3],
48        "tstr" => cbor_bytes!(vec![1, 2, 3]),
49        -2 => cbor_null!(),
50        3 => cbor_true!(),
51    };
52
53    assert_eq!(manual_object, macro_object);
54    println!("Object {:?}", manual_object);
55
56    // Serialize to bytes.
57    let mut manual_data = vec![];
58    sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59    let hex_manual_data = hexify(&manual_data);
60    let mut macro_data = vec![];
61    sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62    let hex_macro_data = hexify(&macro_data);
63
64    assert_eq!(hex_manual_data, hex_macro_data);
65
66    // Serialized version is in canonical order.
67    println!("Serializes to {}", hex_manual_data);
68    assert_eq!(
69        hex_manual_data,
70        concat!(
71            "a4",         // 4-map
72            "01",         // int(1) =>
73            "820203",     // 2-array [2, 3],
74            "03",         // int(3) =>
75            "f5",         // true,
76            "21",         // nint(-2) =>
77            "f6",         // null,
78            "6474737472", // 4-tstr "tstr" =>
79            "43010203"    // 3-bstr
80        )
81    );
82
83    // Convert back to an object.  This is different than the original object,
84    // because the map is now in canonical order.
85    let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86    println!("Deserializes to {:?}", recovered_object);
87}
Source

pub fn map(m: Vec<(Value, Value)>) -> Value

Create a CBOR map value.

Keys do not have to be sorted.

§Panics

You may not call this function with identical keys in its argument.

Examples found in repository?
examples/cbor.rs (lines 35-43)
32fn main() {
33    // Build a CBOR object with various different types included. Note that this
34    // object is not built in canonical order.
35    let manual_object = Value::map(vec![
36        (
37            Value::from(1),
38            Value::array(vec![Value::from(2), Value::from(3)]),
39        ),
40        (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41        (Value::from(-2), Value::null_value()),
42        (Value::from(3), Value::bool_value(true)),
43    ]);
44
45    // Build the same object using the crate's convenience macros.
46    let macro_object = cbor_map! {
47        1 => cbor_array![2, 3],
48        "tstr" => cbor_bytes!(vec![1, 2, 3]),
49        -2 => cbor_null!(),
50        3 => cbor_true!(),
51    };
52
53    assert_eq!(manual_object, macro_object);
54    println!("Object {:?}", manual_object);
55
56    // Serialize to bytes.
57    let mut manual_data = vec![];
58    sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59    let hex_manual_data = hexify(&manual_data);
60    let mut macro_data = vec![];
61    sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62    let hex_macro_data = hexify(&macro_data);
63
64    assert_eq!(hex_manual_data, hex_macro_data);
65
66    // Serialized version is in canonical order.
67    println!("Serializes to {}", hex_manual_data);
68    assert_eq!(
69        hex_manual_data,
70        concat!(
71            "a4",         // 4-map
72            "01",         // int(1) =>
73            "820203",     // 2-array [2, 3],
74            "03",         // int(3) =>
75            "f5",         // true,
76            "21",         // nint(-2) =>
77            "f6",         // null,
78            "6474737472", // 4-tstr "tstr" =>
79            "43010203"    // 3-bstr
80        )
81    );
82
83    // Convert back to an object.  This is different than the original object,
84    // because the map is now in canonical order.
85    let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86    println!("Deserializes to {:?}", recovered_object);
87}
Source

pub fn tag(int: u64, value: Value) -> Value

Create a CBOR tagged value.

Source

pub fn bool_value(b: bool) -> Value

Create a CBOR boolean simple value.

Examples found in repository?
examples/cbor.rs (line 42)
32fn main() {
33    // Build a CBOR object with various different types included. Note that this
34    // object is not built in canonical order.
35    let manual_object = Value::map(vec![
36        (
37            Value::from(1),
38            Value::array(vec![Value::from(2), Value::from(3)]),
39        ),
40        (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41        (Value::from(-2), Value::null_value()),
42        (Value::from(3), Value::bool_value(true)),
43    ]);
44
45    // Build the same object using the crate's convenience macros.
46    let macro_object = cbor_map! {
47        1 => cbor_array![2, 3],
48        "tstr" => cbor_bytes!(vec![1, 2, 3]),
49        -2 => cbor_null!(),
50        3 => cbor_true!(),
51    };
52
53    assert_eq!(manual_object, macro_object);
54    println!("Object {:?}", manual_object);
55
56    // Serialize to bytes.
57    let mut manual_data = vec![];
58    sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59    let hex_manual_data = hexify(&manual_data);
60    let mut macro_data = vec![];
61    sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62    let hex_macro_data = hexify(&macro_data);
63
64    assert_eq!(hex_manual_data, hex_macro_data);
65
66    // Serialized version is in canonical order.
67    println!("Serializes to {}", hex_manual_data);
68    assert_eq!(
69        hex_manual_data,
70        concat!(
71            "a4",         // 4-map
72            "01",         // int(1) =>
73            "820203",     // 2-array [2, 3],
74            "03",         // int(3) =>
75            "f5",         // true,
76            "21",         // nint(-2) =>
77            "f6",         // null,
78            "6474737472", // 4-tstr "tstr" =>
79            "43010203"    // 3-bstr
80        )
81    );
82
83    // Convert back to an object.  This is different than the original object,
84    // because the map is now in canonical order.
85    let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86    println!("Deserializes to {:?}", recovered_object);
87}
Source

pub fn null_value() -> Value

Creates a null value.

Examples found in repository?
examples/cbor.rs (line 41)
32fn main() {
33    // Build a CBOR object with various different types included. Note that this
34    // object is not built in canonical order.
35    let manual_object = Value::map(vec![
36        (
37            Value::from(1),
38            Value::array(vec![Value::from(2), Value::from(3)]),
39        ),
40        (Value::from("tstr".to_owned()), Value::from(vec![1, 2, 3])),
41        (Value::from(-2), Value::null_value()),
42        (Value::from(3), Value::bool_value(true)),
43    ]);
44
45    // Build the same object using the crate's convenience macros.
46    let macro_object = cbor_map! {
47        1 => cbor_array![2, 3],
48        "tstr" => cbor_bytes!(vec![1, 2, 3]),
49        -2 => cbor_null!(),
50        3 => cbor_true!(),
51    };
52
53    assert_eq!(manual_object, macro_object);
54    println!("Object {:?}", manual_object);
55
56    // Serialize to bytes.
57    let mut manual_data = vec![];
58    sk_cbor::writer::write(manual_object, &mut manual_data).unwrap();
59    let hex_manual_data = hexify(&manual_data);
60    let mut macro_data = vec![];
61    sk_cbor::writer::write(macro_object, &mut macro_data).unwrap();
62    let hex_macro_data = hexify(&macro_data);
63
64    assert_eq!(hex_manual_data, hex_macro_data);
65
66    // Serialized version is in canonical order.
67    println!("Serializes to {}", hex_manual_data);
68    assert_eq!(
69        hex_manual_data,
70        concat!(
71            "a4",         // 4-map
72            "01",         // int(1) =>
73            "820203",     // 2-array [2, 3],
74            "03",         // int(3) =>
75            "f5",         // true,
76            "21",         // nint(-2) =>
77            "f6",         // null,
78            "6474737472", // 4-tstr "tstr" =>
79            "43010203"    // 3-bstr
80        )
81    );
82
83    // Convert back to an object.  This is different than the original object,
84    // because the map is now in canonical order.
85    let recovered_object = sk_cbor::reader::read(&manual_data).unwrap();
86    println!("Deserializes to {:?}", recovered_object);
87}
Source

pub fn undefined() -> Value

Creates an undefined value.

Source

pub fn extract_unsigned(self) -> Option<u64>

Source

pub fn extract_integer(self) -> Option<i64>

Source

pub fn extract_byte_string(self) -> Option<Vec<u8>>

Source

pub fn extract_text_string(self) -> Option<String>

Source

pub fn extract_array(self) -> Option<Vec<Value>>

Source

pub fn extract_map(self) -> Option<Vec<(Value, Value)>>

Source

pub fn extract_tag(self) -> Option<(u64, Value)>

Source

pub fn extract_bool(self) -> Option<bool>

Source

pub fn extract_null(self) -> Option<()>

Source

pub fn extract_undefined(self) -> Option<()>

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

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

impl Eq for Value

Source§

impl From<&[u8; 0]> for Value

Source§

fn from(bytes: &[u8; 0]) -> Self

Converts to this type from the input type.
Source§

impl From<&[u8]> for Value

Source§

fn from(bytes: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Value

Source§

fn from(text: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(text: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<(Value, Value)>> for Value

Source§

fn from(map: Vec<(Value, Value)>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Value>> for Value

Source§

fn from(array: Vec<Value>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Value

Source§

fn from(bytes: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(i: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(i: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(i: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(i: i64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(u: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(u: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(u: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(u: u64) -> Self

Converts to this type from the input type.
Source§

impl Ord for Value

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Value

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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> IntoCborValue for T
where Value: From<T>,

Source§

fn into_cbor_value(self) -> Value

Convert self into a CBOR Value, consuming it along the way.
Source§

impl<T> IntoCborValueOption for T
where Value: From<T>,

Source§

fn into_cbor_value_option(self) -> Option<Value>

Convert self into a CBOR Option<Value>, consuming it along the way.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.