Skip to content

Commit bd960c9

Browse files
Add some conversion trait impls
- `impl AsRef<str> for AsciiChar` (unstable) - `impl<T, const N: usize> From<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]>` - `impl<T, const N: usize> AsRef<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]>` - `impl<T, const N: usize> AsRef<[MaybeUninit<T>]> for MaybeUninit<[T; N]>` - `impl<T, const N: usize> AsMut<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]>` - `impl<T, const N: usize> AsMut<[MaybeUninit<T>]> for MaybeUninit<[T; N]>` - `impl<T, const N: usize> From<MaybeUninit<[T; N]>> for [MaybeUninit<T>; N]` - `impl<T, const N: usize> AsRef<[Cell<T>; N]> for Cell<[T; N]>` - `impl<T, const N: usize> AsRef<[Cell<T>]> for Cell<[T; N]>` - `impl<T> AsRef<[Cell<T>]> for Cell<[T]>`
1 parent 2e2642e commit bd960c9

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

library/core/src/ascii/ascii_char.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,14 @@ macro_rules! into_int_impl {
11691169

11701170
into_int_impl!(u8 u16 u32 u64 u128 char);
11711171

1172+
#[unstable(feature = "ascii_char", issue = "110998")]
1173+
impl AsRef<str> for AsciiChar {
1174+
#[inline(always)]
1175+
fn as_ref(&self) -> &str {
1176+
self.as_str()
1177+
}
1178+
}
1179+
11721180
impl [AsciiChar] {
11731181
/// Views this slice of ASCII characters as a UTF-8 `str`.
11741182
#[unstable(feature = "ascii_char", issue = "110998")]

library/core/src/cell.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,30 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
670670
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
671671
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Cell<U>> for Cell<T> {}
672672

673+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
674+
impl<T, const N: usize> AsRef<[Cell<T>; N]> for Cell<[T; N]> {
675+
#[inline(always)]
676+
fn as_ref(&self) -> &[Cell<T>; N] {
677+
self.as_array_of_cells()
678+
}
679+
}
680+
681+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
682+
impl<T, const N: usize> AsRef<[Cell<T>]> for Cell<[T; N]> {
683+
#[inline(always)]
684+
fn as_ref(&self) -> &[Cell<T>] {
685+
&*self.as_array_of_cells()
686+
}
687+
}
688+
689+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
690+
impl<T> AsRef<[Cell<T>]> for Cell<[T]> {
691+
#[inline(always)]
692+
fn as_ref(&self) -> &[Cell<T>] {
693+
self.as_slice_of_cells()
694+
}
695+
}
696+
673697
impl<T> Cell<[T]> {
674698
/// Returns a `&[Cell<T>]` from a `&Cell<[T]>`
675699
///

library/core/src/mem/maybe_uninit.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,58 @@ impl<T, const N: usize> MaybeUninit<[T; N]> {
14521452
}
14531453
}
14541454

1455+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
1456+
impl<T, const N: usize> From<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]> {
1457+
#[inline(always)]
1458+
fn from(arr: [MaybeUninit<T>; N]) -> Self {
1459+
// SAFETY: T and MaybeUninit<T> have the same layout
1460+
unsafe { intrinsics::transmute_unchecked(arr) }
1461+
}
1462+
}
1463+
1464+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
1465+
impl<T, const N: usize> AsRef<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]> {
1466+
#[inline(always)]
1467+
fn as_ref(&self) -> &[MaybeUninit<T>; N] {
1468+
// SAFETY: T and MaybeUninit<T> have the same layout
1469+
unsafe { &*(self as *const MaybeUninit<[T; N]> as *const [MaybeUninit<T>; N]) }
1470+
}
1471+
}
1472+
1473+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
1474+
impl<T, const N: usize> AsRef<[MaybeUninit<T>]> for MaybeUninit<[T; N]> {
1475+
#[inline(always)]
1476+
fn as_ref(&self) -> &[MaybeUninit<T>] {
1477+
&*AsRef::<[MaybeUninit<T>; N]>::as_ref(self)
1478+
}
1479+
}
1480+
1481+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
1482+
impl<T, const N: usize> AsMut<[MaybeUninit<T>; N]> for MaybeUninit<[T; N]> {
1483+
#[inline(always)]
1484+
fn as_mut(&mut self) -> &mut [MaybeUninit<T>; N] {
1485+
// SAFETY: T and MaybeUninit<T> have the same layout
1486+
unsafe { &mut *(self as *mut MaybeUninit<[T; N]> as *mut [MaybeUninit<T>; N]) }
1487+
}
1488+
}
1489+
1490+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
1491+
impl<T, const N: usize> AsMut<[MaybeUninit<T>]> for MaybeUninit<[T; N]> {
1492+
#[inline(always)]
1493+
fn as_mut(&mut self) -> &mut [MaybeUninit<T>] {
1494+
&mut *AsMut::<[MaybeUninit<T>; N]>::as_mut(self)
1495+
}
1496+
}
1497+
1498+
#[stable(feature = "more_conversion_trait_impls", since = "CURRENT_RUSTC_VERSION")]
1499+
impl<T, const N: usize> From<MaybeUninit<[T; N]>> for [MaybeUninit<T>; N] {
1500+
#[inline(always)]
1501+
fn from(arr: MaybeUninit<[T; N]>) -> Self {
1502+
// SAFETY: T and MaybeUninit<T> have the same layout
1503+
unsafe { intrinsics::transmute_unchecked(arr) }
1504+
}
1505+
}
1506+
14551507
impl<T, const N: usize> [MaybeUninit<T>; N] {
14561508
/// Transposes a `[MaybeUninit<T>; N]` into a `MaybeUninit<[T; N]>`.
14571509
///

tests/ui/suggestions/issue-71394-no-from-impl.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ LL | let _: &[i8] = data.into();
55
| ^^^^ the trait `From<&[u8]>` is not implemented for `&[i8]`
66
|
77
= help: the following other types implement trait `From<T>`:
8+
`[MaybeUninit<T>; N]` implements `From<MaybeUninit<[T; N]>>`
89
`[T; 10]` implements `From<(T, T, T, T, T, T, T, T, T, T)>`
910
`[T; 11]` implements `From<(T, T, T, T, T, T, T, T, T, T, T)>`
1011
`[T; 12]` implements `From<(T, T, T, T, T, T, T, T, T, T, T, T)>`
1112
`[T; 1]` implements `From<(T,)>`
1213
`[T; 2]` implements `From<(T, T)>`
1314
`[T; 3]` implements `From<(T, T, T)>`
1415
`[T; 4]` implements `From<(T, T, T, T)>`
15-
`[T; 5]` implements `From<(T, T, T, T, T)>`
16-
and 6 others
16+
and 7 others
1717
= note: required for `&[u8]` to implement `Into<&[i8]>`
1818

1919
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)