1
Fork 0

impl From<Cow> for boxed slices and strings

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>`
This commit is contained in:
Josh Stone 2020-04-22 13:03:05 -07:00
parent 82e90d6426
commit b0fb57bd8d
4 changed files with 56 additions and 0 deletions

View file

@ -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;
@ -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>`
@ -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]>`

View file

@ -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.

View file

@ -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

View file

@ -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`