stabilize `Box::take`
Tracking issue: rust-lang/rust#147212
There hasn't been any activity on the tracking issue for a while, and this is a pretty small feature, so opening this to hopefully get a FCP started.
Closes: rust-lang/rust#147212
Add useful APIs to `Unique(Arc|Rc)`
Some of these we already had private methods for (only used for `(try_)map`), some of these (or lack thereof) (namely the `try_new` apis) prevented me from using `UniqueArc` in other parts of std.
cc rust-lang/rust#112566
Re-export `core::fmt::NumBuffer` in `alloc` (and `std`)
We just stabilized `core::fmt::NumBuffer` in Rust 1.98, but it seems to be a simple oversight that this was not also re-exported in `alloc::fmt` (and `std` just re-exports that whole module). Let's correct that.
I'm marking this stable right away, although I don't think the later `since` version will be properly rendered in docs for a re-export. I'm not sure it's worth trying to clarify that in freeform documentation.
Generalise (most) impls on `Box`
Resolves the trait impl concern for rust-lang/rust#156882; `Default` wasn't touched since changing that would be breaking, but hopefully all of this should be fine - I'll do a crater run to be sure. Certain impls that were for one reason or another Annoying to generalise (e.g. `Clone for Box<Path>`) were also untouched since I believe we can add those in the future, as they're on concrete types.
r? clarfonthey
alloc: a bunch of safety comments
Following up from rust-lang/rust#160941. Triaging this is what found the errors in rust-lang/rust#162285 & rust-lang/rust#162286. More to come, but I didn't want to make the review effort too high on any single PR.
r? libs
string: don't unwind prematurely
`splice` on a vec can technically unwind (since we still say `handle_alloc_error` *may* unwind in the future, and nightly users could trigger this via setting the alloc error hook) which would expose a string in an invalid state during `replace_range`. This adds an abort guard around it to ensure this is never triggered.
r? libs
box: fixup map/try_map deallocate calls
`Box::{map, try_map}` unsoundly called `deallocate` on the previous box's pointer, even if it was a ZST. This was probably okay for `Global` but it's definitely not for arbitrary custom allocators.
r? libs
Add custom allocator support to `(try_)map` on `UniqueArc` and `UniqueRc`
Follow-up to rust-lang/rust#161617.
This one required me to add allocator support to a lot more methods.
Probably good for them to have it for future additions ^^
The old, allocator-free methods went unused from this change and I had to remove them because we don't allow warnings (they are trivial to implement on top of the allocator ones). This had the side effect of making the diff a bit weird. Sorry!
r? nia-e
Revert "Use `drop_guard` in some places in {core,alloc,std}"
cc @panstromek @GrigorenkoPV
Reverts this PR due to perf issues, so that a fix can be verified to not affect perf. Apologies for not initially running perf; kinda tunnel-visioned "this just does the same thing" without considering the changes from non-generic functions to generic closures.
`alloc` crate: shrink undocumented `unsafe` blocks
Reduce the scope of `unsafe` blocks that are not yet documented by moving safe operations out of the blocks, making it easier to add the missing documentation in the future.
Change some `Infallible` to `!` in std
I believe this desirable since we recommend `!` over `Infallible`, so std follows its own advice. Most changes are to unstable features, but not all. Similar changes in compiler have to wait for a bootstrap update.
Reduce the scope of `unsafe` blocks that are not yet documented by moving safe
operations out of the blocks, making it easier to add the missing documentation
in the future.
Use better generic type parameter names for `Extend` and `FromIterator`
Change
```rs
pub trait Extend<A> {
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T);
…
}
```
to
```rs
pub trait Extend<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I);
…
}
```
to be more consistent with common use of `T` as the item type in containers, and their `Extend` impls; and use `I` as the name for the `IntoIterator` type.
also do the basically the same thing for `FromIterator<A>`→`FromIterator<T>` as well
cases of these parameters in trait impls for the abovementioned traits.
These changes obviously don't change/break behavior of the traits and its users, but the improve the way the documentation of the traits renders (the improvement is the more sensible and more consistent names for the parameters). The only downside I'm aware of (besides that this is touching quite a few files..) is that this does affect some link anchor names in *some* cases. E.g.
```diff
- impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
+ impl<T, V: FromIterator<T>> FromIterator<Option<T>> for Option<V> {
```
would mean that [doc.rust-lang.org/core/iter/trait.FromIterator.html#impl-FromIterator<Option\<A>>-for-Option\<V>](https://doc.rust-lang.org/core/iter/trait.FromIterator.html#impl-FromIterator%3COption%3CA%3E%3E-for-Option%3CV%3E) no longer works, as it changes `…#impl-FromIterator<Option<A>>-for-Option<V>` to `…#impl-FromIterator<Option<T>>-for-Option<V>`
Abort instead of unwinding out of an inconsistent BTreeMap::split_off
Fixesrust-lang/rust#158165. Supersedes rust-lang/rust#161784, which I had to abandon after a bad force-push from a shallow clone left its head detached.
`BTreeMap::split_off` runs the caller's `Ord`/`Borrow` impl via `search_node` inside `Root::split_off`'s descent. After the first `move_suffix`, the two roots alias the same values through structurally invalid trees until the borders are fixed; a comparator panic there unwinds with a stale length and double-frees on later iteration or drop. It is reachable from `#![forbid(unsafe_code)]` on stable (reproducer on the issue).
Guard the descent loop with an abort-on-panic `PanicGuard`, as `btree::mem::replace` already does. `catch_unwind` isnt available in `alloc` (no_std), and I kept an inline guard rather than the unstable `DropGuard`. It is armed once before the loop; the first `search_node` stays outside it, since a panic there can still unwind safely (nothing has moved yet).
Verified with Miri: the reproducer goes from a double-free to a clean abort, and normal multi-level splits are unaffected. Adds a happy-path regression test.
Credit to @ostrowr for the report and to rust-lang/rust#158710 for the original approach.
r? @nia-e
After the first move_suffix in Root::split_off, the source and result
trees share values through two temporarily invalid structures. If a
later key comparison panics, unwinding leaves the map with a stale
length over a partially detached tree, and consuming iteration then
double-frees the shared values, reachable from safe code.
Guard the descent loop with a mem::DropGuard that aborts on unwind,
armed before the first move_suffix and dismissed once the borders are
fixed.