Skip to content

Add a generic From<&Borrow> impl for Cow #48191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/liballoc/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,17 @@ impl<'a, T: ?Sized + ToOwned> AsRef<T> for Cow<'a, T> {
}
}

#[stable(feature = "generic_cow_from", since = "1.24.0")]
impl<'a, B, T> From<&'a B> for Cow<'a, T>
where
B: ?Sized + Borrow<T>,
T: ?Sized + ToOwned,
{
fn from(b: &'a B) -> Self {
Cow::Borrowed(b.borrow())
}
}

#[stable(feature = "cow_add", since = "1.14.0")]
impl<'a> Add<&'a str> for Cow<'a, str> {
type Output = Cow<'a, str>;
Expand Down
8 changes: 0 additions & 8 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2128,14 +2128,6 @@ impl<'a> From<Cow<'a, str>> for String {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> From<&'a str> for Cow<'a, str> {
#[inline]
fn from(s: &'a str) -> Cow<'a, str> {
Cow::Borrowed(s)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> From<String> for Cow<'a, str> {
#[inline]
Expand Down
74 changes: 74 additions & 0 deletions src/liballoc/tests/borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::ffi::{CStr, CString, OsStr, OsString};

#[test]
fn test_cow_from() {
const MSG: &'static str = "Hello, World";
let s = MSG.to_string();
assert_eq!(Cow::<str>::from(&s), Cow::Borrowed(MSG));
assert_eq!(Cow::from(s.as_str()), Cow::Borrowed(MSG));
assert_eq!(
Cow::from(s),
|| -> Cow<str> { Cow::Owned(MSG.to_string()) }()
);

const VALUES: &[u8] = &[1u8, 2, 3, 4, 5, 6, 7, 8];
let v = VALUES.iter().map(|b| *b).collect::<Vec<u8>>();
assert_eq!(Cow::<[u8]>::from(&v), Cow::Borrowed(VALUES));
assert_eq!(Cow::from(v.as_slice()), Cow::Borrowed(VALUES));
assert_eq!(
Cow::from(v),
|| -> Cow<[u8]> { Cow::Owned(VALUES.iter().map(|b| *b).collect::<Vec<u8>>() )}()
);

let p = PathBuf::new();
assert_eq!(Cow::<Path>::from(&p), Cow::Borrowed(Path::new("")));
assert_eq!(Cow::from(p.as_path()), Cow::Borrowed(Path::new("")));
assert_eq!(
Cow::from(p),
|| -> Cow<Path> { Cow::Owned(PathBuf::new()) }()
);

let cstring = CString::new(MSG).unwrap();
let cstr = {
const MSG_NULL_TERMINATED: &'static str = "Hello, World\0";
CStr::from_bytes_with_nul(MSG_NULL_TERMINATED.as_bytes()).unwrap()
};
assert_eq!(Cow::<CStr>::from(&cstring), Cow::Borrowed(cstr));
assert_eq!(Cow::from(cstring.as_c_str()), Cow::Borrowed(cstr));

let s = OsString::from(MSG.to_string());
assert_eq!(Cow::<OsString>::from(&s), Cow::Borrowed(OsStr::new(MSG)));
assert_eq!(Cow::from(s.as_os_str()), Cow::Borrowed(OsStr::new(MSG)));
}

#[test]
fn test_generic_cow_from() {
struct VecWrapper {
_inner: Vec<i32>,
}

impl VecWrapper {
fn new<'a, T: Into<Cow<'a, [i32]>>>(val: T) -> Self {
VecWrapper {
_inner: val.into().into_owned(),
}
}
}

let ints = vec![0i32, 1, 2, 3, 4, 5];
let _vw0 = VecWrapper::new(ints.as_slice());
let _vw1 = VecWrapper::new(&ints);
let _vw2 = VecWrapper::new(ints);
}
1 change: 1 addition & 0 deletions src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use std::collections::hash_map::DefaultHasher;

mod binary_heap;
mod btree;
mod borrow;
mod cow_str;
mod fmt;
mod heap;
Expand Down
7 changes: 0 additions & 7 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2233,13 +2233,6 @@ impl<'a> From<&'a str> for Vec<u8> {
// Clone-on-write
////////////////////////////////////////////////////////////////////////////////

#[stable(feature = "cow_from_vec", since = "1.8.0")]
impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> {
fn from(s: &'a [T]) -> Cow<'a, [T]> {
Cow::Borrowed(s)
}
}

#[stable(feature = "cow_from_vec", since = "1.8.0")]
impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> {
fn from(v: Vec<T>) -> Cow<'a, [T]> {
Expand Down
8 changes: 0 additions & 8 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,14 +1445,6 @@ impl Default for PathBuf {
}
}

#[stable(feature = "cow_from_path", since = "1.6.0")]
impl<'a> From<&'a Path> for Cow<'a, Path> {
#[inline]
fn from(s: &'a Path) -> Cow<'a, Path> {
Cow::Borrowed(s)
}
}

#[stable(feature = "cow_from_path", since = "1.6.0")]
impl<'a> From<PathBuf> for Cow<'a, Path> {
#[inline]
Expand Down