Skip to content

Commit

Permalink
impl From<Cow> for boxed slices and strings
Browse files Browse the repository at this point in the history
These forward `Borrowed`/`Owned` values to existing `Box::from` impls.

- `From<Cow<'_, [T]>> for Box<[T]>`
- `From<Cow<'_, str>> for Box<str>`
- `From<Cow<'_, CStr>> for Box<CStr>`
- `From<Cow<'_, OsStr>> for Box<OsStr>`
- `From<Cow<'_, Path>> for Box<Path>`
  • Loading branch information
cuviper committed Apr 22, 2020
1 parent 82e90d6 commit b0fb57b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/liballoc/boxed.rs
Expand Up @@ -146,6 +146,7 @@ use core::ptr::{self, NonNull, Unique};
use core::task::{Context, Poll};

use crate::alloc::{self, AllocInit, AllocRef, Global};
use crate::borrow::Cow;
use crate::raw_vec::RawVec;
use crate::str::from_boxed_utf8_unchecked;
use crate::vec::Vec;
Expand Down Expand Up @@ -774,6 +775,17 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
}
}

#[stable(feature = "box_from_cow", since = "1.45.0")]
impl<T: Copy> From<Cow<'_, [T]>> for Box<[T]> {
#[inline]
fn from(cow: Cow<'_, [T]>) -> Box<[T]> {
match cow {
Cow::Borrowed(slice) => Box::from(slice),
Cow::Owned(slice) => Box::from(slice),
}
}
}

#[stable(feature = "box_from_slice", since = "1.17.0")]
impl From<&str> for Box<str> {
/// Converts a `&str` into a `Box<str>`
Expand All @@ -792,6 +804,17 @@ impl From<&str> for Box<str> {
}
}

#[stable(feature = "box_from_cow", since = "1.45.0")]
impl From<Cow<'_, str>> for Box<str> {
#[inline]
fn from(cow: Cow<'_, str>) -> Box<str> {
match cow {
Cow::Borrowed(s) => Box::from(s),
Cow::Owned(s) => Box::from(s),
}
}
}

#[stable(feature = "boxed_str_conv", since = "1.19.0")]
impl From<Box<str>> for Box<[u8]> {
/// Converts a `Box<str>>` into a `Box<[u8]>`
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/ffi/c_str.rs
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/ffi/os_str.rs
Expand Up @@ -849,6 +849,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
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/path.rs
Expand Up @@ -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`
Expand Down

0 comments on commit b0fb57b

Please sign in to comment.