|
10 | 10 |
|
11 | 11 | use std::borrow::Cow;
|
12 | 12 | use std::path::{Path, PathBuf};
|
13 |
| -use std::ffi::{CStr, CString, OSStr, OSString}; |
| 13 | +use std::ffi::{CStr, CString, OsStr, OsString}; |
14 | 14 |
|
15 | 15 | #[test]
|
| 16 | +#[ignore] |
16 | 17 | fn test_cow_from() {
|
17 | 18 | const MSG: &'static str = "Hello, World";
|
18 | 19 | let s = MSG.to_string();
|
19 |
| - assert_eq!(Cow::from(&s), Cow::Borrowed(MSG)); |
| 20 | + assert_eq!(Cow::<str>::from(&s), Cow::Borrowed(MSG)); |
20 | 21 | assert_eq!(Cow::from(s.as_str()), Cow::Borrowed(MSG));
|
21 |
| - assert_eq!(Cow::from(s), Cow::Owned(MSG.to_string())); |
| 22 | + assert_eq!( |
| 23 | + Cow::from(s), |
| 24 | + || -> Cow<str> { Cow::Owned(MSG.to_string()) }() |
| 25 | + ); |
22 | 26 |
|
23 |
| - const VALUES: &'static [u8] = [1u8, 2, 3, 4, 5, 6, 7, 8]; |
24 |
| - let v = VALUES.iter().collect::<Vec<_>>(); |
25 |
| - assert_eq!(Cow::from(&v), Cow::Borrowed(VALUES)); |
| 27 | + const VALUES: &[u8] = &[1u8, 2, 3, 4, 5, 6, 7, 8]; |
| 28 | + let v = VALUES.iter().map(|b| *b).collect::<Vec<u8>>(); |
| 29 | + assert_eq!(Cow::<[u8]>::from(&v), Cow::Borrowed(VALUES)); |
26 | 30 | assert_eq!(Cow::from(v.as_slice()), Cow::Borrowed(VALUES));
|
27 |
| - assert_eq!(Cow::from(v), Cow::Owned(VALUES.iter().collect::<Vec<_>>())); |
| 31 | + assert_eq!( |
| 32 | + Cow::from(v), |
| 33 | + || -> Cow<[u8]> { Cow::Owned(VALUES.iter().map(|b| *b).collect::<Vec<u8>>() )}() |
| 34 | + ); |
28 | 35 |
|
29 | 36 | let p = PathBuf::new();
|
30 |
| - assert_eq!(Cow::from(&p), Cow::Borrowed(Path::new(""))); |
| 37 | + assert_eq!(Cow::<Path>::from(&p), Cow::Borrowed(Path::new(""))); |
31 | 38 | assert_eq!(Cow::from(p.as_path()), Cow::Borrowed(Path::new("")));
|
| 39 | + assert_eq!( |
| 40 | + Cow::from(p), |
| 41 | + || -> Cow<Path> { Cow::Owned(PathBuf::new()) }() |
| 42 | + ); |
32 | 43 |
|
33 |
| - let cstring = CString::new(MSG); |
| 44 | + let cstring = CString::new(MSG).unwrap(); |
34 | 45 | let cstr = {
|
35 | 46 | const MSG_NULL_TERMINATED: &'static str = "Hello, World\0";
|
36 |
| - CStr::from_bytes_with_nul(MSG_NULL_TERMINATED).unwrap() |
| 47 | + CStr::from_bytes_with_nul(MSG_NULL_TERMINATED.as_bytes()).unwrap() |
37 | 48 | };
|
38 |
| - assert_eq(Cow::from(&cstring), Cow::Borrowed(cstr)); |
39 |
| - assert_eq(Cow::from(cstring.as_c_str()), Cow::Borrowed(cstr)); |
| 49 | + assert_eq!(Cow::<CStr>::from(&cstring), Cow::Borrowed(cstr)); |
| 50 | + assert_eq!(Cow::from(cstring.as_c_str()), Cow::Borrowed(cstr)); |
40 | 51 |
|
41 |
| - let s = OSString::from(MSG.into()); |
42 |
| - assert_eq!(Cow::from(&s), Cow::Borrowed(OSStr::new(msg))); |
43 |
| - assert_eq!(Cow::from(s.as_os_str()), Cow::Borrowed(OSStr::new(msg))); |
| 52 | + let s = OsString::from(MSG.to_string()); |
| 53 | + assert_eq!(Cow::<OsString>::from(&s), Cow::Borrowed(OsStr::new(MSG))); |
| 54 | + assert_eq!(Cow::from(s.as_os_str()), Cow::Borrowed(OsStr::new(MSG))); |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn test_generic_cow_from() { |
| 59 | + struct VecWrapper { |
| 60 | + _inner: Vec<i32>, |
| 61 | + } |
| 62 | + |
| 63 | + impl VecWrapper { |
| 64 | + fn new<'a, T: Into<Cow<'a, [i32]>>>(val: T) -> Self { |
| 65 | + VecWrapper { |
| 66 | + _inner: val.into().into_owned(), |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + let ints = vec![0i32, 1, 2, 3, 4, 5]; |
| 72 | + let _vw0 = VecWrapper::new(ints.as_slice()); |
| 73 | + let _vw1 = VecWrapper::new(&ints); |
| 74 | + let _vw2 = VecWrapper::new(ints); |
44 | 75 | }
|
0 commit comments