use {
crate::{
int_encoding::{BigEndian, ByteOrder, FixInt, IntEncoding, LittleEndian, VarInt},
len::{BincodeLen, SeqLen},
tag_encoding::TagEncoding,
},
core::marker::PhantomData,
};
pub const DEFAULT_PREALLOCATION_SIZE_LIMIT: usize = 4 << 20; pub const PREALLOCATION_SIZE_LIMIT_DISABLED: usize = usize::MAX;
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,
> {
_l: PhantomData<LengthEncoding>,
_b: PhantomData<ByteOrder>,
_i: PhantomData<IntEncoding>,
_t: PhantomData<TagEncoding>,
}
impl<
const ZERO_COPY_ALIGN_CHECK: bool,
const PREALLOCATION_SIZE_LIMIT: usize,
LengthEncoding,
ByteOrder,
IntEncoding,
TagEncoding,
> Clone
for Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
ByteOrder,
IntEncoding,
TagEncoding,
>
{
fn clone(&self) -> Self {
*self
}
}
impl<
const ZERO_COPY_ALIGN_CHECK: bool,
const PREALLOCATION_SIZE_LIMIT: usize,
LengthEncoding,
ByteOrder,
IntEncoding,
TagEncoding,
> Copy
for Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
ByteOrder,
IntEncoding,
TagEncoding,
>
{
}
const fn generate<
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,
> {
Configuration {
_l: PhantomData,
_b: PhantomData,
_i: PhantomData,
_t: PhantomData,
}
}
impl Configuration {
pub const fn default() -> DefaultConfig {
generate()
}
}
pub type DefaultConfig = Configuration;
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,
>
{
#[expect(clippy::new_without_default)]
pub const fn new() -> Self {
generate()
}
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,
{
generate()
}
pub const fn with_big_endian(
self,
) -> Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
BigEndian,
IntEncoding,
> {
generate()
}
pub const fn with_little_endian(
self,
) -> Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
LittleEndian,
IntEncoding,
> {
generate()
}
#[cfg(target_endian = "little")]
pub const fn with_platform_endian(
self,
) -> Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
LittleEndian,
IntEncoding,
> {
generate()
}
#[cfg(target_endian = "big")]
pub const fn with_platform_endian(
self,
) -> Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
BigEndian,
IntEncoding,
> {
generate()
}
pub const fn with_fixint_encoding(
self,
) -> Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
ByteOrder,
FixInt,
> {
generate()
}
pub const fn with_varint_encoding(
self,
) -> Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
ByteOrder,
VarInt,
> {
generate()
}
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,
{
generate()
}
pub const fn enable_zero_copy_align_check(
self,
) -> Configuration<true, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding> {
generate()
}
pub const unsafe fn disable_zero_copy_align_check(
self,
) -> Configuration<false, PREALLOCATION_SIZE_LIMIT, LengthEncoding, ByteOrder, IntEncoding>
{
generate()
}
pub const fn with_preallocation_size_limit<const LIMIT: usize>(
self,
) -> Configuration<ZERO_COPY_ALIGN_CHECK, LIMIT, LengthEncoding, ByteOrder, IntEncoding> {
generate()
}
pub const fn disable_preallocation_size_limit(
self,
) -> Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT_DISABLED,
LengthEncoding,
ByteOrder,
IntEncoding,
> {
generate()
}
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,
{
generate()
}
}
pub trait ConfigCore: 'static + Sized {
const PREALLOCATION_SIZE_LIMIT: Option<usize>;
const ZERO_COPY_ALIGN_CHECK: bool;
type ByteOrder: ByteOrder;
type IntEncoding: IntEncoding<Self::ByteOrder>;
}
impl<
const ZERO_COPY_ALIGN_CHECK: bool,
const PREALLOCATION_SIZE_LIMIT: usize,
LengthEncoding: 'static,
B,
I,
TagEncoding: 'static,
> ConfigCore
for Configuration<
ZERO_COPY_ALIGN_CHECK,
PREALLOCATION_SIZE_LIMIT,
LengthEncoding,
B,
I,
TagEncoding,
>
where
B: ByteOrder,
I: IntEncoding<B>,
{
const PREALLOCATION_SIZE_LIMIT: Option<usize> =
if PREALLOCATION_SIZE_LIMIT == PREALLOCATION_SIZE_LIMIT_DISABLED {
None
} else {
Some(PREALLOCATION_SIZE_LIMIT)
};
const ZERO_COPY_ALIGN_CHECK: bool = ZERO_COPY_ALIGN_CHECK;
type ByteOrder = B;
type IntEncoding = I;
}
pub trait Config: ConfigCore {
type LengthEncoding: SeqLen<Self> + 'static;
type TagEncoding: TagEncoding<Self> + 'static;
}
impl<
const ZERO_COPY_ALIGN_CHECK: bool,
const PREALLOCATION_SIZE_LIMIT: usize,
LengthEncoding: 'static,
B,
I,
T,
> Config for Configuration<ZERO_COPY_ALIGN_CHECK, PREALLOCATION_SIZE_LIMIT, LengthEncoding, B, I, T>
where
LengthEncoding: SeqLen<Self>,
T: TagEncoding<Self>,
B: ByteOrder,
I: IntEncoding<B>,
{
type LengthEncoding = LengthEncoding;
type TagEncoding = T;
}
mod serde;
pub use serde::*;