1 use std::borrow::{Cow, ToOwned};
2 use std::ffi::{CStr, OsStr};
7 macro_rules! test_from_cow {
8 ($value:ident => $($ty:ty),+) => {$(
9 let borrowed = <$ty>::from(Cow::Borrowed($value));
10 let owned = <$ty>::from(Cow::Owned($value.to_owned()));
11 assert_eq!($value, &*borrowed);
12 assert_eq!($value, &*owned);
14 ($value:ident : & $ty:ty) => {
15 test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>);
20 fn test_from_cow_slice() {
21 let slice: &[i32] = &[1, 2, 3];
22 test_from_cow!(slice: &[i32]);
26 fn test_from_cow_str() {
28 test_from_cow!(string: &str);
32 fn test_from_cow_c_str() {
33 let string = CStr::from_bytes_with_nul(b"hello\0").unwrap();
34 test_from_cow!(string: &CStr);
38 fn test_from_cow_os_str() {
39 let string = OsStr::new("hello");
40 test_from_cow!(string: &OsStr);
44 fn test_from_cow_path() {
45 let path = Path::new("hello");
46 test_from_cow!(path: &Path);
51 // test that the methods of `Cow` are usable in a const context
53 const COW: Cow<'_, str> = Cow::Borrowed("moo");
55 const IS_BORROWED: bool = COW.is_borrowed();
58 const IS_OWNED: bool = COW.is_owned();