@@ -6,33 +6,33 @@ use super::{
6
6
Iter ,
7
7
RawBinary ,
8
8
RawBson ,
9
- RawDoc ,
9
+ RawDocument ,
10
10
RawRegex ,
11
11
Result ,
12
12
} ;
13
13
use crate :: { oid:: ObjectId , spec:: ElementType , Bson , DateTime , Timestamp } ;
14
14
15
15
/// A slice of a BSON document containing a BSON array value (akin to [`std::str`]). This can be
16
- /// retrieved from a [`RawDoc `] via [`RawDoc ::get`].
16
+ /// retrieved from a [`RawDocument `] via [`RawDocument ::get`].
17
17
///
18
18
/// This is an _unsized_ type, meaning that it must always be used behind a pointer like `&`.
19
19
///
20
- /// Accessing elements within a [`RawArr `] is similar to element access in [`crate::Document`],
20
+ /// Accessing elements within a [`RawArray `] is similar to element access in [`crate::Document`],
21
21
/// but because the contents are parsed during iteration instead of at creation time, format errors
22
22
/// can happen at any time during use.
23
23
///
24
- /// Iterating over a [`RawArr `] yields either an error or a value that borrows from the
24
+ /// Iterating over a [`RawArray `] yields either an error or a value that borrows from the
25
25
/// original document without making any additional allocations.
26
26
///
27
27
/// ```
28
- /// use bson::{doc, raw::RawDoc };
28
+ /// use bson::{doc, raw::RawDocument };
29
29
///
30
30
/// let doc = doc! {
31
31
/// "x": [1, true, "two", 5.5]
32
32
/// };
33
33
/// let bytes = bson::to_vec(&doc)?;
34
34
///
35
- /// let rawdoc = RawDoc ::new(bytes.as_slice())?;
35
+ /// let rawdoc = RawDocument ::new(bytes.as_slice())?;
36
36
/// let rawarray = rawdoc.get_array("x")?;
37
37
///
38
38
/// for v in rawarray {
@@ -41,44 +41,44 @@ use crate::{oid::ObjectId, spec::ElementType, Bson, DateTime, Timestamp};
41
41
/// # Ok::<(), Box<dyn std::error::Error>>(())
42
42
/// ```
43
43
///
44
- /// Individual elements can be accessed using [`RawArr ::get`] or any of
45
- /// the type-specific getters, such as [`RawArr ::get_object_id`] or
46
- /// [`RawArr ::get_str`]. Note that accessing elements is an O(N) operation, as it
44
+ /// Individual elements can be accessed using [`RawArray ::get`] or any of
45
+ /// the type-specific getters, such as [`RawArray ::get_object_id`] or
46
+ /// [`RawArray ::get_str`]. Note that accessing elements is an O(N) operation, as it
47
47
/// requires iterating through the array from the beginning to find the requested index.
48
48
///
49
49
/// ```
50
50
/// # use bson::raw::{ValueAccessError};
51
- /// use bson::{doc, raw::RawDoc };
51
+ /// use bson::{doc, raw::RawDocument };
52
52
///
53
53
/// let doc = doc! {
54
54
/// "x": [1, true, "two", 5.5]
55
55
/// };
56
56
/// let bytes = bson::to_vec(&doc)?;
57
57
///
58
- /// let rawdoc = RawDoc ::new(bytes.as_slice())?;
58
+ /// let rawdoc = RawDocument ::new(bytes.as_slice())?;
59
59
/// let rawarray = rawdoc.get_array("x")?;
60
60
///
61
61
/// assert_eq!(rawarray.get_bool(1)?, true);
62
62
/// # Ok::<(), Box<dyn std::error::Error>>(())
63
63
/// ```
64
64
#[ derive( PartialEq ) ]
65
65
#[ repr( transparent) ]
66
- pub struct RawArr {
67
- pub ( crate ) doc : RawDoc ,
66
+ pub struct RawArray {
67
+ pub ( crate ) doc : RawDocument ,
68
68
}
69
69
70
- impl RawArr {
71
- pub ( crate ) fn from_doc ( doc : & RawDoc ) -> & RawArr {
70
+ impl RawArray {
71
+ pub ( crate ) fn from_doc ( doc : & RawDocument ) -> & RawArray {
72
72
// SAFETY:
73
73
//
74
74
// Dereferencing a raw pointer requires unsafe due to the potential that the pointer is
75
75
// null, dangling, or misaligned. We know the pointer is not null or dangling due to the
76
- // fact that it's created by a safe reference. Converting &RawDoc to *const
77
- // RawDoc will be properly aligned due to them being references to the same type,
78
- // and converting *const RawDoc to *const RawArr is aligned due to the fact that
79
- // the only field in a RawArr is a RawDoc , meaning the structs are represented
76
+ // fact that it's created by a safe reference. Converting &RawDocument to *const
77
+ // RawDocument will be properly aligned due to them being references to the same type,
78
+ // and converting *const RawDocument to *const RawArray is aligned due to the fact that
79
+ // the only field in a RawArray is a RawDocument , meaning the structs are represented
80
80
// identically at the byte level.
81
- unsafe { & * ( doc as * const RawDoc as * const RawArr ) }
81
+ unsafe { & * ( doc as * const RawDocument as * const RawArray ) }
82
82
}
83
83
84
84
/// Gets a reference to the value at the given index.
@@ -128,13 +128,13 @@ impl RawArr {
128
128
129
129
/// Gets a reference to the document at the given index or returns an error if the
130
130
/// value at that index isn't a document.
131
- pub fn get_document ( & self , index : usize ) -> ValueAccessResult < & RawDoc > {
131
+ pub fn get_document ( & self , index : usize ) -> ValueAccessResult < & RawDocument > {
132
132
self . get_with ( index, ElementType :: EmbeddedDocument , RawBson :: as_document)
133
133
}
134
134
135
135
/// Gets a reference to the array at the given index or returns an error if the
136
136
/// value at that index isn't a array.
137
- pub fn get_array ( & self , index : usize ) -> ValueAccessResult < & RawArr > {
137
+ pub fn get_array ( & self , index : usize ) -> ValueAccessResult < & RawArray > {
138
138
self . get_with ( index, ElementType :: Array , RawBson :: as_array)
139
139
}
140
140
@@ -186,24 +186,24 @@ impl RawArr {
186
186
self . get_with ( index, ElementType :: Int64 , RawBson :: as_i64)
187
187
}
188
188
189
- /// Gets a reference to the raw bytes of the RawArr .
189
+ /// Gets a reference to the raw bytes of the [`RawArray`] .
190
190
pub fn as_bytes ( & self ) -> & [ u8 ] {
191
191
self . doc . as_bytes ( )
192
192
}
193
193
}
194
194
195
- impl std:: fmt:: Debug for RawArr {
195
+ impl std:: fmt:: Debug for RawArray {
196
196
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
197
- f. debug_struct ( "RawArr " )
197
+ f. debug_struct ( "RawArray " )
198
198
. field ( "data" , & hex:: encode ( self . doc . as_bytes ( ) ) )
199
199
. finish ( )
200
200
}
201
201
}
202
202
203
- impl TryFrom < & RawArr > for Vec < Bson > {
203
+ impl TryFrom < & RawArray > for Vec < Bson > {
204
204
type Error = Error ;
205
205
206
- fn try_from ( arr : & RawArr ) -> Result < Vec < Bson > > {
206
+ fn try_from ( arr : & RawArray ) -> Result < Vec < Bson > > {
207
207
arr. into_iter ( )
208
208
. map ( |result| {
209
209
let rawbson = result?;
@@ -213,23 +213,23 @@ impl TryFrom<&RawArr> for Vec<Bson> {
213
213
}
214
214
}
215
215
216
- impl < ' a > IntoIterator for & ' a RawArr {
217
- type IntoIter = RawArrIter < ' a > ;
216
+ impl < ' a > IntoIterator for & ' a RawArray {
217
+ type IntoIter = RawArrayIter < ' a > ;
218
218
type Item = Result < RawBson < ' a > > ;
219
219
220
- fn into_iter ( self ) -> RawArrIter < ' a > {
221
- RawArrIter {
220
+ fn into_iter ( self ) -> RawArrayIter < ' a > {
221
+ RawArrayIter {
222
222
inner : self . doc . into_iter ( ) ,
223
223
}
224
224
}
225
225
}
226
226
227
227
/// An iterator over borrowed raw BSON array values.
228
- pub struct RawArrIter < ' a > {
228
+ pub struct RawArrayIter < ' a > {
229
229
inner : Iter < ' a > ,
230
230
}
231
231
232
- impl < ' a > Iterator for RawArrIter < ' a > {
232
+ impl < ' a > Iterator for RawArrayIter < ' a > {
233
233
type Item = Result < RawBson < ' a > > ;
234
234
235
235
fn next ( & mut self ) -> Option < Result < RawBson < ' a > > > {
0 commit comments