-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 fallible Box, Arc, and Rc allocator APIs #80310
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
8a6e339
to
8ee2191
Compare
/// ``` | ||
#[unstable(feature = "allocator_api", issue = "32838")] | ||
// #[unstable(feature = "new_uninit", issue = "63291")] | ||
pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> { |
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.
Unclear if I should also be adding these
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 have no objections
// #[unstable(feature = "new_uninit", issue = "63291")] | ||
pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> { | ||
let layout = Layout::new::<mem::MaybeUninit<T>>(); | ||
let ptr = alloc.allocate(layout)?.cast(); |
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 did not update new_uninit_in
to be try_new_uninit_in().unwrap_or_else()
, but can if folks prefer. I wasn't sure if it would have performance issues.
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.
As the current implementation of new_uninit_in
already does an unwrap_or_else
on failed allocations, I think it'd result in the same performance. If you want, you can still do a perf CI run to make sure though?
The job Click to see the possible cause of the failure (guessed by this bot)
|
8ee2191
to
2f2e9c9
Compare
Drive by comment since I'm looking at making similar changes for Rc/Arc. It seems like a |
No, I'm considering adding those actually, I just wasn't sure what the libs team would want. |
2f2e9c9
to
2c1111f
Compare
I've added APIs for Arc and Rc. I have only modified |
2c1111f
to
bb15fa1
Compare
@kennytm I've made further changes, if you'd like to review. I think this is ready to land if the @rust-lang/libs is okay with this going through without an RFC (it is feature gated, and @SimonSapin indicated an RFC is probably not necessary so probably no big deal). |
/// | ||
/// The function `mem_to_rcbox` is called with the data pointer | ||
/// and must return back a (potentially fat)-pointer for the `RcBox<T>`. | ||
unsafe fn try_allocate_for_layout( |
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.
could you make allocate_for_layout
reuse try_allocate_for_layout(...).unwrap_or_else(...)
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.
Done!
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.
oh ok. too bad the layout still needs to be recomputed.
@bors r+ |
📌 Commit 8f3cb7d has been approved by |
/// | ||
/// [zeroed]: mem::MaybeUninit::zeroed | ||
#[unstable(feature = "allocator_api", issue = "32838")] | ||
// #[unstable(feature = "new_uninit", issue = "63291")] |
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.
This should be #[inline]
/// # Ok::<(), std::alloc::AllocError>(()) | ||
/// ``` | ||
#[unstable(feature = "allocator_api", issue = "32838")] | ||
// #[unstable(feature = "new_uninit", issue = "63291")] |
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.
This should be #[inline]
library/alloc/src/boxed.rs
Outdated
/// ``` | ||
/// #![feature(allocator_api, new_uninit)] | ||
/// | ||
/// |
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.
/// |
library/alloc/src/sync.rs
Outdated
/// # Ok::<(), std::alloc::AllocError>(()) | ||
/// ``` | ||
/// | ||
/// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed |
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.
/// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed | |
/// [zeroed]: mem::MaybeUninit::zeroed |
⌛ Trying commit 375e7c5 with merge 233a249a8282ce5b42fc70c2cce6537e0743dbf7... |
☀️ Try build successful - checks-actions |
Queued 233a249a8282ce5b42fc70c2cce6537e0743dbf7 with parent 44e3daf, future comparison URL. @rustbot label: +S-waiting-on-perf |
Finished benchmarking try commit (233a249a8282ce5b42fc70c2cce6537e0743dbf7): comparison url. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up. @bors rollup=never |
@bors rollup- r=kennytm |
📌 Commit 375e7c5 has been approved by |
☀️ Test successful - checks-actions |
Marking as |
Removing |
@jonas-schievink ah, i wish there were a way to flag that something should be included in the blog post |
I don't recall us every mentioning unstable feature in the release blog post. You could write a dedicated blog post for this feature though! |
Hmm, good point. It seems minor enough that I probably won't bother, though. |
fix incorrect Box::from_raw_in doctest Now that Miri can run doctests, I ran it on liballoc, and found exactly one problem: this test creates a `Box<u8>` to deallocate a 4-byte allocation! Introduced by rust-lang#80310 so r? `@Manishearth` `@kennytm`
cc #48043
It was suggested in #48043 (comment) that
Box::try_*
follows the spirit of RFC 2116. This PR is an attempt to add the relevant APIs, tied to the same feature gate. Happy to make any changes or turn this into an RFC if necessary.cc @rust-lang/wg-allocators