Struct bson::raw::RawArrayBuf
source · pub struct RawArrayBuf { /* private fields */ }Expand description
An owned BSON array value (akin to std::path::PathBuf), backed by a buffer of raw BSON
bytes. This type can be used to construct owned array values, which can be used to append to
RawDocumentBuf or as a field in a Deserialize struct.
Iterating over a RawArrayBuf yields either an error or a RawBson value that borrows from
the original document without making any additional allocations.
use bson::raw::RawArrayBuf;
let mut array = RawArrayBuf::new();
array.push("a string");
array.push(12_i32);
let mut iter = array.into_iter();
let value = iter.next().unwrap()?;
assert_eq!(value.as_str(), Some("a string"));
let value = iter.next().unwrap()?;
assert_eq!(value.as_i32(), Some(12));
assert!(iter.next().is_none());This type implements Deref to RawArray, meaning that all methods on
RawArray are available on RawArrayBuf values as well. This includes RawArray::get or
any of the type-specific getters, such as RawArray::get_object_id or RawArray::get_str.
Note that accessing elements is an O(N) operation, as it requires iterating through the document
from the beginning to find the requested key.
Implementations§
source§impl RawArrayBuf
impl RawArrayBuf
sourcepub fn new() -> RawArrayBuf
pub fn new() -> RawArrayBuf
Construct a new, empty RawArrayBuf.
sourcepub fn push(&mut self, value: impl Into<RawBson>)
pub fn push(&mut self, value: impl Into<RawBson>)
Append a value to the end of the array.
use bson::raw::{RawArrayBuf, RawDocumentBuf};
let mut array = RawArrayBuf::new();
array.push("a string");
array.push(12_i32);
let mut doc = RawDocumentBuf::new();
doc.append("a key", "a value");
array.push(doc.clone());
let mut iter = array.into_iter();
let value = iter.next().unwrap()?;
assert_eq!(value.as_str(), Some("a string"));
let value = iter.next().unwrap()?;
assert_eq!(value.as_i32(), Some(12));
let value = iter.next().unwrap()?;
assert_eq!(value.as_document(), Some(doc.as_ref()));
assert!(iter.next().is_none());Methods from Deref<Target = RawArray>§
sourcepub fn to_raw_array_buf(&self) -> RawArrayBuf
pub fn to_raw_array_buf(&self) -> RawArrayBuf
Convert this borrowed RawArray into an owned RawArrayBuf.
This involves a traversal of the array to count the values.
sourcepub fn get(&self, index: usize) -> Result<Option<RawBsonRef<'_>>>
pub fn get(&self, index: usize) -> Result<Option<RawBsonRef<'_>>>
Gets a reference to the value at the given index.
sourcepub fn get_f64(&self, index: usize) -> ValueAccessResult<f64>
pub fn get_f64(&self, index: usize) -> ValueAccessResult<f64>
Gets the BSON double at the given index or returns an error if the value at that index isn’t a double.
sourcepub fn get_str(&self, index: usize) -> ValueAccessResult<&str>
pub fn get_str(&self, index: usize) -> ValueAccessResult<&str>
Gets a reference to the string at the given index or returns an error if the value at that index isn’t a string.
sourcepub fn get_document(&self, index: usize) -> ValueAccessResult<&RawDocument>
pub fn get_document(&self, index: usize) -> ValueAccessResult<&RawDocument>
Gets a reference to the document at the given index or returns an error if the value at that index isn’t a document.
sourcepub fn get_array(&self, index: usize) -> ValueAccessResult<&RawArray>
pub fn get_array(&self, index: usize) -> ValueAccessResult<&RawArray>
Gets a reference to the array at the given index or returns an error if the value at that index isn’t a array.
sourcepub fn get_binary(&self, index: usize) -> ValueAccessResult<RawBinaryRef<'_>>
pub fn get_binary(&self, index: usize) -> ValueAccessResult<RawBinaryRef<'_>>
Gets a reference to the BSON binary value at the given index or returns an error if the value at that index isn’t a binary.
sourcepub fn get_object_id(&self, index: usize) -> ValueAccessResult<ObjectId>
pub fn get_object_id(&self, index: usize) -> ValueAccessResult<ObjectId>
Gets the ObjectId at the given index or returns an error if the value at that index isn’t an ObjectId.
sourcepub fn get_bool(&self, index: usize) -> ValueAccessResult<bool>
pub fn get_bool(&self, index: usize) -> ValueAccessResult<bool>
Gets the boolean at the given index or returns an error if the value at that index isn’t a boolean.
sourcepub fn get_datetime(&self, index: usize) -> ValueAccessResult<DateTime>
pub fn get_datetime(&self, index: usize) -> ValueAccessResult<DateTime>
Gets the DateTime at the given index or returns an error if the value at that index isn’t a DateTime.
sourcepub fn get_regex(&self, index: usize) -> ValueAccessResult<RawRegexRef<'_>>
pub fn get_regex(&self, index: usize) -> ValueAccessResult<RawRegexRef<'_>>
Gets a reference to the BSON regex at the given index or returns an error if the value at that index isn’t a regex.
sourcepub fn get_timestamp(&self, index: usize) -> ValueAccessResult<Timestamp>
pub fn get_timestamp(&self, index: usize) -> ValueAccessResult<Timestamp>
Gets a reference to the BSON timestamp at the given index or returns an error if the value at that index isn’t a timestamp.
sourcepub fn get_i32(&self, index: usize) -> ValueAccessResult<i32>
pub fn get_i32(&self, index: usize) -> ValueAccessResult<i32>
Gets the BSON int32 at the given index or returns an error if the value at that index isn’t a 32-bit integer.
sourcepub fn get_i64(&self, index: usize) -> ValueAccessResult<i64>
pub fn get_i64(&self, index: usize) -> ValueAccessResult<i64>
Gets BSON int64 at the given index or returns an error if the value at that index isn’t a 64-bit integer.
Trait Implementations§
source§impl AsRef<RawArray> for RawArrayBuf
impl AsRef<RawArray> for RawArrayBuf
source§impl Borrow<RawArray> for RawArrayBuf
impl Borrow<RawArray> for RawArrayBuf
source§impl Clone for RawArrayBuf
impl Clone for RawArrayBuf
source§fn clone(&self) -> RawArrayBuf
fn clone(&self) -> RawArrayBuf
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for RawArrayBuf
impl Debug for RawArrayBuf
source§impl Default for RawArrayBuf
impl Default for RawArrayBuf
source§impl Deref for RawArrayBuf
impl Deref for RawArrayBuf
source§impl<'de> Deserialize<'de> for RawArrayBuf
impl<'de> Deserialize<'de> for RawArrayBuf
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl<'a> From<&'a RawArrayBuf> for Cow<'a, RawArray>
impl<'a> From<&'a RawArrayBuf> for Cow<'a, RawArray>
source§fn from(rd: &'a RawArrayBuf) -> Self
fn from(rd: &'a RawArrayBuf) -> Self
source§impl<'a> From<&'a RawArrayBuf> for RawBsonRef<'a>
impl<'a> From<&'a RawArrayBuf> for RawBsonRef<'a>
source§fn from(a: &'a RawArrayBuf) -> Self
fn from(a: &'a RawArrayBuf) -> Self
source§impl<'a> From<RawArrayBuf> for Cow<'a, RawArray>
impl<'a> From<RawArrayBuf> for Cow<'a, RawArray>
source§fn from(rd: RawArrayBuf) -> Self
fn from(rd: RawArrayBuf) -> Self
source§impl From<RawArrayBuf> for RawBson
impl From<RawArrayBuf> for RawBson
source§fn from(a: RawArrayBuf) -> Self
fn from(a: RawArrayBuf) -> Self
source§impl<T: Into<RawBson>> FromIterator<T> for RawArrayBuf
impl<T: Into<RawBson>> FromIterator<T> for RawArrayBuf
source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
source§impl<'a> IntoIterator for &'a RawArrayBuf
impl<'a> IntoIterator for &'a RawArrayBuf
§type IntoIter = RawArrayIter<'a>
type IntoIter = RawArrayIter<'a>
§type Item = Result<RawBsonRef<'a>, Error>
type Item = Result<RawBsonRef<'a>, Error>
source§impl PartialEq for RawArrayBuf
impl PartialEq for RawArrayBuf
source§fn eq(&self, other: &RawArrayBuf) -> bool
fn eq(&self, other: &RawArrayBuf) -> bool
self and other values to be equal, and is used
by ==.source§impl Serialize for RawArrayBuf
impl Serialize for RawArrayBuf
impl StructuralPartialEq for RawArrayBuf
Auto Trait Implementations§
impl RefUnwindSafe for RawArrayBuf
impl Send for RawArrayBuf
impl Sync for RawArrayBuf
impl Unpin for RawArrayBuf
impl UnwindSafe for RawArrayBuf
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self to use its Binary implementation when Debug-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self to use its Display implementation when
Debug-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self to use its Octal implementation when Debug-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_ref() into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_mut() into the pipe
function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B> of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B> of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R> view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R> view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow() only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut() only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref() only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut() only in debug builds, and is erased in release
builds.