When I try to compile this code:
pub trait Bytes {
type Type;
}
impl<T> Bytes for T
where
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
where
Self: Sized,
{
type Type;
}