Skip to content

Experiment with changing Buffer::parts_mut to Buffer::as_mut_ptr. #1415

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

Conversation

sunfishcode
Copy link
Member

Based on an idea suggested here, experiment with changing from a parts_mut that returns (*mut T, usize) to a as_mut_ptr which returns a *mut [MaybeUninit<T>].

Based on an idea suggested [here], experiment with changing from a
`parts_mut` that returns `(*mut T, usize)` to a `as_mut_ptr` which
returns a `*mut [MaybeUninit<T>]`.

[here]: https://hachyderm.io/deck/@wffl@im-in.space/114145162248844972
@sunfishcode sunfishcode force-pushed the sunfishcode/buffer-types branch from 26ee6b9 to 1748541 Compare March 12, 2025 13:49
@sunfishcode
Copy link
Member Author

It appears pointer::len requires Rust 1.79.0. So we may not make this change in rustix for now, but we should still think about it for Buffer in general.

@joshtriplett

This comment was marked as resolved.

@sunfishcode

This comment was marked as resolved.

@sunfishcode sunfishcode added the semver bump Issues that will require a semver-incompatible fix label Mar 12, 2025
@joshtriplett

This comment was marked as resolved.

@sunfishcode
Copy link
Member Author

I like this idea in theory, but in practice, it ends up making the code more verbose and repetitive. I'm now prototyping Buffer type changes in a standalone repo, where it has simplified the code to have separate getters for the pointer and length, because it's sometimes useful to be able to get the length without having a &mut self. So I'll close this for now.

Another idea here is that the subtle problem above with &mut [MaybeUninit<T>] could be avoided if we could instead use &[MaybeUninitCell<T>], where MaybeUninitCell would be a wrapper around a MaybeUninit which has an interor-mutability way to store initialized values. That way, users with a shared reference could initialize but not deinitialize the elements. That would fix the subtlety above. However, in practice, that wouldn't perform the bookkeeping to track how many elements have been initialized, so the resulting API still wouldn't be very ergonomic.

@sunfishcode sunfishcode deleted the sunfishcode/buffer-types branch April 21, 2025 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver bump Issues that will require a semver-incompatible fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants