File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
use crate :: { vec:: Vec , CapacityError } ;
4
4
use core:: {
5
+ borrow:: Borrow ,
5
6
error:: Error ,
6
7
ffi:: { c_char, CStr } ,
7
8
fmt,
@@ -271,6 +272,18 @@ impl<const N: usize> CString<N> {
271
272
}
272
273
}
273
274
275
+ impl < const N : usize > Borrow < CStr > for CString < N > {
276
+ fn borrow ( & self ) -> & CStr {
277
+ self . as_c_str ( )
278
+ }
279
+ }
280
+
281
+ impl < const N : usize > AsRef < CStr > for CString < N > {
282
+ fn as_ref ( & self ) -> & CStr {
283
+ self . as_c_str ( )
284
+ }
285
+ }
286
+
274
287
impl < const N : usize > Deref for CString < N > {
275
288
type Target = CStr ;
276
289
@@ -402,4 +415,18 @@ mod tests {
402
415
403
416
assert_eq ! ( string. deref( ) , c"ABC" ) ;
404
417
}
418
+
419
+ #[ test]
420
+ fn as_ref ( ) {
421
+ let mut string = CString :: < 4 > :: new ( ) ;
422
+ string. extend_from_bytes ( b"foo" ) . unwrap ( ) ;
423
+ assert_eq ! ( string. as_ref( ) , c"foo" ) ;
424
+ }
425
+
426
+ #[ test]
427
+ fn borrow ( ) {
428
+ let mut string = CString :: < 4 > :: new ( ) ;
429
+ string. extend_from_bytes ( b"foo" ) . unwrap ( ) ;
430
+ assert_eq ! ( Borrow :: <CStr >:: borrow( & string) , c"foo" ) ;
431
+ }
405
432
}
You can’t perform that action at this time.
0 commit comments