`T` cannot be known at compilation time with `Sized` bound

When I try to compile this code:

pub trait Bytes {
    type Type;

impl<T> Bytes for T
        T: Sized,
    type Type = [u8; std::mem::size_of::<T>()];

The compiler takes me an error:

error[E0277]: the size for values of type `T` cannot be known at compilation time
   --> src\from_bytes.rs:16:42
14  |         T: Sized,
    |                  - help: consider further restricting type parameter `T`: `, T: std::marker::Sized`
15  | {
16  |     type Type = [u8; std::mem::size_of::<T>()];
    |                                          ^ 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>

But did I restrict the type T to be Sized? Right?
Then what does the compiler need?

Also, I tried to write like this. But that doesn't help either.

pub trait Bytes
        Self: Sized,
    type Type;
1 Like

I think the error message here is misleading. It depends how the compiler monomorphises (which I'm not super expert in), but for this impl to work, the compiler needs to know the size of T - but it doesn't, it only knows that he has some size (not what it is exactly).

Wait for other people to comment first, but might be worth raising as a diagnostics bug at https://github.com/rust-lang/rust.


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