-
Notifications
You must be signed in to change notification settings - Fork 7
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
Make pinned-init usable with 1.82.0+ #24
Conversation
do you mind rebasing this one on top of main? thanks! |
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.
If I only enable the arc
feature (with default-features = false
), then it doesn't compile, this should fix it.
60eb861
to
0b1558a
Compare
bfe482d
to
bc78bd2
Compare
Thinking more about it, after the introduction of InPlaceWrite you actually need to check that you're not writing to a shared Arc. Thus |
You're correct. But having the function panic is not a good idea, since panics in the kernel are not an option. In that case, it probably makes more sense to just not implement |
As you prefer! The choice is:
It's not strictly necessary. There are cases in which get_mut_unchecked is useful even with refcount > 1, but pinned_init is only concerned with the case of refcount == 1 because it provides a safe API. So using get_mut_unchecked is only a small optimization. Again there are multiple choices:
In both cases, just tell me which of the possibilities you prefer and I'll implement it. :) |
I think we should remove the
Oh I have an idea, we can just use match Arc::get_mut(...) {
Ok(...) => ...
Err(...) => unsafe { hint::unreachable_unchecked() },
} That way we have the performance and don't rely on an unstable feature. |
I mean that future backports may have to account for the Arc implementation being in |
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.
some minor things, but otherwise looks pretty good.
87f96df
to
0251607
Compare
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.
I sadly can't comment on commit messages, in the first commit 876f228 ("lib: revert InPlaceWrite implementation for Arc<MaybeUninit<T>>
") I would prefer if you put Suggested-by: Benno Lossin <[email protected]>
, for kernel tags, I use that. Thanks!
Same of course for the next one.
1919cf5 ("lib: make "std" independent of allocator_api
") also needs its commit message updated, it still references the get_mut_unchecked
feature.
The kernel version of pinned_init implemented InPlaceWrite on UniqueArc, not Arc. This ensures that InPlaceWrite is not writing to a shared Arc. Userspace does not have this facility and therefore cannot lift the kernel implementation of InPlaceWrite directly into Arc<>. One possibility would be to use Arc::get_mut(), though this would introduce a panic in the case where the Arc is shared. So just revert part of commit 6841b61 ("rust: init: add `write_[pin_]init` functions", 2024-11-22). Suggested-by: Benno Lossin <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
Instead of using an unstable feature use unreachable_unchecked() to enable optimization. Suggested-by: Benno Lossin <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
The only dependency of the InPlaceInit trait on the allocator API is the AllocError type. Replace it with Infallible instead, i.e. allow any error as long as it has an "impl From<Infallible> for MyError" - which can have a trivial implementation as seen in examples/rror.rs. While admittedly of limited usefulness due to orphan rules, this is a first step towards allowing usage of pinned_init entirely without the allocator API, and therefore on stable Rust. Signed-off-by: Paolo Bonzini <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
Memory allocation (i.e. the Box and Arc) can be present even without the allocator_api. Allow feature = "std" when the code is simply checking for the existence of Box and Arc, and the allocator API is not used. Signed-off-by: Paolo Bonzini <[email protected]>
When compiling without allocator_api, assume that allocations cannot fail. This way, nightly Rust features are not absolutely needed for pinned_init, and it can be used with stable Rust; right now the minimum supported Rust version is 1.82.0, where the new_uninit version was stabilized. Signed-off-by: Paolo Bonzini <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
thanks again for all the help! |
This pull request, on top of #23, makes it possible to use
pinned_init
with stable Rust. In particular:new_uninit
has been stabilized (and anyway in the end it was just a glorified Box::<MaybeUninit>::new())allocator_api
is needed to have fallible allocation, but not if you just replacecore::alloc::AllocError
withcore::convert::Infallible
get_mut_unchecked
is not needed, because after the introduction ofInPlaceWrite
you actually need to check that you're not writing to a sharedArc
—thusArc::get_mut
is the right associated function to use.The main work is to isolate a little bit more the pieces of code that refer to
feature(allocator_api)
.Successful CI run at bonzini#2.
Diff on top of #23 here.