Skip to content

core: add {Imm,M}utablePrimitiveSlice trait #19241

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

Closed
wants to merge 1 commit into from
Closed

core: add {Imm,M}utablePrimitiveSlice trait #19241

wants to merge 1 commit into from

Conversation

emberian
Copy link
Member

Lets eligible slices be converted to a slice of bytes. Abstracts out some
sensible unsafe behavior from rust-image.

@rust-highfive
Copy link
Contributor

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@emberian
Copy link
Member Author

r? @aturon

Lets eligible slices be converted to a slice of bytes. Abstracts out some
sensible unsafe behavior from `rust-image`.
@Gankra
Copy link
Contributor

Gankra commented Nov 23, 2014

CC @alexcrichton on if we want to take the opportunity to use the new raw tooling to do explicit lifetime management here.


/// This uses `Primitive` instead of `Copy` because otherwise one would be
/// able to write invalid enum discriminants or bools, which is unsafe.
impl<T: Primitive> MutablePrimitiveSlice for [T] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unsafe, because Primitive is just a trait, and can be implemented for new types (e.g. new enums that you create). Also note that Primitive has been deprecated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same above I believe as well)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also exposing the underlying endian-ness of the system (not portable), which may want to at least be mentioned in the documentation if we move forward with this.

@aturon
Copy link
Member

aturon commented Nov 23, 2014

Unfortunately, I doubt that there is a way to make the mutable part here safe while providing a blanket impl (because these are open-ended).

In general, before we bring this into the standard library, it would be helpful to have more information about the use cases (and better yet, some evidence that many clients would benefit). This is something that can easily be provided in an external library, so it should only be brought into core if it would be very widely used.

@emberian
Copy link
Member Author

@aturon Ah, yes, I forgot about extension impls. Quite unfortunate, really. It'd be nice to be able to represent a closed set of types. (Closed in the extension meaning, not topological).

@emberian
Copy link
Member Author

@aturon The use cases in rust-image are storing a Vec<P> where P: Pixel<T>, T: Primitive, and then accessing that vector as bytes for pixel-component operations. When sending arrays across the network, it'd also be useful to have this for primitive arrays, without having to do a loop and .write_be_u16, for example (if you need to do an endian swap, you can do it on the entire buffer before hand, though a buffered reader might suffice), though this is quite niche given general purpose, high-speed serialization libraries, and not hand-written hacky things.

I've begun a casual audit of unsafe code used by various libraries, and this is a core piece of rust-image, and I have a feeling this pattern is widely applicable to applications which need to work with representations.

@emberian emberian closed this Nov 24, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Mar 3, 2025
Fix sysroot crate-graph construction not mapping crate-ids for proc-macros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants