|
1 | | -use crate::bson::RawBson; |
| 1 | +#[cfg(feature = "bson-3")] |
| 2 | +pub(crate) type CStr = crate::bson::raw::CStr; |
| 3 | +#[cfg(feature = "bson-3")] |
| 4 | +pub(crate) type CString = crate::bson::raw::CString; |
| 5 | +#[cfg(feature = "bson-3")] |
| 6 | +pub(crate) use crate::bson::raw::cstr; |
2 | 7 |
|
3 | | -pub(crate) trait RawDocumentBufExt: Sized { |
4 | | - fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()>; |
| 8 | +#[cfg(not(feature = "bson-3"))] |
| 9 | +pub(crate) type CStr = str; |
| 10 | +#[cfg(not(feature = "bson-3"))] |
| 11 | +pub(crate) type CString = String; |
| 12 | +#[cfg(not(feature = "bson-3"))] |
| 13 | +macro_rules! cstr { |
| 14 | + ($text:literal) => { |
| 15 | + $text |
| 16 | + }; |
| 17 | +} |
| 18 | +#[cfg(not(feature = "bson-3"))] |
| 19 | +pub(crate) use cstr; |
5 | 20 |
|
6 | | - fn append_ref_err<'a>( |
| 21 | +pub(crate) fn cstr_to_str(cs: &CStr) -> &str { |
| 22 | + #[cfg(feature = "bson-3")] |
| 23 | + { |
| 24 | + cs.as_str() |
| 25 | + } |
| 26 | + #[cfg(not(feature = "bson-3"))] |
| 27 | + { |
| 28 | + cs |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +pub(crate) trait RawDocumentBufExt: Sized { |
| 33 | + fn append_ref_compat<'a>( |
7 | 34 | &mut self, |
8 | | - key: impl AsRef<str>, |
9 | | - value: impl Into<crate::bson::raw::RawBsonRef<'a>>, |
10 | | - ) -> RawResult<()>; |
| 35 | + key: impl AsRef<CStr>, |
| 36 | + value: impl Into<crate::bson::raw::RawBsonRef<'a>> + 'a, |
| 37 | + ); |
11 | 38 |
|
12 | 39 | #[cfg(not(feature = "bson-3"))] |
13 | 40 | fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self>; |
14 | 41 | } |
15 | 42 |
|
16 | 43 | #[cfg(feature = "bson-3")] |
17 | 44 | impl RawDocumentBufExt for crate::bson::RawDocumentBuf { |
18 | | - fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()> { |
19 | | - self.append(key, value.into()) |
20 | | - } |
21 | | - |
22 | | - fn append_ref_err<'a>( |
| 45 | + fn append_ref_compat<'a>( |
23 | 46 | &mut self, |
24 | | - key: impl AsRef<str>, |
25 | | - value: impl Into<crate::bson::raw::RawBsonRef<'a>>, |
26 | | - ) -> RawResult<()> { |
27 | | - self.append(key, value) |
| 47 | + key: impl AsRef<CStr>, |
| 48 | + value: impl Into<crate::bson::raw::RawBsonRef<'a>> + 'a, |
| 49 | + ) { |
| 50 | + self.append(key, value); |
28 | 51 | } |
29 | 52 | } |
30 | 53 |
|
31 | 54 | #[cfg(not(feature = "bson-3"))] |
32 | 55 | impl RawDocumentBufExt for crate::bson::RawDocumentBuf { |
33 | | - fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()> { |
34 | | - self.append(key, value); |
35 | | - Ok(()) |
36 | | - } |
37 | | - |
38 | | - fn append_ref_err<'a>( |
| 56 | + fn append_ref_compat<'a>( |
39 | 57 | &mut self, |
40 | | - key: impl AsRef<str>, |
| 58 | + key: impl AsRef<CStr>, |
41 | 59 | value: impl Into<crate::bson::raw::RawBsonRef<'a>>, |
42 | | - ) -> RawResult<()> { |
43 | | - self.append_ref(key, value); |
44 | | - Ok(()) |
| 60 | + ) { |
| 61 | + self.append_ref(key, value) |
45 | 62 | } |
46 | 63 |
|
47 | 64 | fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self> { |
48 | 65 | Self::from_bytes(data) |
49 | 66 | } |
50 | 67 | } |
51 | 68 |
|
52 | | -pub(crate) trait RawArrayBufExt: Sized { |
53 | | - #[allow(dead_code)] |
54 | | - fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self>; |
55 | | - |
56 | | - fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()>; |
57 | | -} |
58 | | - |
59 | | -#[cfg(feature = "bson-3")] |
60 | | -impl RawArrayBufExt for crate::bson::RawArrayBuf { |
61 | | - fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self> { |
62 | | - Self::from_iter(iter.into_iter().map(|v| v.into())) |
63 | | - } |
64 | | - |
65 | | - fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()> { |
66 | | - self.push(value.into()) |
67 | | - } |
68 | | -} |
69 | | - |
70 | | -#[cfg(not(feature = "bson-3"))] |
71 | | -impl RawArrayBufExt for crate::bson::RawArrayBuf { |
72 | | - fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self> { |
73 | | - Ok(Self::from_iter(iter)) |
74 | | - } |
75 | | - |
76 | | - fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()> { |
77 | | - self.push(value); |
78 | | - Ok(()) |
79 | | - } |
80 | | -} |
81 | | - |
82 | 69 | #[cfg(not(feature = "bson-3"))] |
83 | 70 | pub(crate) trait RawDocumentExt { |
84 | 71 | fn decode_from_bytes<D: AsRef<[u8]> + ?Sized>(data: &D) -> RawResult<&Self>; |
|
0 commit comments