pub trait Packed {
// Provided method
unsafe fn repr_c_optimization_safe(_version: u32) -> IsPacked { ... }
}
Expand description
This trait describes whether a type is such that it can just be blitted. See method repr_c_optimization_safe. Note! The name Packed is a little misleading. A better name would be ‘packed’
Provided Methods§
Sourceunsafe fn repr_c_optimization_safe(_version: u32) -> IsPacked
unsafe fn repr_c_optimization_safe(_version: u32) -> IsPacked
This method returns true if the optimization is allowed for the protocol version given as an argument. This may return true if and only if the given protocol version has a serialized format identical to the memory layout of the given protocol version. Note, the only memory layout existing is that of the most recent version, so Packed-optimization only works when disk-format is identical to memory version.
This can return true for types which have an in-memory layout that is packed and therefore identical to the layout that savefile will use on disk. This means that types for which this trait is implemented can be serialized very quickly by just writing their raw bits to disc.
Rules to allow returning true:
- The type must “be Copy” (i.e, implement the
Copy
-trait) - The type must not contain any padding (if there is padding, backward compatibility will fail, since in fallback mode regular savefile-deserialize will be used, and it will not use padding)
- The type must have a strictly deterministic memory layout (no field order randomization). This typically means repr(C)
- All the constituent types of the type must also implement
Packed
(correctly).
Constructing an instance of ‘IsPacked’ with value ‘true’ is not safe. See
documentation of ‘IsPacked’. The idea is that the Packed-trait itself
can still be safe to implement, it just won’t be possible to get hold of an
instance of IsPacked(true). That is, a safe implementation of Packed
can’t return
IsPacked(true), if everything else follows safety rules. To make it impossible to just
‘steal’ such a value from some other thing implementing ‘Packed’,
this method is marked unsafe (however, it can be left unimplemented,
making it still possible to safely implement Packed).
§Safety
The returned value must not be used, except by the Savefile-framework. It must not be forwarded anywhere else.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.