pub struct Array(/* private fields */);Expand description
Conversion helper for Value::array.
This type wraps Vec<Value> and provides From implementations
for common collection types, so that Value::array() can accept
them all through a single impl Into<Array> bound.
Supported source types (where T: Into<Value>):
[T; N]— fixed-size array&[T]— slice (requiresT: Copy)Vec<T>— vectorBox<[T]>— boxed slice()— empty array
Elements are converted to Value via their Into<Value>
implementation. This means any type that implements
Into<Value> (integers, strings, booleans, floats, nested
Values, etc.) can be used as elements.
// From a fixed-size array of integers:
let a = Value::array([1, 2, 3]);
// From a Vec of strings:
let b = Value::array(vec!["x", "y"]);
// From a slice:
let items: &[i32] = &[10, 20];
let c = Value::array(items);
// Empty array via ():
let d = Value::array(());
assert_eq!(d.len(), Some(0));Implementations§
Source§impl Array
impl Array
Sourcepub fn into_inner(self) -> Vec<Value>
pub fn into_inner(self) -> Vec<Value>
Unwrap into the inner Vec.
Sourcepub fn from_sequence<I>(items: I) -> Selfwhere
I: IntoIterator<Item = Value>,
pub fn from_sequence<I>(items: I) -> Selfwhere
I: IntoIterator<Item = Value>,
Build an array from an iterator of values.
This is the write-side counterpart of iterating a CBOR sequence
into an array. For the fallible input produced by
SequenceDecoder and
SequenceReader, use
try_from_sequence instead.
let a = Array::from_sequence([Value::from(1), Value::from(2), Value::from(3)]);
assert_eq!(a.get_ref().len(), 3);Sourcepub fn try_from_sequence<I, E>(items: I) -> Result<Self, E>
pub fn try_from_sequence<I, E>(items: I) -> Result<Self, E>
Build an array from a fallible iterator of values, stopping at the first error.
Accepts any IntoIterator<Item = Result<Value, E>>, which
includes both SequenceDecoder
(E = Error) and SequenceReader
(E = IoError).
// Three concatenated CBOR items: 0x01, 0x02, 0x03.
let a: Array = Array::try_from_sequence(
DecodeOptions::new().sequence_decoder(&[0x01, 0x02, 0x03]),
).unwrap();
assert_eq!(a.get_ref().len(), 3);