InlineableFactory implying Factory

This commit is contained in:
AF 2023-06-08 01:12:04 +00:00
parent 93782ba0a6
commit 5037ee3c84
4 changed files with 30 additions and 30 deletions

View File

@ -50,7 +50,7 @@ impl<ErrorA: Error, ErrorB: Error> Error for PairParseError<ErrorA, ErrorB> {}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, B: Mentionable<'a, Ctx>> StaticPair<'a, Ctx>
for Pair<A, B>
where
A::Fctr: InlineableFactory,
A::Fctr: InlineableFactory<'a, Ctx>,
{
type FactoryData = Pair<Self::FA, Self::FB>;
type A = A;

View File

@ -172,27 +172,27 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> ExtStack<'a, Ctx, A> for Sta
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> InlineableFactory
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> InlineableFactory<'a, Ctx>
for StackNodeFactory<'a, Ctx, A>
where
A::Fctr: InlineableFactory,
A::Fctr: InlineableFactory<'a, Ctx>,
{
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> FixedSizeFactory
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> FixedSizeFactory<'a, Ctx>
for StackNodeFactory<'a, Ctx, A>
where
A::Fctr: FixedSizeFactory,
A::Fctr: FixedSizeFactory<'a, Ctx>,
{
fn size(&self) -> usize {
Stack::<'a, Ctx, A>::SIZE + self.element_factory.size()
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> ConstSizeFactory
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> ConstSizeFactory<'a, Ctx>
for StackNodeFactory<'a, Ctx, A>
where
A::Fctr: ConstSizeFactory,
A::Fctr: ConstSizeFactory<'a, Ctx>,
{
const SIZE: usize = Stack::<'a, Ctx, A>::SIZE + A::Fctr::SIZE;
}

View File

@ -11,16 +11,16 @@ use super::{
};
/// This factory should return an error on EOF.
pub trait InlineableFactory {}
pub trait InlineableFactory<'a, Ctx: Context<'a>>: Factory<'a, Ctx> {}
/// This factory always reads the same amount of bytes or returns error.
pub trait FixedSizeFactory: InlineableFactory {
pub trait FixedSizeFactory<'a, Ctx: Context<'a>>: InlineableFactory<'a, Ctx> {
/// For [`ConstSizeFactory`] this must return [`ConstSizeFactory::SIZE`].
fn size(&self) -> usize;
}
/// Compile-time analogue of [`FixedSizeFactory`].
pub trait ConstSizeFactory: FixedSizeFactory {
pub trait ConstSizeFactory<'a, Ctx: Context<'a>>: FixedSizeFactory<'a, Ctx> {
/// Must be equal to [`FixedSizeFactory::size()`].
const SIZE: usize;
}
@ -54,26 +54,26 @@ pub trait ConstSizeAtomic: InlineableAtomic {
impl<A: ConstSizeAtomic> InlineableAtomic for A {}
impl<A: InlineableAtomic> InlineableFactory for AtomicFactory<A> {}
impl<'a, Ctx: Context<'a>, A: InlineableAtomic> InlineableFactory<'a, Ctx> for AtomicFactory<A> {}
impl<A: ConstSizeAtomic> FixedSizeFactory for AtomicFactory<A> {
impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic> FixedSizeFactory<'a, Ctx> for AtomicFactory<A> {
fn size(&self) -> usize {
A::SIZE
}
}
impl<A: ConstSizeAtomic> ConstSizeFactory for AtomicFactory<A> {
impl<'a, Ctx: Context<'a>, A: ConstSizeAtomic> ConstSizeFactory<'a, Ctx> for AtomicFactory<A> {
const SIZE: usize = A::SIZE;
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> InlineableObject<'a, Ctx> for A where
A::Fctr: InlineableFactory
A::Fctr: InlineableFactory<'a, Ctx>
{
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> FixedSizeObject<'a, Ctx> for A
where
A::Fctr: FixedSizeFactory,
A::Fctr: FixedSizeFactory<'a, Ctx>,
{
fn size(&self) -> usize {
self.factory().size()
@ -82,7 +82,7 @@ where
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> ConstSizeObject<'a, Ctx> for A
where
A::Fctr: ConstSizeFactory,
A::Fctr: ConstSizeFactory<'a, Ctx>,
{
const SIZE: usize = A::Fctr::SIZE;
}
@ -134,7 +134,7 @@ pub type CheckedParseResult<'a, Ctx, F> =
Result<Mtbl<'a, Ctx, F>, CheckedParseError<ParseError<'a, Ctx, F>>>;
/// Extension trait for factories that ensures fixed size read.
pub trait CheckedParse<'a, Ctx: Context<'a>>: FixedSizeFactory + Factory<'a, Ctx> {
pub trait CheckedParse<'a, Ctx: Context<'a>>: FixedSizeFactory<'a, Ctx> {
/// Verify proper read length using [`Deserializer::tell`].
fn deserialize_checked(
&self,
@ -150,7 +150,7 @@ pub trait CheckedSerialize<'a, Ctx: Context<'a>>: Serializable + FixedSizeObject
fn serialize_checked(&self, serializer: &mut dyn Serializer) -> Result<(), SizeError>;
}
impl<'a, Ctx: Context<'a>, F: FixedSizeFactory + Factory<'a, Ctx>> CheckedParse<'a, Ctx> for F {
impl<'a, Ctx: Context<'a>, F: FixedSizeFactory<'a, Ctx>> CheckedParse<'a, Ctx> for F {
fn deserialize_checked(
&self,
deserializer: &mut dyn Deserializer,
@ -200,9 +200,9 @@ pub trait AlwaysFixedSize {
fn _size(&self) -> usize;
}
impl<F: AlwaysFixedSize> InlineableFactory for F {}
impl<'a, Ctx: Context<'a>, F: AlwaysFixedSize + Factory<'a, Ctx>> InlineableFactory<'a, Ctx> for F {}
impl<F: AlwaysFixedSize> FixedSizeFactory for F {
impl<'a, Ctx: Context<'a>, F: AlwaysFixedSize + Factory<'a, Ctx>> FixedSizeFactory<'a, Ctx> for F {
fn size(&self) -> usize {
self._size()
}
@ -219,7 +219,7 @@ impl<F: AlwaysConstSize> AlwaysFixedSize for F {
}
}
impl<F: AlwaysConstSize> ConstSizeFactory for F {
impl<'a, Ctx: Context<'a>, F: AlwaysConstSize + Factory<'a, Ctx>> ConstSizeFactory<'a, Ctx> for F {
const SIZE: usize = Self::_SIZE;
}

View File

@ -37,7 +37,7 @@ pub trait StaticPair<'a, Ctx: Context<'a>>:
/// Second element's type. Must equal [`StaticPairSerializable::SB`].
type B: Mentionable<'a, Ctx, Fctr = Self::FB>;
/// First element's factory.
type FA: Factory<'a, Ctx, Mtbl = Self::A> + InlineableFactory;
type FA: Factory<'a, Ctx, Mtbl = Self::A> + InlineableFactory<'a, Ctx>;
/// Second element's factory.
type FB: Factory<'a, Ctx, Mtbl = Self::B>;
/// See [Factory::ParseError].
@ -156,18 +156,18 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> Factory<'a, Ctx>
}
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> InlineableFactory
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> InlineableFactory<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP>
where
SP::FB: InlineableFactory,
SP::FB: InlineableFactory<'a, Ctx>,
{
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FixedSizeFactory
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> FixedSizeFactory<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP>
where
SP::FA: FixedSizeFactory,
SP::FB: FixedSizeFactory,
SP::FA: FixedSizeFactory<'a, Ctx>,
SP::FB: FixedSizeFactory<'a, Ctx>,
{
fn size(&self) -> usize {
let (fa, fb) = SP::factories(&self.factory_data);
@ -175,11 +175,11 @@ where
}
}
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> ConstSizeFactory
impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> ConstSizeFactory<'a, Ctx>
for StaticPairFactory<'a, Ctx, SP>
where
SP::FA: ConstSizeFactory,
SP::FB: ConstSizeFactory,
SP::FA: ConstSizeFactory<'a, Ctx>,
SP::FB: ConstSizeFactory<'a, Ctx>,
{
const SIZE: usize = SP::FA::SIZE + SP::FB::SIZE;
}