pub struct Array { /* private fields */ }Implementations§
Source§impl Array
impl Array
pub fn new(data: &[u8]) -> RawResult<&Array>
Sourcepub unsafe fn new_unchecked(data: &[u8]) -> &Array
pub unsafe fn new_unchecked(data: &[u8]) -> &Array
Return a new Array from the provided bytes.
§Safety
The provided bytes must start with a valid length indicator and end with a NUL terminator, as described in the bson spec.
The following is valid:
// Represents the array [null, 514i32], which is the same as the document
// {"0": null, "1": 514}
let bson = b"\x0f\0\0\0\x0A0\0\x101\0\x02\x02\0\0\0";
let arr = unsafe { Array::new_unchecked(bson) };
let mut arriter = arr.into_iter();
assert!(arriter.next().unwrap().and_then(|b| b.as_null()).is_ok());
assert_eq!(arriter.next().unwrap().and_then(|b| b.as_i32()).unwrap(), 514);And so is this, even though the provided document is not an array, because the errors will be caught during decode.
// Represents the document {"0": null, "X": 514}
let bson = b"\x0f\0\0\0\x0A0\0\x10X\0\x02\x02\0\0\0";
let arr = unsafe { Array::new_unchecked(bson) };
let mut arriter = arr.into_iter();
assert!(arriter.next().unwrap().and_then(|b| b.as_null()).is_ok());
assert!(arriter.next().unwrap().is_err());
assert!(arriter.next().is_none());§Bad:
The following, however, indicates the wrong size for the document, and is therefore unsound.
// Contains a length indicator, that is longer than the array
let invalid = b"\x06\0\0\0\0";
let arr: &Array = unsafe { Array::new_unchecked(invalid) };