-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let PacketWriter::write_packet use buffers convertible to Cow #25
Conversation
As pointed out in issue #19, in some cases it may not be desirable to heap allocate Ogg packet contents, but the current parameters of the `write_packet` method impose a heap allocation no matter what. However, the `PacketWriter` struct doesn't actually need to own the packet data, as long as it stays in memory unmodified during its lifetime. The `Cow` smart pointer that the standard library provides captures this requirement more precisely, and provides for more elegant, flexible and potentially efficient client code. Therefore, substitute `Box` for `Cow` in the aforementioned code. Boxed slices can still be used with this new design, although with a different syntax, which breaks backwards compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I wonder if it's possible to have something more general than Cow
. Maybe AsRef<[u8]>
or something?
This sounds like a good idea, thanks! However, after swapping the generic On the other hand, with the current approach, the caller can just convert whatever type that implements Another advantage I can see from using a |
Newer Rust versions work introduced additional lifetime inference rules that made this code work, but they are not available in the MSRV this library targets.
Good point about the need for a generic param. The PR should be good now but I won't merge it for now because it is a breaking change of the API. I want to make a new release of the ogg crate once the 2021 edition lands though. |
That's understandable, thanks. However, right now it's bugging me a bit that the CI build for Rust 1.27.2 does not pass, as I've developed this PR using the latest Rust version and used some syntax that simply didn't compile back then. Do you plan on bumping MSRV on the next release? Should I continue to fix compilation with this older Rust version? |
@AlexTMjugador no, the MSRV will be increased to 1.56.0. |
This reverts commit 01ee00e. Fixing this build error is no longer necessary because MSRV will be bumped to the current stable version, which accepts this shorter syntax.
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
As pointed out in issue #19, in some cases it may not be desirable to heap allocate Ogg packet contents, but the current parameters of the
write_packet
method impose a heap allocation no matter what.However, the
PacketWriter
struct doesn't actually need to own the packet data, as long as it stays in memory unmodified during its lifetime. TheCow
smart pointer that the standard library provides captures this requirement more precisely, and provides for more elegant, flexible and potentially efficient client code.Therefore, substitute
Box
forCow
in the aforementioned code. Boxed slices can still be used with this new design, although with a different syntax, which breaks backwards compatibility.