pub unsafe trait ToBinary {
const POD: bool = false;
// Required method
fn encode_binary(&self, encoder: &mut BytesWriter<'_>);
}
Expand description
A trait for converting a value to a compact binary representation.
This trait is used to append the binary encoding of a type to the end of a provided encoder.
For more details on the specific binary implementation, see the binary
module.
§Safety
If you use the default implementation (i.e., POD = false
), this trait is always safe to implement.
When POD
is set to true
, it indicates that this type can be directly memory-copied
to the output, maintaining the same representation as if it were encoded field-by-field.
§Constraints for POD = true
:
- Every bit pattern must be valid for the type and layout.
- There must be no padding between fields.
- Fields must be laid out in the same order as they would be encoded and decoded via the
encode_binary
function.
§Notes
- The
encode_binary
function should always write a value, even in error cases. There is no error return;
Provided Associated Constants§
Required Methods§
Sourcefn encode_binary(&self, encoder: &mut BytesWriter<'_>)
fn encode_binary(&self, encoder: &mut BytesWriter<'_>)
Encodes the type into its binary representation.
This function should append the binary encoding of self
to the provided encoder
.
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.