Bug with `Sized` with generic type?

I was trying to create a small wrapper around transmute when the source and the destination is known:

use std::mem::{size_of, transmute};

type SomeUnion = i64; // not the type I really use in my code
unsafe fn cast<T: Sized>(data: &SomeUnion) -> &[u8]
    let data: &[u8; size_of::<T>()] = unsafe{transmute(data)};

For some reason the compiler fails to find the size of T even if I explicitely asked for T to be Sized:

error[E0277]: the size for values of type `T` cannot be known at compilation time
   --> src/lib.rs:5:31
3   | fn cast<T: Sized>(data: &i64) -> &[u8]
    |         - this type parameter needs to be `std::marker::Sized`
4   | {
5   |     let data: &[u8; size_of::<T>()] = unsafe{transmute(data)};
    |                               ^ doesn't have a size known at compile-time
    = help: the trait `std::marker::Sized` is not implemented for `T`
    = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

It looks like a bug, but before opening one, I wanted to be sure that it wasn't a bad understanding on my side.


This bug has already reported as #43408.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.