Skip to content
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

Merged
merged 5 commits into from
Oct 20, 2023

Conversation

ia0
Copy link
Contributor

@ia0 ia0 commented Oct 12, 2023

No description provided.

Copy link
Contributor

@zachs18 zachs18 left a 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>
@Lokathor
Copy link
Owner

I'm gonna rub the CI and let zachs take the lead on the reviewing for now. The basic idea seems reasonable

Copy link
Contributor Author

@ia0 ia0 left a 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.

@ia0 ia0 marked this pull request as ready for review October 12, 2023 18:07
@ia0
Copy link
Contributor Author

ia0 commented Oct 12, 2023

I've removed the fact that it fixes #167 from the description because it doesn't fix it in its generality. Only the T: Sized case.

Copy link
Contributor

@zachs18 zachs18 left a 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.

@Lokathor Lokathor added the semver-minor semver minor change label Oct 12, 2023
@Lokathor Lokathor linked an issue Oct 12, 2023 that may be closed by this pull request
@Lokathor Lokathor merged commit fd27a5b into Lokathor:main Oct 20, 2023
@Lokathor
Copy link
Owner

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.

@ia0 ia0 deleted the box-bytes branch October 20, 2023 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor semver minor change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Getting allocated bytes (Box/Rc/Arc) out of Box<T>
3 participants