1 use rustix
::ffi
::{CStr, CString}
;
4 #[cfg(feature = "itoa")]
5 use rustix
::path
::DecInt
;
7 use std
::ffi
::{OsStr, OsString}
;
8 use std
::path
::{Component, Components, Iter, Path, PathBuf}
;
13 use std
::borrow
::Borrow
;
15 let t
: &str = "hello";
16 assert_eq
!("hello", t
.as_str().unwrap());
17 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
18 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
19 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
21 let t
: String
= "hello".to_owned();
22 assert_eq
!("hello", Arg
::as_str(&t
).unwrap());
23 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
24 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
25 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
27 let t
: &OsStr
= OsStr
::new("hello");
28 assert_eq
!("hello", t
.as_str().unwrap());
29 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
30 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
31 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
33 let t
: OsString
= OsString
::from("hello".to_owned());
34 assert_eq
!("hello", t
.as_str().unwrap());
35 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
36 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
37 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
39 let t
: &Path
= Path
::new("hello");
40 assert_eq
!("hello", t
.as_str().unwrap());
41 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
42 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
43 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
45 let t
: PathBuf
= PathBuf
::from("hello".to_owned());
46 assert_eq
!("hello", t
.as_str().unwrap());
47 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
48 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
49 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
51 let t
: &CStr
= cstr
!("hello");
52 assert_eq
!("hello", t
.as_str().unwrap());
53 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
54 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
55 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
57 let t
: CString
= cstr
!("hello").to_owned();
58 assert_eq
!("hello", t
.as_str().unwrap());
59 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
62 Borrow
::borrow(&Arg
::as_cow_c_str(&t
).unwrap())
64 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
66 let t
: Components
= Path
::new("hello").components();
67 assert_eq
!("hello", t
.as_str().unwrap());
68 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
69 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
70 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
72 let t
: Component
= Path
::new("hello").components().next().unwrap();
73 assert_eq
!("hello", t
.as_str().unwrap());
74 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
75 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
76 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
78 let t
: Iter
= Path
::new("hello").iter();
79 assert_eq
!("hello", t
.as_str().unwrap());
80 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
81 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
82 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
84 let t
: Cow
<'_
, str> = Cow
::Borrowed("hello");
85 assert_eq
!("hello", t
.as_str().unwrap());
86 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
87 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
88 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
90 let t
: Cow
<'_
, str> = Cow
::Owned("hello".to_owned());
91 assert_eq
!("hello", t
.as_str().unwrap());
92 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
93 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
94 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
96 let t
: Cow
<'_
, OsStr
> = Cow
::Borrowed(OsStr
::new("hello"));
97 assert_eq
!("hello", t
.as_str().unwrap());
98 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
99 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
100 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
102 let t
: Cow
<'_
, OsStr
> = Cow
::Owned(OsString
::from("hello".to_owned()));
103 assert_eq
!("hello", t
.as_str().unwrap());
104 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
105 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
106 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
108 let t
: Cow
<'_
, CStr
> = Cow
::Borrowed(cstr
!("hello"));
109 assert_eq
!("hello", t
.as_str().unwrap());
110 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
111 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
112 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
114 let t
: Cow
<'_
, CStr
> = Cow
::Owned(cstr
!("hello").to_owned());
115 assert_eq
!("hello", t
.as_str().unwrap());
116 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
117 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
118 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
120 let t
: &[u8] = b
"hello";
121 assert_eq
!("hello", t
.as_str().unwrap());
122 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
123 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
124 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
126 let t
: Vec
<u8> = b
"hello".to_vec();
127 assert_eq
!("hello", t
.as_str().unwrap());
128 assert_eq
!("hello".to_owned(), Arg
::to_string_lossy(&t
));
129 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
130 assert_eq
!(cstr
!("hello"), Borrow
::borrow(&t
.into_c_str().unwrap()));
132 #[cfg(feature = "itoa")]
134 let t
: DecInt
= DecInt
::new(43110);
135 assert_eq
!("43110", t
.as_str());
136 assert_eq
!("43110".to_owned(), Arg
::to_string_lossy(&t
));
137 assert_eq
!(cstr
!("43110"), Borrow
::borrow(&t
.as_cow_c_str().unwrap()));
138 assert_eq
!(cstr
!("43110"), t
.as_c_str());
139 assert_eq
!(cstr
!("43110"), Borrow
::borrow(&t
.into_c_str().unwrap()));
145 use std
::borrow
::Borrow
;
147 let t
: &[u8] = b
"hello\xc0world";
148 assert_eq
!(t
.as_str().unwrap_err(), io
::Errno
::INVAL
);
149 assert_eq
!("hello\u{fffd}world".to_owned(), Arg
::to_string_lossy(&t
));
152 Borrow
::<CStr
>::borrow(&t
.as_cow_c_str().unwrap()).to_bytes_with_nul()
156 Borrow
::<CStr
>::borrow(&t
.into_c_str().unwrap()).to_bytes_with_nul()
161 fn test_embedded_nul() {
162 let t
: &[u8] = b
"hello\0world";
163 assert_eq
!("hello\0world", t
.as_str().unwrap());
164 assert_eq
!("hello\0world".to_owned(), Arg
::to_string_lossy(&t
));
165 assert_eq
!(t
.as_cow_c_str().unwrap_err(), io
::Errno
::INVAL
);
166 assert_eq
!(t
.into_c_str().unwrap_err(), io
::Errno
::INVAL
);