Skip to content

Commit b0fb57b

Browse files
committed
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>`
1 parent 82e90d6 commit b0fb57b

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/liballoc/boxed.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ use core::ptr::{self, NonNull, Unique};
146146
use core::task::{Context, Poll};
147147

148148
use crate::alloc::{self, AllocInit, AllocRef, Global};
149+
use crate::borrow::Cow;
149150
use crate::raw_vec::RawVec;
150151
use crate::str::from_boxed_utf8_unchecked;
151152
use crate::vec::Vec;
@@ -774,6 +775,17 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
774775
}
775776
}
776777

778+
#[stable(feature = "box_from_cow", since = "1.45.0")]
779+
impl<T: Copy> From<Cow<'_, [T]>> for Box<[T]> {
780+
#[inline]
781+
fn from(cow: Cow<'_, [T]>) -> Box<[T]> {
782+
match cow {
783+
Cow::Borrowed(slice) => Box::from(slice),
784+
Cow::Owned(slice) => Box::from(slice),
785+
}
786+
}
787+
}
788+
777789
#[stable(feature = "box_from_slice", since = "1.17.0")]
778790
impl From<&str> for Box<str> {
779791
/// Converts a `&str` into a `Box<str>`
@@ -792,6 +804,17 @@ impl From<&str> for Box<str> {
792804
}
793805
}
794806

807+
#[stable(feature = "box_from_cow", since = "1.45.0")]
808+
impl From<Cow<'_, str>> for Box<str> {
809+
#[inline]
810+
fn from(cow: Cow<'_, str>) -> Box<str> {
811+
match cow {
812+
Cow::Borrowed(s) => Box::from(s),
813+
Cow::Owned(s) => Box::from(s),
814+
}
815+
}
816+
}
817+
795818
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
796819
impl From<Box<str>> for Box<[u8]> {
797820
/// Converts a `Box<str>>` into a `Box<[u8]>`

src/libstd/ffi/c_str.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,17 @@ impl From<&CStr> for Box<CStr> {
730730
}
731731
}
732732

733+
#[stable(feature = "box_from_cow", since = "1.45.0")]
734+
impl From<Cow<'_, CStr>> for Box<CStr> {
735+
#[inline]
736+
fn from(cow: Cow<'_, CStr>) -> Box<CStr> {
737+
match cow {
738+
Cow::Borrowed(s) => Box::from(s),
739+
Cow::Owned(s) => Box::from(s),
740+
}
741+
}
742+
}
743+
733744
#[stable(feature = "c_string_from_box", since = "1.18.0")]
734745
impl From<Box<CStr>> for CString {
735746
/// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.

src/libstd/ffi/os_str.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,17 @@ impl From<&OsStr> for Box<OsStr> {
849849
}
850850
}
851851

852+
#[stable(feature = "box_from_cow", since = "1.45.0")]
853+
impl From<Cow<'_, OsStr>> for Box<OsStr> {
854+
#[inline]
855+
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
856+
match cow {
857+
Cow::Borrowed(s) => Box::from(s),
858+
Cow::Owned(s) => Box::from(s),
859+
}
860+
}
861+
}
862+
852863
#[stable(feature = "os_string_from_box", since = "1.18.0")]
853864
impl From<Box<OsStr>> for OsString {
854865
/// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or

src/libstd/path.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,17 @@ impl From<&Path> for Box<Path> {
14331433
}
14341434
}
14351435

1436+
#[stable(feature = "box_from_cow", since = "1.45.0")]
1437+
impl From<Cow<'_, Path>> for Box<Path> {
1438+
#[inline]
1439+
fn from(cow: Cow<'_, Path>) -> Box<Path> {
1440+
match cow {
1441+
Cow::Borrowed(path) => Box::from(path),
1442+
Cow::Owned(path) => Box::from(path),
1443+
}
1444+
}
1445+
}
1446+
14361447
#[stable(feature = "path_buf_from_box", since = "1.18.0")]
14371448
impl From<Box<Path>> for PathBuf {
14381449
/// Converts a `Box<Path>` into a `PathBuf`

0 commit comments

Comments
 (0)