Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fa63ef4

Browse files
committedAug 21, 2024
Auto merge of #129329 - eduardosm:rc-from-mut-slice, r=<try>
Implement `From<&mut {slice}>` for `Rc/Arc<{slice}>` ACP: rust-lang/libs-team#424 New API: ```rust 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 982c6f8 + df68072 commit fa63ef4

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
 

‎library/alloc/src/ffi/c_str.rs

+21
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,17 @@ impl From<&CStr> for Arc<CStr> {
884884
}
885885
}
886886

887+
#[cfg(target_has_atomic = "ptr")]
888+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
889+
impl From<&mut CStr> for Arc<CStr> {
890+
/// Converts a `&mut CStr` into a `Arc<CStr>`,
891+
/// by copying the contents into a newly allocated [`Arc`].
892+
#[inline]
893+
fn from(s: &mut CStr) -> Arc<CStr> {
894+
Arc::from(&*s)
895+
}
896+
}
897+
887898
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
888899
impl From<CString> for Rc<CStr> {
889900
/// Converts a [`CString`] into an <code>[Rc]<[CStr]></code> by moving the [`CString`]
@@ -906,6 +917,16 @@ impl From<&CStr> for Rc<CStr> {
906917
}
907918
}
908919

920+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
921+
impl From<&mut CStr> for Rc<CStr> {
922+
/// Converts a `&mut CStr` into a `Rc<CStr>`,
923+
/// by copying the contents into a newly allocated [`Rc`].
924+
#[inline]
925+
fn from(s: &mut CStr) -> Rc<CStr> {
926+
Rc::from(&*s)
927+
}
928+
}
929+
909930
#[cfg(not(no_global_oom_handling))]
910931
#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
911932
impl Default for Rc<CStr> {

‎library/alloc/src/rc.rs

+40
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,26 @@ impl<T: Clone> From<&[T]> for Rc<[T]> {
26072607
}
26082608
}
26092609

2610+
#[cfg(not(no_global_oom_handling))]
2611+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
2612+
impl<T: Clone> From<&mut [T]> for Rc<[T]> {
2613+
/// Allocates a reference-counted slice and fills it by cloning `v`'s items.
2614+
///
2615+
/// # Example
2616+
///
2617+
/// ```
2618+
/// # use std::rc::Rc;
2619+
/// let mut original = [1, 2, 3];
2620+
/// let original: &mut [i32] = &mut original;
2621+
/// let shared: Rc<[i32]> = Rc::from(original);
2622+
/// assert_eq!(&[1, 2, 3], &shared[..]);
2623+
/// ```
2624+
#[inline]
2625+
fn from(v: &mut [T]) -> Rc<[T]> {
2626+
Rc::from(&*v)
2627+
}
2628+
}
2629+
26102630
#[cfg(not(no_global_oom_handling))]
26112631
#[stable(feature = "shared_from_slice", since = "1.21.0")]
26122632
impl From<&str> for Rc<str> {
@@ -2626,6 +2646,26 @@ impl From<&str> for Rc<str> {
26262646
}
26272647
}
26282648

2649+
#[cfg(not(no_global_oom_handling))]
2650+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
2651+
impl From<&mut str> for Rc<str> {
2652+
/// Allocates a reference-counted string slice and copies `v` into it.
2653+
///
2654+
/// # Example
2655+
///
2656+
/// ```
2657+
/// # use std::rc::Rc;
2658+
/// let mut original = String::from("statue");
2659+
/// let original: &mut str = &mut original;
2660+
/// let shared: Rc<str> = Rc::from(original);
2661+
/// assert_eq!("statue", &shared[..]);
2662+
/// ```
2663+
#[inline]
2664+
fn from(v: &mut str) -> Rc<str> {
2665+
Rc::from(&*v)
2666+
}
2667+
}
2668+
26292669
#[cfg(not(no_global_oom_handling))]
26302670
#[stable(feature = "shared_from_slice", since = "1.21.0")]
26312671
impl From<String> for Rc<str> {

‎library/alloc/src/sync.rs

+40
Original file line numberDiff line numberDiff line change
@@ -3565,6 +3565,26 @@ impl<T: Clone> From<&[T]> for Arc<[T]> {
35653565
}
35663566
}
35673567

3568+
#[cfg(not(no_global_oom_handling))]
3569+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
3570+
impl<T: Clone> From<&mut [T]> for Arc<[T]> {
3571+
/// Allocates a reference-counted slice and fills it by cloning `v`'s items.
3572+
///
3573+
/// # Example
3574+
///
3575+
/// ```
3576+
/// # use std::sync::Arc;
3577+
/// let mut original = [1, 2, 3];
3578+
/// let original: &mut [i32] = &mut original;
3579+
/// let shared: Arc<[i32]> = Arc::from(original);
3580+
/// assert_eq!(&[1, 2, 3], &shared[..]);
3581+
/// ```
3582+
#[inline]
3583+
fn from(v: &mut [T]) -> Arc<[T]> {
3584+
Arc::from(&*v)
3585+
}
3586+
}
3587+
35683588
#[cfg(not(no_global_oom_handling))]
35693589
#[stable(feature = "shared_from_slice", since = "1.21.0")]
35703590
impl From<&str> for Arc<str> {
@@ -3584,6 +3604,26 @@ impl From<&str> for Arc<str> {
35843604
}
35853605
}
35863606

3607+
#[cfg(not(no_global_oom_handling))]
3608+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
3609+
impl From<&mut str> for Arc<str> {
3610+
/// Allocates a reference-counted `str` and copies `v` into it.
3611+
///
3612+
/// # Example
3613+
///
3614+
/// ```
3615+
/// # use std::sync::Arc;
3616+
/// let mut original = String::from("eggplant");
3617+
/// let original: &mut str = &mut original;
3618+
/// let shared: Arc<str> = Arc::from(original);
3619+
/// assert_eq!("eggplant", &shared[..]);
3620+
/// ```
3621+
#[inline]
3622+
fn from(v: &mut str) -> Arc<str> {
3623+
Arc::from(&*v)
3624+
}
3625+
}
3626+
35873627
#[cfg(not(no_global_oom_handling))]
35883628
#[stable(feature = "shared_from_slice", since = "1.21.0")]
35893629
impl From<String> for Arc<str> {

‎library/std/src/ffi/os_str.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,15 @@ impl From<&OsStr> for Arc<OsStr> {
12951295
}
12961296
}
12971297

1298+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1299+
impl From<&mut OsStr> for Arc<OsStr> {
1300+
/// Copies the string into a newly allocated <code>[Arc]&lt;[OsStr]&gt;</code>.
1301+
#[inline]
1302+
fn from(s: &mut OsStr) -> Arc<OsStr> {
1303+
Arc::from(&*s)
1304+
}
1305+
}
1306+
12981307
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
12991308
impl From<OsString> for Rc<OsStr> {
13001309
/// Converts an [`OsString`] into an <code>[Rc]<[OsStr]></code> by moving the [`OsString`]
@@ -1316,6 +1325,15 @@ impl From<&OsStr> for Rc<OsStr> {
13161325
}
13171326
}
13181327

1328+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1329+
impl From<&mut OsStr> for Rc<OsStr> {
1330+
/// Copies the string into a newly allocated <code>[Rc]&lt;[OsStr]&gt;</code>.
1331+
#[inline]
1332+
fn from(s: &mut OsStr) -> Rc<OsStr> {
1333+
Rc::from(&*s)
1334+
}
1335+
}
1336+
13191337
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
13201338
impl<'a> From<OsString> for Cow<'a, OsStr> {
13211339
/// Moves the string into a [`Cow::Owned`].

‎library/std/src/path.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,15 @@ impl From<&Path> for Arc<Path> {
19641964
}
19651965
}
19661966

1967+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1968+
impl From<&mut Path> for Arc<Path> {
1969+
/// Converts a [`Path`] into an [`Arc`] by copying the [`Path`] data into a new [`Arc`] buffer.
1970+
#[inline]
1971+
fn from(s: &mut Path) -> Arc<Path> {
1972+
Arc::from(&*s)
1973+
}
1974+
}
1975+
19671976
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
19681977
impl From<PathBuf> for Rc<Path> {
19691978
/// Converts a [`PathBuf`] into an <code>[Rc]<[Path]></code> by moving the [`PathBuf`] data into
@@ -1985,6 +1994,15 @@ impl From<&Path> for Rc<Path> {
19851994
}
19861995
}
19871996

1997+
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
1998+
impl From<&mut Path> for Rc<Path> {
1999+
/// Converts a [`Path`] into an [`Rc`] by copying the [`Path`] data into a new [`Rc`] buffer.
2000+
#[inline]
2001+
fn from(s: &mut Path) -> Rc<Path> {
2002+
Rc::from(&*s)
2003+
}
2004+
}
2005+
19882006
#[stable(feature = "rust1", since = "1.0.0")]
19892007
impl ToOwned for Path {
19902008
type Owned = PathBuf;

0 commit comments

Comments
 (0)
Failed to load comments.