tuffy / bitstream-io

Library for reading/writing un-aligned values from/to streams in big-endian and little-endian formats
Apache License 2.0
52 stars 20 forks source link

{To,From}{Bit,Byte}StreamWith: allow lifetime-constrained Contexts #16

Closed fengalin closed 10 months ago

fengalin commented 10 months ago

Warning: this proposal introduces a breaking change. The change is as trivial as adding <'_>, but all implementers of {To,From}{Bit,Byte}StreamWith would be affected. IMO this would warrant a major version bump.

See the examples for a complete use case.

In current implementation, {To,From}{Bit,Byte}StreamWith impose that the Context be static. In some cases, we want to pass a Context which owns a reference to another struct.

This MR adds a lifetime to these traits to allow using a Context constrained by a lifetime. Ex.:

impl FromBitStreamWith<'a> for FrameHeader {
    type Context = StreamInfo<'a>;
    [...]
}

Implementations for which the Context is 'static bound can use:

impl FromBitStreamWith<'_> for FrameHeader {
    type Context = StreamInfo;
    [...]
}
fengalin commented 10 months ago

I just realized this should also be applied to To{Bit,Byte}StreamWith. I'll push an update tomorrow.

fengalin commented 10 months ago

It's ready now.

fengalin commented 10 months ago

Thank you for your reactivity @tuffy!

Since this requires a major version bump, I understand it might take some times before the next release. Do you have an idea when that could occur though? Just asking so I can schedule some downstream modifications accordingly.

fengalin commented 10 months ago

Thanks for the release @tuffy!