Skip to content

Commit 1948307

Browse files
committed
Support sized-to-unsized transmute_{ref,mut}!
Closes #2721 gherrit-pr-id: G73f67c103188ed404d0051bc140c4d0711fe0753
1 parent a184611 commit 1948307

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/macros.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,13 @@ mod tests {
11961196
const X: &'static [[u8; 2]; 4] = transmute_ref!(&ARRAY_OF_U8S);
11971197
assert_eq!(*X, ARRAY_OF_ARRAYS);
11981198

1199+
// Test sized -> unsized transmutation.
1200+
let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1201+
let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1202+
let slice_of_arrays = &array_of_arrays[..];
1203+
let x: &[[u8; 2]] = transmute_ref!(&array_of_u8s);
1204+
assert_eq!(x, slice_of_arrays);
1205+
11991206
// Before 1.61.0, we can't define the `const fn transmute_ref` function
12001207
// that we do on and after 1.61.0.
12011208
#[cfg(no_zerocopy_generic_bounds_in_const_fn_1_61_0)]

src/util/macro_util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ pub trait TransmuteRefDst<'a> {
820820

821821
impl<'a, Src: ?Sized, Dst: ?Sized> TransmuteRefDst<'a> for Wrap<&'a Src, &'a Dst>
822822
where
823-
Src: KnownLayout<PointerMetadata = usize> + IntoBytes + Immutable,
823+
Src: KnownLayout + IntoBytes + Immutable,
824824
Dst: KnownLayout<PointerMetadata = usize> + FromBytes + Immutable,
825825
{
826826
type Dst = Dst;
@@ -853,7 +853,7 @@ pub trait TransmuteMutDst<'a> {
853853

854854
impl<'a, Src: ?Sized, Dst: ?Sized> TransmuteMutDst<'a> for Wrap<&'a mut Src, &'a mut Dst>
855855
where
856-
Src: KnownLayout<PointerMetadata = usize> + FromBytes + IntoBytes,
856+
Src: KnownLayout + FromBytes + IntoBytes,
857857
Dst: KnownLayout<PointerMetadata = usize> + FromBytes + IntoBytes,
858858
{
859859
type Dst = Dst;

0 commit comments

Comments
 (0)