diff options
| author | bors <bors@rust-lang.org> | 2020-05-19 08:08:48 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-05-19 08:08:48 +0000 |
| commit | 914adf04af1c1a984707f778da3d04590c03d144 (patch) | |
| tree | cf43672b28ea604bd6b54ca7712e0001b17c5e2c /src/libstd | |
| parent | 5943351d0eb878c1cb5af42b9e85e101d8c58ed7 (diff) | |
| parent | 22efd959109b9a231edbc81a8bc818eaa5763e78 (diff) | |
| download | rust-914adf04af1c1a984707f778da3d04590c03d144.tar.gz rust-914adf04af1c1a984707f778da3d04590c03d144.zip | |
Auto merge of #71447 - cuviper:unsized_cow, r=dtolnay
impl From<Cow> for Box, Rc, and Arc These forward `Borrowed`/`Owned` values to existing `From` impls. - `Box<T>` is a fundamental type, so it would be a breaking change to add a blanket impl. Therefore, `From<Cow>` is only implemented for `[T]`, `str`, `CStr`, `OsStr`, and `Path`. - For `Rc<T>` and `Arc<T>`, `From<Cow>` is implemented for everything that implements `From` the borrowed and owned types separately.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 11 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 11 | ||||
| -rw-r--r-- | src/libstd/path.rs | 11 |
3 files changed, 33 insertions, 0 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 0a4802fb2c8..4bac9a4917d 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -730,6 +730,17 @@ impl From<&CStr> for Box<CStr> { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From<Cow<'_, CStr>> for Box<CStr> { + #[inline] + fn from(cow: Cow<'_, CStr>) -> Box<CStr> { + match cow { + Cow::Borrowed(s) => Box::from(s), + Cow::Owned(s) => Box::from(s), + } + } +} + #[stable(feature = "c_string_from_box", since = "1.18.0")] impl From<Box<CStr>> for CString { /// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating. diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 7a05aaf71f2..d1eaf3c583f 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -850,6 +850,17 @@ impl From<&OsStr> for Box<OsStr> { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From<Cow<'_, OsStr>> for Box<OsStr> { + #[inline] + fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> { + match cow { + Cow::Borrowed(s) => Box::from(s), + Cow::Owned(s) => Box::from(s), + } + } +} + #[stable(feature = "os_string_from_box", since = "1.18.0")] impl From<Box<OsStr>> for OsString { /// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 8516e80f3b8..0fe5451bb95 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1433,6 +1433,17 @@ impl From<&Path> for Box<Path> { } } +#[stable(feature = "box_from_cow", since = "1.45.0")] +impl From<Cow<'_, Path>> for Box<Path> { + #[inline] + fn from(cow: Cow<'_, Path>) -> Box<Path> { + match cow { + Cow::Borrowed(path) => Box::from(path), + Cow::Owned(path) => Box::from(path), + } + } +} + #[stable(feature = "path_buf_from_box", since = "1.18.0")] impl From<Box<Path>> for PathBuf { /// Converts a `Box<Path>` into a `PathBuf` |
