@@ -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) ]
3232pub 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+
91143impl 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" ) ]
112170impl 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) ]
180238pub 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+
232310impl 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
0 commit comments