Skip to content

Commit 9789c54

Browse files
authored
Rollup merge of rust-lang#129329 - eduardosm:rc-from-mut-slice, r=dtolnay
Implement `From<&mut {slice}>` for `Box/Rc/Arc<{slice}>` ACP: rust-lang/libs-team#424 New API: ```rust impl<T: Clone> From<&mut [T]> for Box<[T]> impl From<&mut str> for Box<str> impl From<&mut CStr> for Box<CStr> impl From<&mut OsStr> for Box<OsStr> impl From<&mut Path> for Box<Path> impl<T: Clone> From<&mut [T]> for Rc<[T]> impl From<&mut str> for Rc<str> impl From<&mut CStr> for Rc<CStr> impl From<&mut OsStr> for Rc<OsStr> impl From<&mut Path> for Rc<Path> impl<T: Clone> From<&mut [T]> for Arc<[T]> impl From<&mut str> for Arc<str> impl From<&mut CStr> for Arc<CStr> impl From<&mut OsStr> for Arc<OsStr> impl From<&mut Path> for Arc<Path> ``` Since they are trait implementations, I think these are insta-stable. As mentioned in rust-lang/libs-team#424 (comment), a crater run might be needed.
2 parents 4c6593f + 9d10ab7 commit 9789c54

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

alloc/src/boxed/convert.rs

+45
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ impl<T: Clone> From<&[T]> for Box<[T]> {
109109
}
110110
}
111111

112+
#[cfg(not(no_global_oom_handling))]
113+
#[stable(feature = "box_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
114+
impl<T: Clone> From<&mut [T]> for Box<[T]> {
115+
/// Converts a `&mut [T]` into a `Box<[T]>`
116+
///
117+
/// This conversion allocates on the heap
118+
/// and performs a copy of `slice` and its contents.
119+
///
120+
/// # Examples
121+
/// ```rust
122+
/// // create a &mut [u8] which will be used to create a Box<[u8]>
123+
/// let mut array = [104, 101, 108, 108, 111];
124+
/// let slice: &mut [u8] = &mut array;
125+
/// let boxed_slice: Box<[u8]> = Box::from(slice);
126+
///
127+
/// println!("{boxed_slice:?}");
128+
/// ```
129+
#[inline]
130+
fn from(slice: &mut [T]) -> Box<[T]> {
131+
Self::from(&*slice)
132+
}
133+
}
134+
112135
#[cfg(not(no_global_oom_handling))]
113136
#[stable(feature = "box_from_cow", since = "1.45.0")]
114137
impl<T: Clone> From<Cow<'_, [T]>> for Box<[T]> {
@@ -147,6 +170,28 @@ impl From<&str> for Box<str> {
147170
}
148171
}
149172

173+
#[cfg(not(no_global_oom_handling))]
174+
#[stable(feature = "box_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
175+
impl From<&mut str> for Box<str> {
176+
/// Converts a `&mut str` into a `Box<str>`
177+
///
178+
/// This conversion allocates on the heap
179+
/// and performs a copy of `s`.
180+
///
181+
/// # Examples
182+
///
183+
/// ```rust
184+
/// let mut original = String::from("hello");
185+
/// let original: &mut str = &mut original;
186+
/// let boxed: Box<str> = Box::from(original);
187+
/// println!("{boxed}");
188+
/// ```
189+
#[inline]
190+
fn from(s: &mut str) -> Box<str> {
191+
Self::from(&*s)
192+
}
193+
}
194+
150195
#[cfg(not(no_global_oom_handling))]
151196
#[stable(feature = "box_from_cow", since = "1.45.0")]
152197
impl From<Cow<'_, str>> for Box<str> {

alloc/src/ffi/c_str.rs

+31
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,16 @@ impl From<&CStr> for Box<CStr> {
772772
}
773773
}
774774

775+
#[cfg(not(test))]
776+
#[stable(feature = "box_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
777+
impl From<&mut CStr> for Box<CStr> {
778+
/// Converts a `&mut CStr` into a `Box<CStr>`,
779+
/// by copying the contents into a newly allocated [`Box`].
780+
fn from(s: &mut CStr) -> Box<CStr> {
781+
Self::from(&*s)
782+
}
783+
}
784+
775785
#[stable(feature = "box_from_cow", since = "1.45.0")]
776786
impl From<Cow<'_, CStr>> for Box<CStr> {
777787
/// Converts a `Cow<'a, CStr>` into a `Box<CStr>`,
@@ -910,6 +920,17 @@ impl From<&CStr> for Arc<CStr> {
910920
}
911921
}
912922

923+
#[cfg(target_has_atomic = "ptr")]
924+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
925+
impl From<&mut CStr> for Arc<CStr> {
926+
/// Converts a `&mut CStr` into a `Arc<CStr>`,
927+
/// by copying the contents into a newly allocated [`Arc`].
928+
#[inline]
929+
fn from(s: &mut CStr) -> Arc<CStr> {
930+
Arc::from(&*s)
931+
}
932+
}
933+
913934
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
914935
impl From<CString> for Rc<CStr> {
915936
/// Converts a [`CString`] into an <code>[Rc]<[CStr]></code> by moving the [`CString`]
@@ -932,6 +953,16 @@ impl From<&CStr> for Rc<CStr> {
932953
}
933954
}
934955

956+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
957+
impl From<&mut CStr> for Rc<CStr> {
958+
/// Converts a `&mut CStr` into a `Rc<CStr>`,
959+
/// by copying the contents into a newly allocated [`Rc`].
960+
#[inline]
961+
fn from(s: &mut CStr) -> Rc<CStr> {
962+
Rc::from(&*s)
963+
}
964+
}
965+
935966
#[cfg(not(no_global_oom_handling))]
936967
#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
937968
impl Default for Rc<CStr> {

alloc/src/rc.rs

+40
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,26 @@ impl<T: Clone> From<&[T]> for Rc<[T]> {
26572657
}
26582658
}
26592659

2660+
#[cfg(not(no_global_oom_handling))]
2661+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
2662+
impl<T: Clone> From<&mut [T]> for Rc<[T]> {
2663+
/// Allocates a reference-counted slice and fills it by cloning `v`'s items.
2664+
///
2665+
/// # Example
2666+
///
2667+
/// ```
2668+
/// # use std::rc::Rc;
2669+
/// let mut original = [1, 2, 3];
2670+
/// let original: &mut [i32] = &mut original;
2671+
/// let shared: Rc<[i32]> = Rc::from(original);
2672+
/// assert_eq!(&[1, 2, 3], &shared[..]);
2673+
/// ```
2674+
#[inline]
2675+
fn from(v: &mut [T]) -> Rc<[T]> {
2676+
Rc::from(&*v)
2677+
}
2678+
}
2679+
26602680
#[cfg(not(no_global_oom_handling))]
26612681
#[stable(feature = "shared_from_slice", since = "1.21.0")]
26622682
impl From<&str> for Rc<str> {
@@ -2676,6 +2696,26 @@ impl From<&str> for Rc<str> {
26762696
}
26772697
}
26782698

2699+
#[cfg(not(no_global_oom_handling))]
2700+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
2701+
impl From<&mut str> for Rc<str> {
2702+
/// Allocates a reference-counted string slice and copies `v` into it.
2703+
///
2704+
/// # Example
2705+
///
2706+
/// ```
2707+
/// # use std::rc::Rc;
2708+
/// let mut original = String::from("statue");
2709+
/// let original: &mut str = &mut original;
2710+
/// let shared: Rc<str> = Rc::from(original);
2711+
/// assert_eq!("statue", &shared[..]);
2712+
/// ```
2713+
#[inline]
2714+
fn from(v: &mut str) -> Rc<str> {
2715+
Rc::from(&*v)
2716+
}
2717+
}
2718+
26792719
#[cfg(not(no_global_oom_handling))]
26802720
#[stable(feature = "shared_from_slice", since = "1.21.0")]
26812721
impl From<String> for Rc<str> {

alloc/src/sync.rs

+40
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,26 @@ impl<T: Clone> From<&[T]> for Arc<[T]> {
36143614
}
36153615
}
36163616

3617+
#[cfg(not(no_global_oom_handling))]
3618+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
3619+
impl<T: Clone> From<&mut [T]> for Arc<[T]> {
3620+
/// Allocates a reference-counted slice and fills it by cloning `v`'s items.
3621+
///
3622+
/// # Example
3623+
///
3624+
/// ```
3625+
/// # use std::sync::Arc;
3626+
/// let mut original = [1, 2, 3];
3627+
/// let original: &mut [i32] = &mut original;
3628+
/// let shared: Arc<[i32]> = Arc::from(original);
3629+
/// assert_eq!(&[1, 2, 3], &shared[..]);
3630+
/// ```
3631+
#[inline]
3632+
fn from(v: &mut [T]) -> Arc<[T]> {
3633+
Arc::from(&*v)
3634+
}
3635+
}
3636+
36173637
#[cfg(not(no_global_oom_handling))]
36183638
#[stable(feature = "shared_from_slice", since = "1.21.0")]
36193639
impl From<&str> for Arc<str> {
@@ -3633,6 +3653,26 @@ impl From<&str> for Arc<str> {
36333653
}
36343654
}
36353655

3656+
#[cfg(not(no_global_oom_handling))]
3657+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
3658+
impl From<&mut str> for Arc<str> {
3659+
/// Allocates a reference-counted `str` and copies `v` into it.
3660+
///
3661+
/// # Example
3662+
///
3663+
/// ```
3664+
/// # use std::sync::Arc;
3665+
/// let mut original = String::from("eggplant");
3666+
/// let original: &mut str = &mut original;
3667+
/// let shared: Arc<str> = Arc::from(original);
3668+
/// assert_eq!("eggplant", &shared[..]);
3669+
/// ```
3670+
#[inline]
3671+
fn from(v: &mut str) -> Arc<str> {
3672+
Arc::from(&*v)
3673+
}
3674+
}
3675+
36363676
#[cfg(not(no_global_oom_handling))]
36373677
#[stable(feature = "shared_from_slice", since = "1.21.0")]
36383678
impl From<String> for Arc<str> {

std/src/ffi/os_str.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,15 @@ impl From<&OsStr> for Box<OsStr> {
12251225
}
12261226
}
12271227

1228+
#[stable(feature = "box_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1229+
impl From<&mut OsStr> for Box<OsStr> {
1230+
/// Copies the string into a newly allocated <code>[Box]&lt;[OsStr]&gt;</code>.
1231+
#[inline]
1232+
fn from(s: &mut OsStr) -> Box<OsStr> {
1233+
Self::from(&*s)
1234+
}
1235+
}
1236+
12281237
#[stable(feature = "box_from_cow", since = "1.45.0")]
12291238
impl From<Cow<'_, OsStr>> for Box<OsStr> {
12301239
/// Converts a `Cow<'a, OsStr>` into a <code>[Box]&lt;[OsStr]&gt;</code>,
@@ -1296,6 +1305,15 @@ impl From<&OsStr> for Arc<OsStr> {
12961305
}
12971306
}
12981307

1308+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1309+
impl From<&mut OsStr> for Arc<OsStr> {
1310+
/// Copies the string into a newly allocated <code>[Arc]&lt;[OsStr]&gt;</code>.
1311+
#[inline]
1312+
fn from(s: &mut OsStr) -> Arc<OsStr> {
1313+
Arc::from(&*s)
1314+
}
1315+
}
1316+
12991317
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
13001318
impl From<OsString> for Rc<OsStr> {
13011319
/// Converts an [`OsString`] into an <code>[Rc]<[OsStr]></code> by moving the [`OsString`]
@@ -1317,6 +1335,15 @@ impl From<&OsStr> for Rc<OsStr> {
13171335
}
13181336
}
13191337

1338+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1339+
impl From<&mut OsStr> for Rc<OsStr> {
1340+
/// Copies the string into a newly allocated <code>[Rc]&lt;[OsStr]&gt;</code>.
1341+
#[inline]
1342+
fn from(s: &mut OsStr) -> Rc<OsStr> {
1343+
Rc::from(&*s)
1344+
}
1345+
}
1346+
13201347
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
13211348
impl<'a> From<OsString> for Cow<'a, OsStr> {
13221349
/// Moves the string into a [`Cow::Owned`].

std/src/path.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,16 @@ impl From<&Path> for Box<Path> {
17621762
}
17631763
}
17641764

1765+
#[stable(feature = "box_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1766+
impl From<&mut Path> for Box<Path> {
1767+
/// Creates a boxed [`Path`] from a reference.
1768+
///
1769+
/// This will allocate and clone `path` to it.
1770+
fn from(path: &mut Path) -> Box<Path> {
1771+
Self::from(&*path)
1772+
}
1773+
}
1774+
17651775
#[stable(feature = "box_from_cow", since = "1.45.0")]
17661776
impl From<Cow<'_, Path>> for Box<Path> {
17671777
/// Creates a boxed [`Path`] from a clone-on-write pointer.
@@ -1990,6 +2000,15 @@ impl From<&Path> for Arc<Path> {
19902000
}
19912001
}
19922002

2003+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
2004+
impl From<&mut Path> for Arc<Path> {
2005+
/// Converts a [`Path`] into an [`Arc`] by copying the [`Path`] data into a new [`Arc`] buffer.
2006+
#[inline]
2007+
fn from(s: &mut Path) -> Arc<Path> {
2008+
Arc::from(&*s)
2009+
}
2010+
}
2011+
19932012
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
19942013
impl From<PathBuf> for Rc<Path> {
19952014
/// Converts a [`PathBuf`] into an <code>[Rc]<[Path]></code> by moving the [`PathBuf`] data into
@@ -2011,6 +2030,15 @@ impl From<&Path> for Rc<Path> {
20112030
}
20122031
}
20132032

2033+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
2034+
impl From<&mut Path> for Rc<Path> {
2035+
/// Converts a [`Path`] into an [`Rc`] by copying the [`Path`] data into a new [`Rc`] buffer.
2036+
#[inline]
2037+
fn from(s: &mut Path) -> Rc<Path> {
2038+
Rc::from(&*s)
2039+
}
2040+
}
2041+
20142042
#[stable(feature = "rust1", since = "1.0.0")]
20152043
impl ToOwned for Path {
20162044
type Owned = PathBuf;

0 commit comments

Comments
 (0)