Skip to content

Commit

Permalink
Implement From<&mut {slice}> for Rc/Arc<{slice}>
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosm committed Aug 20, 2024
1 parent 4d5b3b1 commit df68072
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,17 @@ impl From<&CStr> for Arc<CStr> {
}
}

#[cfg(target_has_atomic = "ptr")]
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut CStr> for Arc<CStr> {
/// Converts a `&mut CStr` into a `Arc<CStr>`,
/// by copying the contents into a newly allocated [`Arc`].
#[inline]
fn from(s: &mut CStr) -> Arc<CStr> {
Arc::from(&*s)
}
}

#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<CString> for Rc<CStr> {
/// Converts a [`CString`] into an <code>[Rc]<[CStr]></code> by moving the [`CString`]
Expand All @@ -906,6 +917,16 @@ impl From<&CStr> for Rc<CStr> {
}
}

#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut CStr> for Rc<CStr> {
/// Converts a `&mut CStr` into a `Rc<CStr>`,
/// by copying the contents into a newly allocated [`Rc`].
#[inline]
fn from(s: &mut CStr) -> Rc<CStr> {
Rc::from(&*s)
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
impl Default for Rc<CStr> {
Expand Down
40 changes: 40 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,26 @@ impl<T: Clone> From<&[T]> for Rc<[T]> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl<T: Clone> From<&mut [T]> for Rc<[T]> {
/// Allocates a reference-counted slice and fills it by cloning `v`'s items.
///
/// # Example
///
/// ```
/// # use std::rc::Rc;
/// let mut original = [1, 2, 3];
/// let original: &mut [i32] = &mut original;
/// let shared: Rc<[i32]> = Rc::from(original);
/// assert_eq!(&[1, 2, 3], &shared[..]);
/// ```
#[inline]
fn from(v: &mut [T]) -> Rc<[T]> {
Rc::from(&*v)
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl From<&str> for Rc<str> {
Expand All @@ -2626,6 +2646,26 @@ impl From<&str> for Rc<str> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut str> for Rc<str> {
/// Allocates a reference-counted string slice and copies `v` into it.
///
/// # Example
///
/// ```
/// # use std::rc::Rc;
/// let mut original = String::from("statue");
/// let original: &mut str = &mut original;
/// let shared: Rc<str> = Rc::from(original);
/// assert_eq!("statue", &shared[..]);
/// ```
#[inline]
fn from(v: &mut str) -> Rc<str> {
Rc::from(&*v)
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl From<String> for Rc<str> {
Expand Down
40 changes: 40 additions & 0 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,26 @@ impl<T: Clone> From<&[T]> for Arc<[T]> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl<T: Clone> From<&mut [T]> for Arc<[T]> {
/// Allocates a reference-counted slice and fills it by cloning `v`'s items.
///
/// # Example
///
/// ```
/// # use std::sync::Arc;
/// let mut original = [1, 2, 3];
/// let original: &mut [i32] = &mut original;
/// let shared: Arc<[i32]> = Arc::from(original);
/// assert_eq!(&[1, 2, 3], &shared[..]);
/// ```
#[inline]
fn from(v: &mut [T]) -> Arc<[T]> {
Arc::from(&*v)
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl From<&str> for Arc<str> {
Expand All @@ -3584,6 +3604,26 @@ impl From<&str> for Arc<str> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut str> for Arc<str> {
/// Allocates a reference-counted `str` and copies `v` into it.
///
/// # Example
///
/// ```
/// # use std::sync::Arc;
/// let mut original = String::from("eggplant");
/// let original: &mut str = &mut original;
/// let shared: Arc<str> = Arc::from(original);
/// assert_eq!("eggplant", &shared[..]);
/// ```
#[inline]
fn from(v: &mut str) -> Arc<str> {
Arc::from(&*v)
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl From<String> for Arc<str> {
Expand Down
18 changes: 18 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,15 @@ impl From<&OsStr> for Arc<OsStr> {
}
}

#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut OsStr> for Arc<OsStr> {
/// Copies the string into a newly allocated <code>[Arc]&lt;[OsStr]&gt;</code>.
#[inline]
fn from(s: &mut OsStr) -> Arc<OsStr> {
Arc::from(&*s)
}
}

#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<OsString> for Rc<OsStr> {
/// Converts an [`OsString`] into an <code>[Rc]<[OsStr]></code> by moving the [`OsString`]
Expand All @@ -1316,6 +1325,15 @@ impl From<&OsStr> for Rc<OsStr> {
}
}

#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut OsStr> for Rc<OsStr> {
/// Copies the string into a newly allocated <code>[Rc]&lt;[OsStr]&gt;</code>.
#[inline]
fn from(s: &mut OsStr) -> Rc<OsStr> {
Rc::from(&*s)
}
}

#[stable(feature = "cow_from_osstr", since = "1.28.0")]
impl<'a> From<OsString> for Cow<'a, OsStr> {
/// Moves the string into a [`Cow::Owned`].
Expand Down
18 changes: 18 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,15 @@ impl From<&Path> for Arc<Path> {
}
}

#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut Path> for Arc<Path> {
/// Converts a [`Path`] into an [`Arc`] by copying the [`Path`] data into a new [`Arc`] buffer.
#[inline]
fn from(s: &mut Path) -> Arc<Path> {
Arc::from(&*s)
}
}

#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<PathBuf> for Rc<Path> {
/// Converts a [`PathBuf`] into an <code>[Rc]<[Path]></code> by moving the [`PathBuf`] data into
Expand All @@ -1985,6 +1994,15 @@ impl From<&Path> for Rc<Path> {
}
}

#[stable(feature = "shared_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
impl From<&mut Path> for Rc<Path> {
/// Converts a [`Path`] into an [`Rc`] by copying the [`Path`] data into a new [`Rc`] buffer.
#[inline]
fn from(s: &mut Path) -> Rc<Path> {
Rc::from(&*s)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for Path {
type Owned = PathBuf;
Expand Down

0 comments on commit df68072

Please sign in to comment.