1 use std::borrow::{Cow, ToOwned};
2 use std::ffi::{CStr, OsStr};
3 use std::path::Path;
4 use std::rc::Rc;
5 use std::sync::Arc;
6 
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);
13     )+};
14     ($value:ident : & $ty:ty) => {
15         test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>);
16     }
17 }
18 
19 #[test]
test_from_cow_slice()20 fn test_from_cow_slice() {
21     let slice: &[i32] = &[1, 2, 3];
22     test_from_cow!(slice: &[i32]);
23 }
24 
25 #[test]
test_from_cow_str()26 fn test_from_cow_str() {
27     let string = "hello";
28     test_from_cow!(string: &str);
29 }
30 
31 #[test]
test_from_cow_c_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);
35 }
36 
37 #[test]
test_from_cow_os_str()38 fn test_from_cow_os_str() {
39     let string = OsStr::new("hello");
40     test_from_cow!(string: &OsStr);
41 }
42 
43 #[test]
test_from_cow_path()44 fn test_from_cow_path() {
45     let path = Path::new("hello");
46     test_from_cow!(path: &Path);
47 }
48 
49 #[test]
cow_const()50 fn cow_const() {
51     // test that the methods of `Cow` are usable in a const context
52 
53     const COW: Cow<'_, str> = Cow::Borrowed("moo");
54 
55     const IS_BORROWED: bool = COW.is_borrowed();
56     assert!(IS_BORROWED);
57 
58     const IS_OWNED: bool = COW.is_owned();
59     assert!(!IS_OWNED);
60 }
61