Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions library/core/src/ascii/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,26 @@ macro_rules! into_int_impl {

into_int_impl!(u8 u16 u32 u64 u128 char);

#[unstable(feature = "ascii_char", issue = "110998")]
impl AsRef<str> for AsciiChar {
#[inline(always)]
fn as_ref(&self) -> &str {
self.as_str()
}
}

#[unstable(feature = "ascii_char", issue = "110998")]
impl AsMut<str> for AsciiChar {
#[inline(always)]
fn as_mut(&mut self) -> &mut str {
let ascii_ptr: *mut [Self] = crate::slice::from_mut(self);
let str_ptr = ascii_ptr as *mut str;
// SAFETY: Each ASCII codepoint in UTF-8 is encoded as one single-byte
// code unit having the same value as the ASCII byte.
unsafe { &mut *str_ptr }
}
}
Comment on lines +1182 to +1191
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably be implemented in a new as_mut_str method, similar to as_str

Also, could everything here use #[inline] without the always?


impl [AsciiChar] {
/// Views this slice of ASCII characters as a UTF-8 `str`.
#[unstable(feature = "ascii_char", issue = "110998")]
Expand Down
24 changes: 24 additions & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,30 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Cell<U>> for Cell<T> {}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> AsRef<[Cell<T>; N]> for Cell<[T; N]> {
#[inline(always)]
fn as_ref(&self) -> &[Cell<T>; N] {
self.as_array_of_cells()
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> AsRef<[Cell<T>]> for Cell<[T; N]> {
#[inline(always)]
fn as_ref(&self) -> &[Cell<T>] {
&*self.as_array_of_cells()
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T> AsRef<[Cell<T>]> for Cell<[T]> {
#[inline(always)]
fn as_ref(&self) -> &[Cell<T>] {
self.as_slice_of_cells()
}
}

impl<T> Cell<[T]> {
/// Returns a `&[Cell<T>]` from a `&Cell<[T]>`
///
Expand Down
50 changes: 50 additions & 0 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,56 @@ impl<T, const N: usize> MaybeUninit<[T; N]> {
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> From<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]> {
#[inline(always)]
fn from(arr: [MaybeUninit<T>; N]) -> Self {
arr.transpose()
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> AsRef<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]> {
#[inline(always)]
fn as_ref(&self) -> &[MaybeUninit<T>; N] {
// SAFETY: T and MaybeUninit<T> have the same layout
unsafe { &*(self as *const MaybeUninit<[T; N]> as *const [MaybeUninit<T>; N]) }
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> AsRef<[MaybeUninit<T>]> for MaybeUninit<[T; N]> {
#[inline(always)]
fn as_ref(&self) -> &[MaybeUninit<T>] {
&*AsRef::<[MaybeUninit<T>; N]>::as_ref(self)
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> AsMut<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]> {
#[inline(always)]
fn as_mut(&mut self) -> &mut [MaybeUninit<T>; N] {
// SAFETY: T and MaybeUninit<T> have the same layout
unsafe { &mut *(self as *mut MaybeUninit<[T; N]> as *mut [MaybeUninit<T>; N]) }
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> AsMut<[MaybeUninit<T>]> for MaybeUninit<[T; N]> {
#[inline(always)]
fn as_mut(&mut self) -> &mut [MaybeUninit<T>] {
&mut *AsMut::<[MaybeUninit<T>; N]>::as_mut(self)
}
}

#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T, const N: usize> From<MaybeUninit<[T; N]>> for [MaybeUninit<T>; N] {
#[inline(always)]
fn from(arr: MaybeUninit<[T; N]>) -> Self {
arr.transpose()
}
}

impl<T, const N: usize> [MaybeUninit<T>; N] {
/// Transposes a `[MaybeUninit<T>; N]` into a `MaybeUninit<[T; N]>`.
///
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/issue-71394-no-from-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | let _: &[i8] = data.into();
`[T; 6]` implements `From<(T, T, T, T, T, T)>`
`[T; 7]` implements `From<(T, T, T, T, T, T, T)>`
`[T; 8]` implements `From<(T, T, T, T, T, T, T, T)>`
and 6 others
and 7 others
= note: required for `&[u8]` to implement `Into<&[i8]>`

error: aborting due to 1 previous error
Expand Down
Loading