Skip to content

mem::transmute on types of different size (but the type size is known) #47966

Closed
@Avarel

Description

@Avarel

Problem

When I try to compile this method, the compiler reports that mem::transmute is being called on types of different sizes. (Which doesn't make sense because T size should be known in this context).

pub fn partition2<T: Sized>(source: [T; 52]) -> [[T; 13]; 4] {
    use std::mem;
    unsafe { mem::transmute(source) }
}

Compiler Error

error[E0512]: transmute called with types of different sizes
 --> src/main.rs:9:14
  |
9 |     unsafe { mem::transmute(source) }
  |              ^^^^^^^^^^^^^^
  |
  = note: source type: [T; 52] (size can vary because of T)
  = note: target type: [[T; 13]; 4] (size can vary because of T)

Working Example

Replacing T with a struct and it compiles fine (also works with String):

pub struct Card {
    value: u8,
    suit: Suit
}

pub enum Suit {
    Spades,
    Clubs,
    Diamonds,
    Hearts,
}

pub fn partition(source: [Card; 52]) -> [[Card; 13]; 4] {
    use std::mem;
    unsafe { mem::transmute(source) }
}

Meta

rustc 1.24.0-nightly (23032d0af 2017-11-30)
binary: rustc
commit-hash: 23032d0afa2b0e0c60a9b2ae62709f846d90007c
commit-date: 2017-11-30
host: x86_64-apple-darwin
release: 1.24.0-nightly
LLVM version: 4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions