|
| 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] |
| 20 | +fn test_from_cow_slice() { |
| 21 | + let slice: &[i32] = &[1, 2, 3]; |
| 22 | + test_from_cow!(slice: &[i32]); |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn test_from_cow_str() { |
| 27 | + let string = "hello"; |
| 28 | + test_from_cow!(string: &str); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 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] |
| 38 | +fn test_from_cow_os_str() { |
| 39 | + let string = OsStr::new("hello"); |
| 40 | + test_from_cow!(string: &OsStr); |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_from_cow_path() { |
| 45 | + let path = Path::new("hello"); |
| 46 | + test_from_cow!(path: &Path); |
| 47 | +} |
0 commit comments