pub struct Configuration<const ZERO_COPY_ALIGN_CHECK: bool = true, const PREALLOCATION_SIZE_LIMIT: usize = DEFAULT_PREALLOCATION_SIZE_LIMIT, LengthEncoding = BincodeLen, ByteOrder = LittleEndian, IntEncoding = FixInt, TagEncoding = u32> { /* private fields */ }Expand description
Compile-time configuration for runtime behavior.
Defaults:
- Zero-copy alignment check is enabled.
- Preallocation size limit is 4 MiB.
- Length encoding is
BincodeLen. - Byte order is
LittleEndian. - Integer encoding is
FixInt. - Tag encoding is
u32.
Implementations§
Source§impl Configuration
impl Configuration
Sourcepub const fn default() -> DefaultConfig
pub const fn default() -> DefaultConfig
Create a new configuration with the default settings.
Defaults:
- Zero-copy alignment check is enabled.
- Preallocation size limit is 4 MiB.
- Length encoding is
BincodeLen. - Byte order is
LittleEndian. - Integer encoding is
FixInt.
Source§impl<const ZERO_COPY_ALIGN_CHECK: bool, const PREALLOCATION_SIZE_LIMIT: usize, LengthEncoding, ByteOrder, IntEncoding, TagEncoding> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding, TagEncoding>
impl<const ZERO_COPY_ALIGN_CHECK: bool, const PREALLOCATION_SIZE_LIMIT: usize, LengthEncoding, ByteOrder, IntEncoding, TagEncoding> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding, TagEncoding>
pub const fn new() -> Self
Sourcepub const fn with_length_encoding<L>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, L, ByteOrder, IntEncoding>where
Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, L>: Config,
pub const fn with_length_encoding<L>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, L, ByteOrder, IntEncoding>where
Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, L>: Config,
Use the given SeqLen implementation for sequence length encoding.
Default is BincodeLen.
Note that this default can be overridden for individual cases by using
containers.
Sourcepub const fn with_big_endian(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, BigEndian, IntEncoding>
pub const fn with_big_endian( self, ) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, BigEndian, IntEncoding>
Use big-endian byte order.
Note that changing the byte order will have a direct impact on zero-copy eligibility. Integers are only eligible for zero-copy when configured byte order matches the native byte order.
Default is LittleEndian.
Sourcepub const fn with_little_endian(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, LittleEndian, IntEncoding>
pub const fn with_little_endian( self, ) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, LittleEndian, IntEncoding>
Use little-endian byte order.
Default is LittleEndian.
Sourcepub const fn with_platform_endian(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, LittleEndian, IntEncoding>
Available on little-endian only.
pub const fn with_platform_endian( self, ) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, LittleEndian, IntEncoding>
Use target platform byte order.
Will use the native byte order of the target platform.
Sourcepub const fn with_fixint_encoding(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, FixInt>
pub const fn with_fixint_encoding( self, ) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, FixInt>
Sourcepub const fn with_varint_encoding(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, VarInt>
pub const fn with_varint_encoding( self, ) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, VarInt>
Use VarInt for integer encoding.
Default is FixInt.
Performance note: variable length integer encoding will hurt serialization and deserialization performance significantly relative to fixed width integer encoding. Additionally, all zero-copy capabilities on integers will be lost. Variable length integer encoding may be beneficial if reducing the resulting size of serialized data is important, but if serialization / deserialization performance is important, fixed width integer encoding is highly recommended.
Sourcepub const fn with_int_encoding<I>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, I>where
Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, I>: Config,
pub const fn with_int_encoding<I>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, I>where
Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, I>: Config,
Use the given IntEncoding implementation for integer encoding.
Can be used for custom, unofficial integer encodings.
Default is FixInt.
Sourcepub const fn enable_zero_copy_align_check(
self,
) -> Configuration<true, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding>
pub const fn enable_zero_copy_align_check( self, ) -> Configuration<true, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding>
Enable the zero-copy alignment check.
If enabled, zero-copy deserialization will ensure that pointers are correctly aligned for the target type before creating references. You should keep this enabled unless you have a very specific use case for disabling it.
This is enabled by default.
Sourcepub const unsafe fn disable_zero_copy_align_check(
self,
) -> Configuration<false, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding>
pub const unsafe fn disable_zero_copy_align_check( self, ) -> Configuration<false, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding>
Disable the zero-copy alignment check.
When disabled, zero-copy deserialization (&'de T and &'de [T] for T: ZeroCopy)
will not verify that pointers into the buffer are correctly aligned before forming
references. Creating a misaligned reference is undefined behavior.
§Safety
You must guarantee every zero-copy reference is correctly aligned for its type.
This holds when:
- The buffer is aligned to at least
align_of::<T>()for each zero-copy typeT, and each zero-copy read occurs at an offset that preserves that alignment. - Or you only deserialize types with alignment 1 (e.g.,
&[u8],&[u8; N],&str, etc).
Only disable this when you control the serialized layout and can enforce alignment; owned deserialization paths are unaffected.
Sourcepub const fn with_preallocation_size_limit<const LIMIT: usize>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, LIMIT, LengthEncoding, ByteOrder, IntEncoding>
pub const fn with_preallocation_size_limit<const LIMIT: usize>( self, ) -> Configuration<ZERO_COPY_ALIGN_CHECK, LIMIT, LengthEncoding, ByteOrder, IntEncoding>
Set the preallocation size limit in bytes.
wincode will preallocate all sequences up to this limit, or error if the size of the allocation would exceed this limit. This is used to prevent malicious data from causing excessive memory usage or OOM.
The default limit is 4 MiB.
Sourcepub const fn disable_preallocation_size_limit(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT_DISABLED, LengthEncoding, ByteOrder, IntEncoding>
pub const fn disable_preallocation_size_limit( self, ) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT_DISABLED, LengthEncoding, ByteOrder, IntEncoding>
Disable the preallocation size limit.
Sourcepub const fn with_tag_encoding<T>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding, T>where
Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding, T>: Config,
pub const fn with_tag_encoding<T>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding, T>where
Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding, T>: Config,
Use the given TagEncoding implementation for enum discriminant encoding.
Default is u32.
This can be overriden for individual cases with the #[wincode(tag_encoding = ...)]
attribute.