According to bytemuck's doc here, Pod
is only a marker trait. Its definition is as following:
pub unsafe trait Pod: Zeroable + Copy + 'static { }
My understanding of a "marker trait" is that: its purpose is to declare some types to have some traits (here I mean "trait" in English, a synonym of "characteristics") that cannot be described by functions/methods. In this way, others (especially the consumers of these types) know how to properly handle these types. For example, Copy
is a marker trait. When marking a type as Copy
, the Rust compiler knows when dealing a = b
for this type, it should do a bitwise copy. Send
is a also a marker trait. When marking, the compiler knows this type could be sent to another thread.
Come back to Pod
. If I declare that my type is a Pod
, why should I at least make my type a Copy
? When I mark my type as Pod
, I'm saying to bytemuck
, "Hey, my type is a Pod
. You can trust me without any doubt. My type is absolutely a Pod
. You can now reinterpret some bytes as my type."
Is it because bytemuck
wants to make sure any type that contains non-Copy
types can't be marked as Pod
? Since "A type is Copy
" is indeed a necessary but not sufficient condition of "A type is Pod
" (After all, how can a type be plain old data if it's even not Copy
). In this way, for example, the developer won't accidentally include a non-Copy
type (like Vec<u8>
and String
) in the custom struct.
If this is actually the reason, I'm still confused. I still need to mark an extremely large type as Copy
only to mark it as Pod
. The following struct is an example.
struct MyStruct {
data: [u8; 10000000],
}
This is somewhat ridiculous. What if I accidentally trigger a bitwise copy of this type?
Isn't it a better idea not to make Copy
a supertrait of Pod
? I (as the code author) will take the full responsibility for every type that I mark as Pod
.