-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
Add from_box_bytes and box_bytes_of with BoxBytes type #211
Conversation
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.
The concept looks fine to me. The invariant should also mention that the bytes may not be uninitialized. The bound on the From
impl must be changed to be sound, and the bounds on box_bytes_of
/(try_)from_box_bytes
bounds should be relaxed.
Co-authored-by: zachs18 <8355914+zachs18@users.noreply.github.com>
I'm gonna rub the CI and let zachs take the lead on the reviewing for now. The basic idea seems reasonable |
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 a lot for the quick review! I guess the next step is to add tests if the change looks acceptable.
I've removed the fact that it fixes #167 from the description because it doesn't fix it in its generality. Only the |
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.
Looks good to me! (with from_raw_parts
)
I think these functions can be extended to allow [T]
later in a semver-compatible way (using a sealed trait bound), so I think it'd be fine to merge this as-is and add that functionality later.
Making these functions accept `[T]`
// Note: pub items in non-pub module cannot be named
// outside the crate unless re-exported.
// These traits are intentionally not re-exported publicly,
// as user implementations would be incorrect.
// See https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed
mod sealed {
use crate::{BoxBytes, PodCastError};
use alloc::boxed::Box;
pub trait BoxBytesOf {
fn box_bytes_of(self: Box<Self>) -> BoxBytes;
}
pub trait FromBoxBytes {
fn try_from_box_bytes(
bytes: BoxBytes,
) -> Result<Box<Self>, (PodCastError, BoxBytes)>;
}
}
impl<T: NoUninit> sealed::BoxBytesOf for T { ... }
impl<T: NoUninit> sealed::BoxBytesOf for [T] { ... }
impl<T: AnyBitPattern> sealed::FromBoxBytes for T { ... }
impl<T: AnyBitPattern> sealed::FromBoxBytes for [T] { ... }
pub fn box_bytes_of<T: sealed::BoxBytesOf>(input: Box<T>) -> BoxBytes {
input.box_bytes_of()
}
// etc
This would be semver-compatible (as a minor change) with the current defintions (it relaxes a trait bound), but would need some mention in the docs of what types are allowed, since the sealed-trait trick makes the generated docs slightly unclear.
I'm going to approve and merge this right now, but I'll hold off on publishing for a little bit. I'm not 100% convinced that the function names are all the best, given that this type is a little unusual, so I'll just think about it over the weekend. |
No description provided.