Skip to content

Commit bdb805b

Browse files
committed
Add a generic impl of From<Borrow<T>> for Cow<T>
1 parent 3bcda48 commit bdb805b

File tree

6 files changed

+56
-23
lines changed

6 files changed

+56
-23
lines changed

src/liballoc/borrow.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ impl<'a, T: ?Sized + ToOwned> AsRef<T> for Cow<'a, T> {
361361
}
362362
}
363363

364+
#[unstable(feature = "generic_cow_from", reason = "recently added", issue = "0000")]
365+
impl<'a, B, T> From<&'a B> for Cow<'a, T>
366+
where
367+
B: ?Sized + Borrow<T>,
368+
T: ?Sized + ToOwned,
369+
{
370+
fn from(b: &'a B) -> Self {
371+
Cow::Borrowed(b.borrow())
372+
}
373+
}
374+
364375
#[stable(feature = "cow_add", since = "1.14.0")]
365376
impl<'a> Add<&'a str> for Cow<'a, str> {
366377
type Output = Cow<'a, str>;

src/liballoc/string.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,14 +2128,6 @@ impl<'a> From<Cow<'a, str>> for String {
21282128
}
21292129
}
21302130

2131-
#[stable(feature = "rust1", since = "1.0.0")]
2132-
impl<'a> From<&'a str> for Cow<'a, str> {
2133-
#[inline]
2134-
fn from(s: &'a str) -> Cow<'a, str> {
2135-
Cow::Borrowed(s)
2136-
}
2137-
}
2138-
21392131
#[stable(feature = "rust1", since = "1.0.0")]
21402132
impl<'a> From<String> for Cow<'a, str> {
21412133
#[inline]

src/liballoc/tests/borrow.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::borrow::Cow;
12+
use std::path::PathBuf;
13+
use std::ffi::{CStr, CString, OSStr, OSString};
14+
15+
#[test]
16+
fn test_cow_from() {
17+
const MSG: &'static str = "Hello, World";
18+
let s = MSG.to_string();
19+
assert_eq!(Cow::from(&s), Cow::Borrowed(MSG));
20+
assert_eq!(Cow::from(s.as_str()), Cow::Borrowed(MSG));
21+
assert_eq!(Cow::from(s), Cow::Owned(MSG.to_string()));
22+
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));
26+
assert_eq!(Cow::from(v.as_slice()), Cow::Borrowed(VALUES));
27+
assert_eq!(Cow::from(v), Cow::Owned(VALUES.iter().collect::<Vec<_>>()));
28+
29+
let p = PathBuf::new();
30+
assert_eq!(Cow::from(&p), Cow::Borrowed(p.as_path()));
31+
assert_eq!(Cow::from(v.as_path()), Cow::Borrowed(p.as_path()));
32+
33+
let cstring = CString::new(MSG);
34+
let cstr = {
35+
const MSG_NULL_TERMINATED: &'static str = "Hello, World\0";
36+
CStr::from_bytes_with_nul(MSG_NULL_TERMINATED).unwrap()
37+
};
38+
assert_eq(Cow::from(&cstring), Cow::Borrowed(cstr));
39+
assert_eq(Cow::from(cstring.as_c_str()), Cow::Borrowed(cstr));
40+
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)));
44+
}

src/liballoc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use std::collections::hash_map::DefaultHasher;
4040

4141
mod binary_heap;
4242
mod btree;
43+
mod borrow;
4344
mod cow_str;
4445
mod fmt;
4546
mod heap;

src/liballoc/vec.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,13 +2233,6 @@ impl<'a> From<&'a str> for Vec<u8> {
22332233
// Clone-on-write
22342234
////////////////////////////////////////////////////////////////////////////////
22352235

2236-
#[stable(feature = "cow_from_vec", since = "1.8.0")]
2237-
impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> {
2238-
fn from(s: &'a [T]) -> Cow<'a, [T]> {
2239-
Cow::Borrowed(s)
2240-
}
2241-
}
2242-
22432236
#[stable(feature = "cow_from_vec", since = "1.8.0")]
22442237
impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> {
22452238
fn from(v: Vec<T>) -> Cow<'a, [T]> {

src/libstd/path.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,14 +1445,6 @@ impl Default for PathBuf {
14451445
}
14461446
}
14471447

1448-
#[stable(feature = "cow_from_path", since = "1.6.0")]
1449-
impl<'a> From<&'a Path> for Cow<'a, Path> {
1450-
#[inline]
1451-
fn from(s: &'a Path) -> Cow<'a, Path> {
1452-
Cow::Borrowed(s)
1453-
}
1454-
}
1455-
14561448
#[stable(feature = "cow_from_path", since = "1.6.0")]
14571449
impl<'a> From<PathBuf> for Cow<'a, Path> {
14581450
#[inline]

0 commit comments

Comments
 (0)