Skip to content

Commit 194177a

Browse files
authored
RUST-1992 Updates to allow the driver to be updated (#571)
1 parent 7703fee commit 194177a

File tree

7 files changed

+138
-27
lines changed

7 files changed

+138
-27
lines changed

src/de/raw.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'de> serde::de::MapAccess<'de> for DocumentAccess<'de> {
363363
match &self.elem {
364364
None => Ok(None),
365365
Some(elem) => seed
366-
.deserialize(BorrowedStrDeserializer::new(elem.key()))
366+
.deserialize(BorrowedStrDeserializer::new(elem.key().as_str()))
367367
.map(Some),
368368
}
369369
}
@@ -421,7 +421,8 @@ impl<'de> serde::de::EnumAccess<'de> for DocumentAccess<'de> {
421421
Some(e) => e,
422422
None => return Err(Error::end_of_stream()),
423423
};
424-
let de: BorrowedStrDeserializer<'_, Error> = BorrowedStrDeserializer::new(elem.key());
424+
let de: BorrowedStrDeserializer<'_, Error> =
425+
BorrowedStrDeserializer::new(elem.key().as_str());
425426
let key = seed.deserialize(de)?;
426427
Ok((key, self))
427428
}

src/raw.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
//! ```rust
8989
//! use bson::{
9090
//! raw::{
91+
//! cstr,
92+
//! CStr,
9193
//! RawBsonRef,
9294
//! RawDocumentBuf,
9395
//! },
@@ -102,12 +104,12 @@
102104
//! let doc = RawDocumentBuf::from_document(&original_doc)?;
103105
//! let mut doc_iter = doc.iter();
104106
//!
105-
//! let (key, value): (&str, RawBsonRef) = doc_iter.next().unwrap()?;
106-
//! assert_eq!(key, "crate");
107+
//! let (key, value): (&CStr, RawBsonRef) = doc_iter.next().unwrap()?;
108+
//! assert_eq!(key, cstr!("crate"));
107109
//! assert_eq!(value.as_str(), Some("bson"));
108110
//!
109-
//! let (key, value): (&str, RawBsonRef) = doc_iter.next().unwrap()?;
110-
//! assert_eq!(key, "year");
111+
//! let (key, value): (&CStr, RawBsonRef) = doc_iter.next().unwrap()?;
112+
//! assert_eq!(key, cstr!("year"));
111113
//! assert_eq!(value.as_str(), Some("2021"));
112114
//! # Ok::<(), bson::error::Error>(())
113115
//! ```

src/raw/cstr.rs

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::error::{Error, Result};
2727
/// // bson::raw::CStr does not:
2828
/// let invalid: &bson::raw::CStr = cstr!("foo\0bar"); // will not compile
2929
/// ```
30-
#[derive(Debug)]
30+
#[derive(Debug, Eq)]
3131
#[repr(transparent)]
3232
pub struct CStr {
3333
data: [u8],
@@ -76,18 +76,70 @@ impl CStr {
7676
self.as_str().is_empty()
7777
}
7878

79+
/// Returns the lowercase equivalent of this as a new [`CString`].
80+
pub fn to_lowercase(&self) -> CString {
81+
CString::from_string_unchecked(self.as_str().to_lowercase())
82+
}
83+
84+
/// Returns the uppercase equivalent of this as a new [`CString`].
85+
pub fn to_uppercase(&self) -> CString {
86+
CString::from_string_unchecked(self.as_str().to_uppercase())
87+
}
88+
7989
pub(crate) fn append_to(&self, buf: &mut Vec<u8>) {
8090
buf.extend(&self.data);
8191
buf.push(0);
8292
}
8393
}
8494

85-
impl PartialEq<&CStr> for &CStr {
86-
fn eq(&self, other: &&CStr) -> bool {
95+
impl PartialEq for CStr {
96+
fn eq(&self, other: &CStr) -> bool {
97+
self.as_str() == other.as_str()
98+
}
99+
}
100+
101+
impl PartialEq<str> for CStr {
102+
fn eq(&self, other: &str) -> bool {
103+
self.as_str() == other
104+
}
105+
}
106+
107+
impl PartialEq<CString> for CStr {
108+
fn eq(&self, other: &CString) -> bool {
109+
self.as_str() == other.as_str()
110+
}
111+
}
112+
113+
impl PartialEq<String> for CStr {
114+
fn eq(&self, other: &String) -> bool {
87115
self.as_str() == other.as_str()
88116
}
89117
}
90118

119+
impl std::hash::Hash for CStr {
120+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
121+
self.as_str().hash(state);
122+
}
123+
}
124+
125+
impl Ord for CStr {
126+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
127+
self.as_str().cmp(other.as_str())
128+
}
129+
}
130+
131+
impl PartialOrd for CStr {
132+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
133+
Some(self.cmp(other))
134+
}
135+
}
136+
137+
impl std::fmt::Display for CStr {
138+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139+
self.as_str().fmt(f)
140+
}
141+
}
142+
91143
impl std::borrow::ToOwned for CStr {
92144
type Owned = CString;
93145

@@ -108,6 +160,12 @@ impl AsRef<str> for CStr {
108160
}
109161
}
110162

163+
impl<'a> From<&'a CStr> for &'a str {
164+
fn from(value: &'a CStr) -> Self {
165+
value.as_str()
166+
}
167+
}
168+
111169
#[cfg(feature = "serde")]
112170
impl serde::Serialize for &CStr {
113171
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
@@ -175,7 +233,7 @@ pub use cstr;
175233
///
176234
/// Like `CStr`, this differs from [`std::ffi::CString`] in that it is required to be valid UTF-8,
177235
/// and does not include the nul terminator in the buffer.
178-
#[derive(Clone, Eq, PartialEq, Hash)]
236+
#[derive(Clone, Eq)]
179237
#[repr(transparent)]
180238
pub struct CString {
181239
data: String,
@@ -229,6 +287,26 @@ impl AsRef<CStr> for CString {
229287
}
230288
}
231289

290+
impl From<CString> for String {
291+
fn from(value: CString) -> Self {
292+
value.into_string()
293+
}
294+
}
295+
296+
impl std::ops::Deref for CString {
297+
type Target = CStr;
298+
299+
fn deref(&self) -> &Self::Target {
300+
self.as_ref()
301+
}
302+
}
303+
304+
impl std::borrow::Borrow<CStr> for CString {
305+
fn borrow(&self) -> &CStr {
306+
self.as_ref()
307+
}
308+
}
309+
232310
impl std::fmt::Debug for CString {
233311
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234312
self.data.fmt(f)
@@ -241,9 +319,33 @@ impl std::fmt::Display for CString {
241319
}
242320
}
243321

244-
impl std::borrow::Borrow<CStr> for CString {
245-
fn borrow(&self) -> &CStr {
246-
self.as_ref()
322+
impl PartialEq for CString {
323+
fn eq(&self, other: &Self) -> bool {
324+
self.data == other.data
325+
}
326+
}
327+
328+
impl PartialEq<CStr> for CString {
329+
fn eq(&self, other: &CStr) -> bool {
330+
self.data.as_str() == other.as_str()
331+
}
332+
}
333+
334+
impl PartialEq<String> for CString {
335+
fn eq(&self, other: &String) -> bool {
336+
&self.data == other
337+
}
338+
}
339+
340+
impl PartialEq<str> for CString {
341+
fn eq(&self, other: &str) -> bool {
342+
self.data.as_str() == other
343+
}
344+
}
345+
346+
impl std::hash::Hash for CString {
347+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
348+
self.data.hash(state);
247349
}
248350
}
249351

src/raw/document.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl RawDocument {
161161
pub fn get(&self, key: impl AsRef<str>) -> RawResult<Option<RawBsonRef<'_>>> {
162162
for elem in RawIter::new(self) {
163163
let elem = elem?;
164-
if key.as_ref() == elem.key() {
164+
if key.as_ref() == elem.key().as_str() {
165165
return Ok(Some(elem.try_into()?));
166166
}
167167
}
@@ -524,7 +524,7 @@ impl RawDocument {
524524
for elem in self.iter_elements() {
525525
let elem = elem?;
526526
let value = deep_utf8_lossy(elem.value_utf8_lossy()?)?;
527-
out.insert(elem.key(), value);
527+
out.insert(elem.key().as_str(), value);
528528
}
529529
Ok(out)
530530
}
@@ -543,15 +543,21 @@ fn deep_utf8_lossy(src: RawBson) -> RawResult<Bson> {
543543
let mut tmp = doc! {};
544544
for elem in doc.iter_elements() {
545545
let elem = elem?;
546-
tmp.insert(elem.key(), deep_utf8_lossy(elem.value_utf8_lossy()?)?);
546+
tmp.insert(
547+
elem.key().as_str(),
548+
deep_utf8_lossy(elem.value_utf8_lossy()?)?,
549+
);
547550
}
548551
Ok(Bson::Document(tmp))
549552
}
550553
RawBson::JavaScriptCodeWithScope(RawJavaScriptCodeWithScope { code, scope }) => {
551554
let mut tmp = doc! {};
552555
for elem in scope.iter_elements() {
553556
let elem = elem?;
554-
tmp.insert(elem.key(), deep_utf8_lossy(elem.value_utf8_lossy()?)?);
557+
tmp.insert(
558+
elem.key().as_str(),
559+
deep_utf8_lossy(elem.value_utf8_lossy()?)?,
560+
);
555561
}
556562
Ok(Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope {
557563
code,
@@ -597,7 +603,7 @@ impl serde::Serialize for &RawDocument {
597603
let mut map = serializer.serialize_map(None)?;
598604
for kvp in self.0 {
599605
let (k, v) = kvp.map_err(serde::ser::Error::custom)?;
600-
map.serialize_entry(k, &v)?;
606+
map.serialize_entry(k.as_str(), &v)?;
601607
}
602608
map.end()
603609
} else {
@@ -643,7 +649,7 @@ impl TryFrom<&RawDocument> for crate::Document {
643649
fn try_from(rawdoc: &RawDocument) -> RawResult<Document> {
644650
rawdoc
645651
.into_iter()
646-
.map(|res| res.and_then(|(k, v)| Ok((k.to_owned(), v.try_into()?))))
652+
.map(|res| res.and_then(|(k, v)| Ok((k.as_str().to_owned(), v.try_into()?))))
647653
.collect()
648654
}
649655
}
@@ -666,7 +672,7 @@ impl TryFrom<&RawDocumentBuf> for Document {
666672

667673
impl<'a> IntoIterator for &'a RawDocument {
668674
type IntoIter = Iter<'a>;
669-
type Item = RawResult<(&'a str, RawBsonRef<'a>)>;
675+
type Item = RawResult<(&'a CStr, RawBsonRef<'a>)>;
670676

671677
fn into_iter(self) -> Iter<'a> {
672678
self.iter()

src/raw/document_buf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl TryFrom<Document> for RawDocumentBuf {
292292

293293
impl<'a> IntoIterator for &'a RawDocumentBuf {
294294
type IntoIter = Iter<'a>;
295-
type Item = Result<(&'a str, RawBsonRef<'a>)>;
295+
type Item = Result<(&'a CStr, RawBsonRef<'a>)>;
296296

297297
fn into_iter(self) -> Iter<'a> {
298298
self.iter()

src/raw/iter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ impl<'a> Iter<'a> {
4444
}
4545

4646
impl<'a> Iterator for Iter<'a> {
47-
type Item = Result<(&'a str, RawBsonRef<'a>)>;
47+
type Item = Result<(&'a CStr, RawBsonRef<'a>)>;
4848

49-
fn next(&mut self) -> Option<Result<(&'a str, RawBsonRef<'a>)>> {
49+
fn next(&mut self) -> Option<Result<(&'a CStr, RawBsonRef<'a>)>> {
5050
match self.inner.next() {
5151
Some(Ok(elem)) => match elem.value() {
5252
Err(e) => Some(Err(e)),
53-
Ok(value) => Some(Ok((elem.key.as_str(), value))),
53+
Ok(value) => Some(Ok((elem.key, value))),
5454
},
5555
Some(Err(e)) => Some(Err(e)),
5656
None => None,
@@ -161,8 +161,8 @@ impl<'a> RawElement<'a> {
161161
self.size
162162
}
163163

164-
pub fn key(&self) -> &'a str {
165-
self.key.as_str()
164+
pub fn key(&self) -> &'a CStr {
165+
self.key
166166
}
167167

168168
pub fn element_type(&self) -> ElementType {

src/raw/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ fn document_iteration() {
404404
assert_eq!(
405405
rawdoc
406406
.into_iter()
407-
.collect::<Result<Vec<(&str, _)>>>()
407+
.collect::<Result<Vec<(_, _)>>>()
408408
.expect("collecting iterated doc")
409409
.len(),
410410
17

0 commit comments

Comments
 (0)