Commit Graph

2470 Commits

Author SHA1 Message Date
Stein Somers bdbe56ecb8 BTreeMap first/last: simplify implementations 2020-04-06 19:00:43 +02:00
IgorPerikov 9fc77c0e15 add detailed panic messages for Vec functions 2020-04-06 17:53:56 +03:00
Stein Somers 6ee7e8c978 Remove the Ord bound that was plaguing drain_filter, and superfluous lifetimes 2020-04-06 15:45:19 +02:00
Dylan DPC 2ec0e148f1
Rollup merge of #70795 - Amanieu:btree_remove_tracking, r=Mark-Simulacrum
Keep track of position when deleting from a BTreeMap

This improves the performance of drain_filter and is needed for future Cursor support for BTreeMap.

cc @ssomers
r? @Mark-Simulacrum
2020-04-05 18:47:47 +02:00
Dylan DPC c2595539e7
Rollup merge of #70777 - faern:use-assoc-int-consts2, r=dtolnay
Don't import integer and float modules, use assoc consts

Stop importing the standard library integer and float modules to reach the `MIN`, `MAX` and other constants. They are available directly on the primitive types now.

This PR is a follow up of #69860 which made sure we use the new constants in documentation.

This type of change touches a lot of files, and previously all my assoc int consts PRs had collisions and were accepted only after a long delay. So I'd prefer to do it in smaller steps now. Just removing these imports seem like a good next step.

r? @dtolnay
2020-04-05 18:47:45 +02:00
Amanieu d'Antras 51357cf1cd Apply review feedback 2020-04-05 14:29:00 +01:00
Amanieu d'Antras 7365748c0c Keep track of position when deleting from a BTreeMap
This improves the performance of drain_filter and is needed for
future Cursor support for BTreeMap.
2020-04-05 12:18:46 +01:00
Dylan DPC c185c4fe47
Rollup merge of #70776 - RalfJung:raw-vec, r=Dylan-DPC,TimDiekmann
clarify comment in RawVec::into_box

On first reading I almost thought "len <= cap" would be all that there is to check here. Expand the comment to clarify that that is not the case.
2020-04-05 13:13:13 +02:00
Dylan DPC 7e4ed72d64
Rollup merge of #70558 - RalfJung:vec-extend-aliasing, r=Amanieu
Fix some aliasing issues in Vec

`Vec::extend` and `Vec::truncate` invalidated references into the vector even without reallocation, because they (implicitly) created a mutable reference covering the *entire* initialized part of the vector.

Fixes https://github.com/rust-lang/rust/issues/70301
I verified the fix by adding some new tests here that I ran in Miri.
2020-04-05 13:13:08 +02:00
Ralf Jung 6cbe1726a7 clarify safety in RawVec::into_box 2020-04-05 12:09:03 +02:00
Linus Färnstrand fff4f08398 Stop importing integer modules in liballoc 2020-04-05 11:22:01 +02:00
Ralf Jung 7e81c11aa8 tweak swap_remove 2020-04-05 08:40:40 +02:00
Trevor Spiteri 2b718e8d9c use ManuallyDrop instead of forget inside collections
This commit changes some usage of mem::forget into mem::ManuallyDrop
in some Vec, VecDeque, BTreeMap and Box methods.

Before the commit, the generated IR for some of the methods was
longer, and even after optimization, some unwinding artifacts were
still present.
2020-04-04 14:30:33 +02:00
Mazdak Farrokhzad 9b22fdc121
Rollup merge of #69860 - faern:use-assoc-int-consts, r=dtolnay
Use associated numeric consts in documentation

Now when the associated constants on int/float types are stabilized and the recommended way of accessing said constants (#68952). We can start using it in this repository, and recommend it via documentation example code.

This PR is the reincarnation of #67913 minus the actual adding + stabilization of said constants. (EDIT: Now it's only changing the documentation. So users will see the new consts, but we don't yet update the internal code)

Because of how fast bit rot happens to PRs that touch this many files, it does not try to replace 100% of the old usage of the constants in the entire repo, but a good chunk of them.
2020-04-03 22:55:02 +02:00
Yoshua Wuyts 3d17993f9f Fix link in task::Wake docs 2020-04-03 11:33:27 +02:00
Linus Färnstrand c0ec0a27b3 Replace max/min_value() with MAX/MIN assoc consts 2020-04-03 09:33:10 +02:00
DutchGhost 3f1a588a94 stabilize BTreeMap::remove_entry 2020-04-02 21:35:26 +02:00
bors 127a11a344 Auto merge of #70362 - TimDiekmann:alloc-overhaul, r=Amanieu
Overhaul of the `AllocRef` trait to match allocator-wg's latest consens; Take 2

GitHub won't let me reopen #69889 so I make a new PR.

In addition to #69889 this fixes the unsoundness of `RawVec::into_box` when using allocators supporting overallocating. Also it uses `MemoryBlock` in `AllocRef` to unify `_in_place` methods by passing `&mut MemoryBlock`. Additionally, `RawVec` now checks for `size_of::<T>()` again and ignore every ZST. The internal capacity of `RawVec` isn't used by ZSTs anymore, as `into_box` now requires a length to be specified.

r? @Amanieu

fixes rust-lang/wg-allocators#38
fixes rust-lang/wg-allocators#41
fixes rust-lang/wg-allocators#44
fixes rust-lang/wg-allocators#51
2020-04-02 06:08:35 +00:00
Tim Diekmann 89ed59d884
Add missing allocation guard in `RawVec::grow` 2020-04-01 10:26:30 +02:00
Dylan DPC 8310320ebd
Rollup merge of #70632 - tspiteri:vec-new, r=sfackler
expand vec![] to Vec::new()

The current expansion of `vec![]` calls `into_vec` on a boxed slice, which results in longer IR, and even after optimization, some unwinding artifacts are still present in the IR. This PR uses `Vec::new()` for `vec![]`.

This also allows `vec![]` to be used in const expressions.
2020-04-01 00:27:26 +02:00
Dylan DPC 718ba0d23b
Rollup merge of #68770 - ssomers:btree_drain_filter, r=Amanieu
BTreeMap/BTreeSet: implement drain_filter

Provide an implementation of drain_filter for BTreeMap and BTreeSet. Should be optimal when the predicate picks only elements in leaf nodes with at least MIN_LEN remaining elements, which is a common case, at least when draining only a fraction of the map/set, and also when the predicate picks elements stored in internal nodes where the right subtree can easily let go of a replacement element.

The first commit adds benchmarks with an external, naive implementation. to compare how much this claimed optimality-in-some-cases is actually worth.
2020-04-01 00:27:18 +02:00
Trevor Spiteri 4d8273dea5 expand vec![] to Vec::new() 2020-03-31 21:37:13 +02:00
Dylan DPC f62cfa76c5
Rollup merge of #69425 - lcnr:make_contiguous, r=Amanieu
add fn make_contiguous to VecDeque

Adds the following method to VecDeque:

```rust
pub fn make_contiguous(&mut self) -> &mut [T];
```

Taken from https://github.com/rust-lang/rust/pull/69400, after a suggestion by @CryZe https://github.com/rust-lang/rust/pull/69400#issuecomment-590216089

I am in favor of merging this instead of https://github.com/rust-lang/rust/pull/69400.
2020-03-31 19:29:26 +02:00
Bastian Kauschke e1afd26c52 fix docs 2020-03-31 16:50:06 +02:00
Mazdak Farrokhzad 9ee373fd94
Rollup merge of #69784 - benesch:fast-strip-prefix-suffix, r=kennytm
Optimize strip_prefix and strip_suffix with str patterns

As mentioned in https://github.com/rust-lang/rust/issues/67302#issuecomment-585639226.
I'm not sure whether adding these methods to `Pattern` is desirable—but they have default implementations so the change is backwards compatible. Plus it seems like they're slated for wholesale replacement soon anyway? #56345

----

Constructing a Searcher in strip_prefix and strip_suffix is
unnecessarily slow when the pattern is a fixed-length string. Add
strip_prefix and strip_suffix methods to the Pattern trait, and add
optimized implementations of these methods in the str implementation.
The old implementation is retained as the default for these methods.
2020-03-31 15:59:40 +02:00
Bastian Kauschke b5223d2725 update `VecDeque::as_(mut)_slice` docs 2020-03-31 15:39:56 +02:00
Nikhil Benesch ac478f2f61
Optimize strip_prefix and strip_suffix with str patterns
Constructing a Searcher in strip_prefix and strip_suffix is
unnecessarily slow when the pattern is a fixed-length string. Add
strip_prefix and strip_suffix methods to the Pattern trait, and add
optimized implementations of these methods in the str implementation.
The old implementation is retained as the default for these methods.
2020-03-30 11:10:21 -04:00
John Kåre Alsaker 87cdfb6e71 Add inline attributes for functions used in the query system 2020-03-30 14:37:44 +02:00
Ralf Jung 5bbaac357d fix and test aliasing in swap_remove 2020-03-30 13:34:03 +02:00
Ralf Jung 8f479e362f fix aliasing in remove()
also add smoke test to detect relocation even in rustc runs
2020-03-30 13:24:55 +02:00
Ralf Jung 4eacf45c9c also cover next() path of draining iterators 2020-03-30 13:01:15 +02:00
Ralf Jung 3411ade32e test more mutating vector methods 2020-03-30 12:24:02 +02:00
Ralf Jung 032d3cd553 fix BTreeMap test compilation with Miri 2020-03-30 12:04:05 +02:00
Ralf Jung 4393923168 add some tests 2020-03-30 11:58:16 +02:00
Ralf Jung fa6c883074 fix ptr invalidation in Vec::truncate 2020-03-30 11:58:16 +02:00
Ralf Jung 86c1c43420 fix pointer invalidation when extnding a vector from an untrusted iterator 2020-03-30 11:58:16 +02:00
Ralf Jung 6556549fa6 fix Vec::extend invalidating unrelated pointers 2020-03-30 11:58:16 +02:00
Stein Somers 0405db3a34 BTreeMap/BTreeSet: implement and test drain_filter 2020-03-29 16:05:53 +02:00
Stein Somers a6cae3d5cf Add benchmarks of drain_filter-like behaviour 2020-03-29 16:05:53 +02:00
Mazdak Farrokhzad f31e56309a
Rollup merge of #70506 - ssomers:btreemap_testing_consts, r=RalfJung
BTreeMap testing: introduce symbolic constants and use height consistently

Doesn't change what or how much is tested, except for some exact integer types, just for convenience and because `node::CAPACITY` is a usize.

r? @RalfJung
2020-03-29 11:50:13 +02:00
Mazdak Farrokhzad c51fcb5f38
Rollup merge of #68692 - jyn514:vec-from-array, r=LukasKalbertodt
impl From<[T; N]> for Vec<T>

Closes https://github.com/rust-lang/rust/issues/67963
2020-03-29 11:50:10 +02:00
Tim Diekmann 3ade8ae660 Implement `init` and `init_offset` on `AllocInit` and mark it unsafe 2020-03-29 01:47:05 +01:00
Stein Somers e92d740b35 BTreeMap testing: introduce symbolic constants and refer to height consistently. 2020-03-28 23:30:43 +01:00
Tim Diekmann bf6a46db31 Make fields in `MemoryBlock` public 2020-03-28 20:22:07 +01:00
Tim Diekmann 03b055b0b4 Remove alignment from `MemoryBlock` 2020-03-26 17:14:12 +01:00
Tim Diekmann fed3d6e646 Fix safety section of `RawVec::into_box` 2020-03-26 17:13:47 +01:00
Tim Diekmann cbbdca0594 Fix wording in `RawVec::from_raw_parts(_in)` 2020-03-26 17:13:31 +01:00
Tim Diekmann b02e53f197 Remove check for ZST in `RawVec::needs_to_grow` 2020-03-26 17:13:25 +01:00
Tim Diekmann ad7de67a32 Refine docs for `RawVec::from_raw_parts(_in)` 2020-03-26 17:13:11 +01:00
Tim Diekmann aae3c52c7a Remove the note on the internal capacity field in `RawVec` 2020-03-26 17:12:57 +01:00
Tim Diekmann ba26a9e957 Fix assertion in `shrink` to use `capacity()` instead 2020-03-26 17:12:45 +01:00
Tim Diekmann 42a8547038 Fix comment in `RawVec::into_box()` 2020-03-26 17:12:35 +01:00
Tim Diekmann c1fa02331a Fix ZST handling for `RawVec` 2020-03-26 17:12:27 +01:00
Tim Diekmann 2526accdd3 Fix issues from review and unsoundness of `RawVec::into_box` 2020-03-26 17:11:47 +01:00
Tim Diekmann 56cbf2f22a Overhaul of the `AllocRef` trait to match allocator-wg's latest consens 2020-03-26 17:10:54 +01:00
Niko Matsakis fda3378e3f introduce `negative_impls` feature gate and document
They used to be covered by `optin_builtin_traits` but negative impls
are now applicable to all traits, not just auto traits.

This also adds docs in the unstable book for the current state of auto traits.
2020-03-26 06:52:55 -04:00
Kornel 42b10e51c1 must_use on split_off
#70194
2020-03-24 17:35:40 +00:00
Saoirse Shipwreckt 32f5724e8a Apply suggestions from code review
Co-Authored-By: Ashley Mannix <ashleymannix@live.com.au>
2020-03-23 15:45:30 +01:00
Saoirse Shipwreckt caff9f92ab Update src/liballoc/task.rs
Co-Authored-By: Ashley Mannix <ashleymannix@live.com.au>
2020-03-23 15:45:30 +01:00
Without Boats 3ae74cafe4 More explicit; CFG on atomic pointer 2020-03-23 15:45:30 +01:00
Without Boats ede03a4175 typo 2020-03-23 15:45:30 +01:00
Without Boats c9acdb0bd4 Improve safety implementation, fix typos 2020-03-23 15:45:30 +01:00
Without Boats 06ede350c2 Add Wake trait for safe construction of Wakers.
Currently, constructing a waker requires calling the unsafe
`Waker::from_raw` API. This API requires the user to manually construct
a vtable for the waker themself - which is both cumbersome and very
error prone. This API would provide an ergonomic, straightforward and
guaranteed memory-safe way of constructing a waker.

It has been our longstanding intention that the `Waker` type essentially
function as an `Arc<dyn Wake>`, with a `Wake` trait as defined here. Two
considerations prevented the original API from being shipped as simply
an `Arc<dyn Wake>`:

- We want to support futures on embedded systems, which may not have an
  allocator, and in optimized executors for which this API may not be
  best-suited. Therefore, we have always explicitly supported the
  maximally-flexible (but also memory-unsafe) `RawWaker` API, and
  `Waker` has always lived in libcore.
- Because `Waker` lives in libcore and `Arc` lives in liballoc, it has
  not been feasible to provide a constructor for `Waker` from `Arc<dyn
  Wake>`.

Therefore, the Wake trait was left out of the initial version of the
task waker API.

However, as Rust 1.41, it is possible under the more flexible orphan
rules to implement `From<Arc<W>> for Waker where W: Wake` in liballoc.
Therefore, we can now define this constructor even though `Waker` lives
in libcore.

This PR adds these APIs:

- A `Wake` trait, which contains two methods
    - A required method `wake`, which is called by `Waker::wake`
    - A provided method `wake_by_ref`, which is called by
      `Waker::wake_by_ref` and which implementors can override if they
      can optimize this use case.
- An implementation of `From<Arc<W>> for Waker where W: Wake + Send +
  Sync + 'static`
- A similar implementation of `From<Arc<W>> for RawWaker`.
2020-03-23 15:44:58 +01:00
Mazdak Farrokhzad c984a96189
Rollup merge of #70269 - matthiaskrgr:clippy_closures, r=Dylan-DPC
remove redundant closures (clippy::redundant_closure)
2020-03-23 04:26:15 +01:00
Bastian Kauschke d068545bfb
update `make_contiguous` docs
Co-Authored-By: Amanieu d'Antras <amanieu@gmail.com>
2020-03-22 16:00:51 +01:00
Dylan DPC 8fe8bad96b
Rollup merge of #70254 - matthiaskrgr:cl4ppy, r=Centril
couple more clippy fixes (let_and_return, if_same_then_else)

* summarize if-else-code with identical blocks (clippy::if_same_then_else)
* don't create variable bindings just to return the bound value immediately (clippy::let_and_return)
2020-03-22 15:48:41 +01:00
Dylan DPC 0bc5fc99d3
Rollup merge of #68099 - lukaslueg:into_raw_unsafe, r=LukasKalbertodt
Amend Rc/Arc::from_raw() docs regarding unsafety

[This](https://stackoverflow.com/questions/59671647/is-it-safe-to-clone-a-type-erased-arc-via-raw-pointer) question on SO boils down to "is it safe to `::from_raw()` a `Rc<T>`/`Arc<T>` using a dummy `T` even if `T` is never dereferenced via the new `Rc`/`Arc`?". It almost never is.

This PR amends the docs of `from_raw()` regarding this point.
2020-03-22 15:48:28 +01:00
Bastian Kauschke ecf301d987 document invariant of `VecDeque::as_(mut)_slice` 2020-03-22 14:59:38 +01:00
Matthias Krüger 263cbd1bbe remove redundant closures (clippy::redundant_closure) 2020-03-22 12:43:19 +01:00
Bastian Kauschke 626fdbce8b add `fn make_contiguous` to VecDeque 2020-03-22 09:35:08 +01:00
Matthias Krüger 74d68ea7eb don't create variable bindings just to return the bound value immediately (clippy::let_and_return) 2020-03-22 00:35:25 +01:00
Mazdak Farrokhzad 54285db640
Rollup merge of #70194 - kornelski:must_split, r=joshtriplett
#[must_use] on split_off()

I've noticed this function used for truncation in the wild. `must_use` will clear that up.
2020-03-21 05:33:36 +01:00
Mazdak Farrokhzad 9d9e3813b2
Rollup merge of #70111 - Mark-Simulacrum:btree-no-shared, r=cuviper
BTreeMap: remove shared root

This replaces the shared root with `Option`s in the BTreeMap code, and then slightly cleans up the node manipulation code taking advantage of the removal of the shared root. I expect that further simplification is possible, but wanted to get this posted for initial review.

Note that `BTreeMap::new()` continues to not allocate.

Benchmarks seem within the margin of error/unaffected, as expected for an entirely predictable branch.

```
 name                                 alloc-bench-a ns/iter  alloc-bench-b ns/iter  diff ns/iter  diff %  speedup
 btree::map::iter_mut_20              20                     21                                1   5.00%   x 0.95
 btree::set::clone_100                1,360                  1,439                            79   5.81%   x 0.95
 btree::set::clone_100_and_into_iter  1,319                  1,434                           115   8.72%   x 0.92
 btree::set::clone_10k                143,515                150,991                       7,476   5.21%   x 0.95
 btree::set::clone_10k_and_clear      142,792                152,916                      10,124   7.09%   x 0.93
 btree::set::clone_10k_and_into_iter  146,019                154,561                       8,542   5.85%   x 0.94
```
2020-03-21 05:33:21 +01:00
Kornel 2f7d7c0333 must_use on split_off 2020-03-20 14:40:35 +00:00
Mark Rousskov 4d85314a00 Update test commentary for shared root removal 2020-03-20 09:43:41 -04:00
Mark Rousskov 13f6d771bb Simplify ensure_root_is_owned callers
This makes ensure_root_is_owned return a reference to the (now guaranteed to
exist) root, allowing callers to operate on it without going through another
unwrap.

Unfortunately this is only rarely useful as it's frequently the case that both
the length and the root need to be accessed and field-level borrows in methods
don't yet exist.
2020-03-20 09:43:41 -04:00
Mark Rousskov 54b7c38889 Drop NodeHeader type from BTree code
We no longer have a separate header because the shared root is gone; all code
can work solely with leafs now.
2020-03-20 09:43:41 -04:00
Mark Rousskov 3c04fda751 Make functions dependent only on shared root avoidance safe 2020-03-20 09:43:40 -04:00
Mark Rousskov 1c44f852df Remove shared root code and assertions from BTree nodes 2020-03-20 09:43:19 -04:00
Mark Rousskov e61f126fb4 Replace shared root with optional root
This simplifies the node manipulation, as we can (in later commits) always know
when traversing nodes that we are not in a shared root.
2020-03-20 09:42:10 -04:00
Tomasz Miąsko fd0e15bbcd Make std::sync::Arc compatible with ThreadSanitizer
The memory fences used previously in Arc implementation are not properly
understood by ThreadSanitizer as synchronization primitives. This had
unfortunate effect where running any non-trivial program compiled with
`-Z sanitizer=thread` would result in numerous false positives.

Replace acquire fences with acquire loads when using ThreadSanitizer to
address the issue.
2020-03-20 00:18:44 +01:00
Mazdak Farrokhzad f907598ba4
Rollup merge of #70029 - jonas-schievink:bootstrap, r=Centril
Bump the bootstrap compiler
2020-03-17 03:05:17 +01:00
Jonas Schievink f53f9a88f1 Bump the bootstrap compiler 2020-03-15 19:43:25 +01:00
Mazdak Farrokhzad cc1623267b
Rollup merge of #69661 - lopopolo:string-from-mut-str, r=sfackler
Implement From<&mut str> for String

I ran into this missing impl when trying to do `String::from` on the result returned from this API in the `uuid` crate:

https://docs.rs/uuid/0.8.1/uuid/adapter/struct.Hyphenated.html#method.encode_lower

I wasn't sure what to put in the stability annotation. I'd appreciate some help with that :)
2020-03-15 15:40:07 +01:00
Yuki Okushi c13548dccd
Rollup merge of #69809 - matthiaskrgr:lifetimes, r=eddyb
remove lifetimes that can be elided (clippy::needless_lifetimes)
2020-03-14 04:03:26 +09:00
Matthias Krüger 7b1b08cfee remove lifetimes that can be elided (clippy::needless_lifetimes) 2020-03-12 20:03:09 +01:00
Mazdak Farrokhzad d21320cbd9
Rollup merge of #69792 - LenaWil:try_reserve_error/impl-error, r=sfackler
Implement Error for TryReserveError

I noticed that the Error trait wasn't implemented for TryReserveError. (#48043)

Not sure if the error messages and code style are 100% correct, it's my first time contributing to the Rust std.
2020-03-12 16:32:21 +01:00
Lena Wildervanck 2c90a37969 Reformat match statement to make the check pass 2020-03-11 17:55:14 +01:00
Lena Wildervanck 599cd683ea Format the match statement 2020-03-11 17:30:04 +01:00
Mazdak Farrokhzad 080d41391d
Rollup merge of #69828 - RalfJung:vec-leak, r=kennytm
fix memory leak when vec::IntoIter panics during drop

Fixes https://github.com/rust-lang/rust/issues/69770
2020-03-11 14:03:47 +01:00
Ryan Lopopolo 533784d3a2 Add docs for From::<&mut str>::from String impl 2020-03-10 18:47:19 -07:00
Ryan Lopopolo 18feaa3fa2 Add stable feature name 2020-03-10 18:45:08 -07:00
Joshua Nelson 3477e67a92 Allow vec.rs to be over 3000 lines :( 2020-03-10 23:49:45 +00:00
Joshua Nelson 8212584c9e Bump release cutoff 2020-03-10 23:44:46 +00:00
Joshua Nelson 1ac4a46142 make the impl a little prettier 2020-03-10 23:44:46 +00:00
Joshua Nelson 96794d86f1 fix test failure 2020-03-10 23:44:46 +00:00
Joshua Nelson f267d9dc19 limit From impl to LengthAtMost32
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
2020-03-10 23:44:46 +00:00
Joshua Nelson daeb8ece8c fix error compiling stage2
Co-Authored-By: lzutao <taolzu@gmail.com>
2020-03-10 23:44:46 +00:00
Joshua Nelson 62722735fb impl From<[T; N]> for Vec<T> 2020-03-10 23:44:46 +00:00
Mazdak Farrokhzad 3e9efbd8b4
Rollup merge of #69877 - CAD97:patch-1, r=dtolnay
Vec::new is const stable in 1.39 not 1.32

Changelog: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1390-2019-11-07

This really surprised me when a MSRV check for 1.35 failed with `Vec::new is not yet stable as a const fn` and the docs said that it was const stabilized in 1.32.
2020-03-10 06:47:59 +01:00
Mazdak Farrokhzad 20361bd2bf
Rollup merge of #69861 - Dylnuge:dylnuge/locale-doc, r=Mark-Simulacrum
Add note about localization to std::fmt docs

Closes #69681
2020-03-10 06:47:58 +01:00
Mazdak Farrokhzad 6ad5e69b5a
Rollup merge of #69799 - TimDiekmann:zst, r=Amanieu
Allow ZSTs in `AllocRef`

Allows ZSTs in all `AllocRef` methods. The implementation of `AllocRef` for `Global` and `System` were adjusted to reflect those changes.

This is the second item on the roadmap to support ZSTs in `AllocRef`: https://github.com/rust-lang/wg-allocators/issues/38#issuecomment-595861542
After this has landed, I will adapt `RawVec`, but since this will be a pretty big overhaul, it makes sense to do a different PR for it.

~~Requires #69794 to land first~~

r? @Amanieu
2020-03-10 06:47:52 +01:00
Christopher Durham a56196205b
Vec::new is const tstable in 1.39 not 1.32 2020-03-09 23:07:04 -04:00
Dylan Nugent 7c60405dd6 Add note about localization to std::fmt docs 2020-03-09 12:31:33 -04:00
Mazdak Farrokhzad c13d296e72
Rollup merge of #69668 - ssomers:btreemap_even_more_comments, r=Mark-Simulacrum
More documentation and simplification of BTreeMap's internals

Salvage the documentation and simplification from #67980, without changing the type locked down by debuginfo.

r? @rkruppe
2020-03-08 16:53:40 +01:00
Ralf Jung 528cbc4879 fix memory leak when vec::IntoIter panics during drop 2020-03-08 16:43:03 +01:00
Mazdak Farrokhzad f497325b13
Rollup merge of #69776 - ssomers:fix69769, r=Mark-Simulacrum
Fix & test leak of some BTreeMap nodes on panic during `into_iter`

Fixes #69769
2020-03-08 11:51:17 +01:00
Tim Diekmann f77afc8f9c Allow ZSTs in `AllocRef` 2020-03-08 11:01:12 +01:00
Mazdak Farrokhzad 10f999b72d
Rollup merge of #69773 - matthiaskrgr:typos, r=petrochenkov
fix various typos
2020-03-07 17:27:32 +01:00
Mazdak Farrokhzad e6d4996a43
Rollup merge of #69765 - RalfJung:miri-test, r=LukasKalbertodt
reduce test size for Miri

The larger sizes take quite a while, and there is probably little point in repeating this quite so often.
2020-03-07 17:27:30 +01:00
Lena Wildervanck b900de0f77 Implement Error for TryReserveError 2020-03-07 00:59:25 +01:00
Matthias Krüger 83980aca20 Don't redundantly repeat field names (clippy::redundant_field_names) 2020-03-06 19:42:18 +01:00
Matthias Krüger 136ad015b6 fix various typos 2020-03-06 15:19:31 +01:00
Stein Somers 44c97c43b5 Fix & test leak of some BTreeMap nodes on panic during `into_iter` 2020-03-06 14:50:09 +01:00
Ralf Jung 2770f300b1 reduce test size for Miri 2020-03-05 23:41:17 +01:00
TrolledWoods 79bc934ff3
Fixed a typo
"vector" was used instead of "string"
2020-03-05 20:20:02 +01:00
Stein Somers 9384cba72e Documentation and slight simplification of BTreeMap's internals 2020-03-04 23:33:30 +01:00
Dylan DPC 8ca3e59f8a
Rollup merge of #69650 - matthiaskrgr:clnp, r=varkor
cleanup more iterator usages (and other things)

* Improve weird formatting by moving comment inside else-code block.
* Use .any(x) instead of .find(x).is_some() on iterators.
* Use .nth(x) instead of .skip(x).next() on iterators.
* Simplify conditions like  x + 1 <= y   to   x < y
* Use let instead of match to get value of enum with single variant.
2020-03-03 21:26:13 +01:00
Yuki Okushi 4699b29a04
Rollup merge of #69609 - TimDiekmann:excess, r=Amanieu
Remove `usable_size` APIs

This removes the usable size APIs:
- remove `usable_size` (obv)
- change return type of allocating methods to include the allocated size
- remove `_excess` API

r? @Amanieu
closes rust-lang/wg-allocators#17
2020-03-03 17:50:06 +09:00
Ryan Lopopolo 2a29726fcd Implement From<&mut str> for String 2020-03-02 21:17:58 -08:00
Matthias Krüger 5abaeb3d67 Simplify conditions like x + 1 <= y to x < y 2020-03-03 01:00:32 +01:00
Tim Diekmann d8e3557dba Remove `usable_size` APIs 2020-03-03 00:08:24 +01:00
Dylan DPC 87284d7e79
Rollup merge of #69569 - matthiaskrgr:nonminimal_bool, r=mark-Simulacrum
simplify boolean expressions
2020-03-01 17:23:30 +01:00
Dylan DPC 55d0a8b201
Rollup merge of #69568 - JOE1994:patch-2, r=Dylan-DPC
Clarify explanation of Vec<T> 'fn resize'

1. Clarified on what should implement `Clone` trait.
2. Minor grammar fix:
to be able clone => to be able **to** clone
2020-03-01 17:23:29 +01:00
Ralf Jung 0edc90cd18 clarify alignment requirements in Vec::from_raw_parts 2020-02-29 14:07:20 +01:00
Matthias Krüger 56a3da3bd0 simplify boolean expressions 2020-02-29 11:36:18 +01:00
Youngsuk Kim 6e265c5bc5
Remove trailing whitespace
Removed trailing whitespace which caused to fail pretty-check
2020-02-29 00:55:05 -05:00
Youngsuk Kim fb46d2b82e
Update src/liballoc/vec.rs
Following suggestion from @jonas-schievink

Co-Authored-By: Jonas Schievink <jonasschievink@gmail.com>
2020-02-29 00:52:32 -05:00
Youngsuk Kim 2ad52cd16d
Clarify explanation of 'fn resize'
1. Clarified on what should implement 'Clone' trait.
2. Minor grammar fix:
to be able clone => to be able to clone
2020-02-28 19:28:26 -05:00
Mazdak Farrokhzad e4cedc9df2
Rollup merge of #69538 - JohnTitor:boxed-slice-try-from, r=Centril
Stabilize `boxed_slice_try_from`

Closes #69202
2020-02-28 17:17:33 +01:00
bors e2223c94bf Auto merge of #68827 - ssomers:btree_navigation_revisited, r=Mark-Simulacrum
BTreeMap navigation done safer & faster

It turns out that there was a faster way to do the tree navigation code bundled in #67073, by moving from edge to KV and from KV to next edge separately. It extracts most of the code as safe functions, and contains the duplication of handles within the short wrapper functions.

This somehow hits a sweet spot in the compiler because it reports boosts all over the board:
```
>cargo benchcmp pre3.txt posz4.txt --threshold 5
 name                                           pre3.txt ns/iter  posz4.txt ns/iter  diff ns/iter   diff %  speedup
 btree::map::first_and_last_0                   40                37                           -3   -7.50%   x 1.08
 btree::map::first_and_last_100                 58                44                          -14  -24.14%   x 1.32
 btree::map::iter_1000                          8,920             3,419                    -5,501  -61.67%   x 2.61
 btree::map::iter_100000                        1,069,290         411,615                -657,675  -61.51%   x 2.60
 btree::map::iter_20                            169               58                         -111  -65.68%   x 2.91
 btree::map::iter_mut_1000                      8,701             3,303                    -5,398  -62.04%   x 2.63
 btree::map::iter_mut_100000                    1,034,560         405,975                -628,585  -60.76%   x 2.55
 btree::map::iter_mut_20                        165               58                         -107  -64.85%   x 2.84
 btree::set::clone_100                          1,831             1,562                      -269  -14.69%   x 1.17
 btree::set::clone_100_and_clear                1,831             1,565                      -266  -14.53%   x 1.17
 btree::set::clone_100_and_into_iter            1,917             1,541                      -376  -19.61%   x 1.24
 btree::set::clone_100_and_pop_all              2,609             2,441                      -168   -6.44%   x 1.07
 btree::set::clone_100_and_remove_all           4,598             3,927                      -671  -14.59%   x 1.17
 btree::set::clone_100_and_remove_half          2,765             2,551                      -214   -7.74%   x 1.08
 btree::set::clone_10k                          191,610           164,616                 -26,994  -14.09%   x 1.16
 btree::set::clone_10k_and_clear                192,003           164,616                 -27,387  -14.26%   x 1.17
 btree::set::clone_10k_and_into_iter            200,037           163,010                 -37,027  -18.51%   x 1.23
 btree::set::clone_10k_and_pop_all              267,023           250,913                 -16,110   -6.03%   x 1.06
 btree::set::clone_10k_and_remove_all           536,230           464,100                 -72,130  -13.45%   x 1.16
 btree::set::clone_10k_and_remove_half          453,350           430,545                 -22,805   -5.03%   x 1.05
 btree::set::difference_random_100_vs_100       1,787             801                        -986  -55.18%   x 2.23
 btree::set::difference_random_100_vs_10k       2,978             2,696                      -282   -9.47%   x 1.10
 btree::set::difference_random_10k_vs_100       111,075           54,734                  -56,341  -50.72%   x 2.03
 btree::set::difference_random_10k_vs_10k       246,380           175,980                 -70,400  -28.57%   x 1.40
 btree::set::difference_staggered_100_vs_100    1,789             951                        -838  -46.84%   x 1.88
 btree::set::difference_staggered_100_vs_10k    2,798             2,606                      -192   -6.86%   x 1.07
 btree::set::difference_staggered_10k_vs_10k    176,452           97,401                  -79,051  -44.80%   x 1.81
 btree::set::intersection_100_neg_vs_10k_pos    34                32                           -2   -5.88%   x 1.06
 btree::set::intersection_100_pos_vs_100_neg    30                27                           -3  -10.00%   x 1.11
 btree::set::intersection_random_100_vs_100     1,537             613                        -924  -60.12%   x 2.51
 btree::set::intersection_random_100_vs_10k     2,793             2,649                      -144   -5.16%   x 1.05
 btree::set::intersection_random_10k_vs_10k     222,127           147,166                 -74,961  -33.75%   x 1.51
 btree::set::intersection_staggered_100_vs_100  1,447             622                        -825  -57.01%   x 2.33
 btree::set::intersection_staggered_100_vs_10k  2,606             2,382                      -224   -8.60%   x 1.09
 btree::set::intersection_staggered_10k_vs_10k  143,620           58,790                  -84,830  -59.07%   x 2.44
 btree::set::is_subset_100_vs_100               1,349             488                        -861  -63.83%   x 2.76
 btree::set::is_subset_100_vs_10k               1,720             1,428                      -292  -16.98%   x 1.20
 btree::set::is_subset_10k_vs_10k               135,984           48,527                  -87,457  -64.31%   x 2.80
```
The `first_and_last` ones are noise (they don't do iteration), the others seem genuine.
As always, approved by Miri.

Also, a separate commit with some more benchmarks of mutable behaviour (which also benefit).

r? @cuviper
2020-02-28 09:32:34 +00:00
Yuki Okushi 4e0bea326e Stabilize `boxed_slice_try_from` 2020-02-28 13:28:09 +09:00
Stein Somers 9f7b58f3c9 Make implementation of navigation simpler, safer and faster 2020-02-28 01:06:23 +01:00
Stein Somers 4f6661a18d Fix and test implementation of BTreeMap's first_entry, last_entry, pop_first, pop_last 2020-02-28 00:13:32 +01:00
bors 892cb143e5 Auto merge of #67290 - jonas-schievink:leak-audit, r=KodrAus
Audit liballoc for leaks in `Drop` impls when user destructor panics

Inspired by https://github.com/rust-lang/rust/pull/67243 and https://github.com/rust-lang/rust/pull/67235, this audits and hopefully fixes the remaining `Drop` impls in liballoc for resource leaks in the presence of panics in destructors called by the affected `Drop` impl.

This does not touch `Hash{Map,Set}` since they live in hashbrown. They have similar issues though.

r? @KodrAus
2020-02-26 12:48:53 +00:00
bors 87e494c4cd Auto merge of #67330 - golddranks:split_inclusive, r=kodraus
Implement split_inclusive for slice and str

# Overview
* Implement `split_inclusive` for `slice` and `str` and `split_inclusive_mut` for `slice`
* `split_inclusive` is a substring/subslice splitting iterator that includes the matched part in the iterated substrings as a terminator.
* EDIT: The behaviour has now changed, as per @KodrAus 's input, to the same semantics with the `split_terminator` function. I updated the examples below.
* Two examples below:
```Rust
    let data = "\nMäry häd ä little lämb\nLittle lämb\n";
    let split: Vec<&str> = data.split_inclusive('\n').collect();
    assert_eq!(split, ["\n", "Märy häd ä little lämb\n", "Little lämb\n"]);
```

```Rust
    let uppercase_separated = "SheePSharKTurtlECaT";
    let mut first_char = true;
    let split: Vec<&str> = uppercase_separated.split_inclusive(|c: char| {
        let split = !first_char && c.is_uppercase();
        first_char = split;
        split
    }).collect();
    assert_eq!(split, ["SheeP", "SharK", "TurtlE", "CaT"]);
```

# Justification for the API
* I was surprised to find that stdlib currently only has splitting iterators that leave out the matched part. In my experience, wanting to leave a substring terminator as a part of the substring is a pretty common usecase.
* This API is strictly more expressive than the standard `split` API: it's easy to get the behaviour of `split` by mapping a subslicing operation that drops the terminator. On the other hand it's impossible to derive this behaviour from `split` without using hacky and brittle `unsafe` code. The normal way to achieve this functionality would be implementing the iterator yourself.
* Especially when dealing with mutable slices, the only way currently is to use `split_at_mut`. This API provides an ergonomic alternative that plays to the strengths of the iterating capabilities of Rust. (Using `split_at_mut` iteratively used to be a real pain before NLL, fortunately the situation is a bit better now.)

# Discussion items
* <s>Does it make sense to mimic `split_terminator` in that the final empty slice would be left off in case of the string/slice ending with a terminator? It might do, as this use case is naturally geared towards considering the matching part as a terminator instead of a separator.</s>
  * EDIT: The behaviour was changed to mimic `split_terminator`.
* Does it make sense to have `split_inclusive_mut` for `&mut str`?
2020-02-22 03:54:50 +00:00
Dylan DPC f7ce5ff19c
Rollup merge of #68705 - BijanT:ll_remove, r=Mark-Simulacrum
Add LinkedList::remove()

LinkedList::remove() removes the element at the specified index and returns it.

I added this because I think having a remove function would be useful to have, and similar functions are in other containers, like Vec and HashMap.

I'm not sure if adding a feature like this requires an RFC or not, so I'm sorry if this PR is premature.
2020-02-20 10:49:08 +01:00
Peter Todd 883e69db95
Change FromStr for String to use Infallible directly
Fixes the confusing documentation on `ParseError` by making it
irrelevant.
2020-02-19 16:37:58 -05:00
Bijan Tabatabai c797ce7877 Add LinkedList::remove()
LinkedList::remove() removes the element at the specified index and returns it.

Signed-off-by: Bijan Tabatabai <bijan311@yahoo.com>
2020-02-19 10:29:12 -06:00
Stein Somers da226dd9dc Lighten tests, in particular for Miri, yet test and explain more 2020-02-16 22:35:44 +01:00
Stein Somers 914b855f40 Fix comments outdated during #66648 2020-02-16 22:10:14 +01:00
Yuki Okushi 5fbfaacab3
Rollup merge of #69058 - TimDiekmann:box, r=Amanieu
Preparation for allocator aware `Box`

This cleans up the `Box` code a bit, and uses `Box::from_raw(ptr)` instead of `Box(ptr)`.
Additionally, `box_free` and `exchange_malloc` now uses the `AllocRef` trait and a comment was added on how `box_free` is tied to `Box`.

This a preparation for an upcoming PR, which makes `Box` aware of an allocator.

r? @Amanieu
2020-02-12 18:55:48 +09:00
Tim Diekmann 76aa29ff5e
Preparation for allocator aware `Box` 2020-02-11 13:16:20 +01:00
Tim Diekmann 25de80ad23 Remove common usage pattern from `AllocRef` 2020-02-10 18:38:09 +01:00
Jonas Schievink 3516df3ecb
Rollup merge of #68742 - tspiteri:string-as-mut, r=sfackler
implement AsMut<str> for String

Closes #68741.
2020-02-09 18:23:30 +01:00
Jonas Schievink 0b50319af6
Rollup merge of #68738 - kennytm:derive-clone-eq-for-fromutf8error, r=sfackler
Derive Clone + Eq for std::string::FromUtf8Error

Implement `Clone` and `Eq` for `std::string::FromUtf8Error`.

Both the inner `Vec<u8>` and `std::str::Utf8Error` are also `Clone + Eq`, so I don't see why we shouldn't derive them on `FromUtf8Error` as well.

(impl are insta-stable, requiring FCP from T-libs.)
2020-02-09 18:23:28 +01:00
Pyry Kontio 5c9dc57cb5 Don't return empty slice on last iteration with matched terminator. Test reverse iteration. 2020-02-09 23:49:44 +09:00
Pyry Kontio 86bf96291d Implement split_inclusive for slice and str, an splitting iterator that includes the matched part in the iterated substrings as a terminator. 2020-02-09 23:48:52 +09:00
Dylan DPC cb87c958ef
Rollup merge of #68834 - ssomers:btree_first_last_fix68829, r=KodrAus
Fix and test implementation of BTreeMap's first/last_entry, pop_first/last

Properly implement and test `first_entry` & `last_entry` to fix problem report #68829
2020-02-09 00:53:52 +01:00
Stein Somers b685985ea4 Refine and extend benchmarks of mutable BTreeSet methods 2020-02-08 11:10:05 +01:00
Lukas Lueg 586c7e3907 Make rc::RcBox and sync::ArcInner repr(C)
Future-proof these types in case rustc reorders
the inner fields. As per discussion in PR #68099.
2020-02-07 22:59:24 +01:00
bors b5e21dbb5c Auto merge of #68499 - ssomers:btree_search_tidying, r=Mark-Simulacrum
BtreeMap range_search spruced up

#39457 created a lower level entry point for `range_search` to operate on, but it's really not hard to move it up a level of abstraction, making it somewhat shorter and reusing existing unsafe code (`new_edge` is unsafe although it is currently not tagged as such).

Benchmark added. Comparison says there's no real difference:
```
>cargo benchcmp old3.txt new3.txt --threshold 5
 name                                           old3.txt ns/iter  new3.txt ns/iter  diff ns/iter   diff %  speedup
 btree::map::find_seq_100                       19                21                           2   10.53%   x 0.90
 btree::map::range_excluded_unbounded           3,117             2,838                     -279   -8.95%   x 1.10
 btree::map::range_included_unbounded           1,768             1,871                      103    5.83%   x 0.94
 btree::set::intersection_10k_neg_vs_10k_pos    35                37                           2    5.71%   x 0.95
 btree::set::intersection_staggered_100_vs_10k  2,488             2,314                     -174   -6.99%   x 1.08
 btree::set::is_subset_10k_vs_100               3                 2                           -1  -33.33%   x 1.50
```

r? @Mark-Simulacrum
2020-02-07 06:24:55 +00:00
Stein Somers ae03e16d08 Lift range_search up one level of abstraction 2020-02-07 02:41:28 +01:00
Stein Somers be051adb57 Create benchmarks for BTreeMap::range 2020-02-07 00:57:54 +01:00
Dylan DPC 2d8f6389d0
Rollup merge of #68524 - jonas-schievink:generator-resume-arguments, r=Zoxc
Generator Resume Arguments

cc https://github.com/rust-lang/rust/issues/43122 and https://github.com/rust-lang/rust/issues/56974

Blockers:
* [x] Fix miscompilation when resume argument is live across a yield point (https://github.com/rust-lang/rust/pull/68524#issuecomment-578459069)
* [x] Fix 10% compile time regression in `await-call-tree` benchmarks (https://github.com/rust-lang/rust/pull/68524#issuecomment-578487162)
  * [x] Fix remaining 1-3% regression (https://github.com/rust-lang/rust/pull/68524#issuecomment-579566255) - resolved (https://github.com/rust-lang/rust/pull/68524#issuecomment-581144901)
* [x] Make dropck rules account for resume arguments (https://github.com/rust-lang/rust/pull/68524#issuecomment-578541137)

Follow-up work:
* Change async/await desugaring to make use of this feature
* Rewrite [`box_region.rs`](3d8778d767/src/librustc_data_structures/box_region.rs) to use resume arguments (this shows up in profiles too)
2020-02-06 22:38:33 +01:00
Stein Somers fa9bfebfc9 Fix and test implementation of BTreeMap's first_entry, last_entry, pop_first, pop_last 2020-02-04 22:35:43 +01:00
Dylan DPC 5e561c2135
Rollup merge of #68711 - hman523:fix-68593, r=Dylan-DPC
Added upper bound of what vecs and boxes can allocate

Fixed issue #68593
I added a line of documentation to these two files to reflect that vectors and boxes ensure that they never allocate more than `isize::MAX` bytes.
r? @steveklabnik
2020-02-03 18:58:27 +01:00
Jonas Schievink 044fe0f558 Add a resume type parameter to `Generator` 2020-02-02 13:20:57 +01:00
Trevor Spiteri fd22823881 implement AsMut<str> for String 2020-02-01 22:19:28 +01:00
kennytm 847d5b4d13
Derive Clone + PartialEq + Eq for std::string::FromUtf8Error 2020-02-02 02:29:28 +08:00
hman523 346920c3c8 Fixed issue 68593 2020-01-31 13:41:07 -06:00
Stein Somers 3cf724d0c1 Bundle and document 6 BTreeMap navigation algorithms 2020-01-31 17:37:17 +01:00
Dylan DPC 6c67466f73
Rollup merge of #68680 - ollie27:doc_alloc_playground, r=Centril
Add `#![doc(html_playground_url = ...)]` to alloc crate

This adds the Run button to code examples just like the core and std docs have.
2020-01-31 01:21:31 +01:00
Oliver Middleton bfcfab630e Add `#![doc(html_playground_url = ...)]` to alloc crate
This adds the Run button to code examples just like the core and std docs have.
2020-01-30 21:14:39 +00:00
Dylan DPC f837c730ca
Rollup merge of #68468 - ssomers:btreemap_prefer_middle, r=Mark-Simulacrum
BTreeMap: tag and explain unsafe internal functions or assert preconditions

#68418 concluded that it's not desirable to tag all internal functions with preconditions as being unsafe. This PR does it to some functions, documents why, and elsewhere enforces the preconditions with asserts.
2020-01-30 01:46:42 +01:00
Dylan DPC 12c9562486
Rollup merge of #66648 - crgl:btree-clone-from, r=Amanieu
Implement clone_from for BTreeMap and BTreeSet

See #28481. This results in up to 90% speedups on simple data types when `self` and `other` are the same size, and is generally comparable or faster. Some concerns:

1. This implementation requires an `Ord` bound on the `Clone` implementation for `BTreeMap` and `BTreeSet`. Since these structs can only be created externally for keys with `Ord` implemented, this should be fine? If not, there's certainly a less safe way to do this.
2. Changing `next_unchecked` on `RangeMut` to return mutable key references allows for replacing the entire overlapping portion of both maps without changing the external interface in any way. However, if `clone_from` fails it can leave the `BTreeMap` in an invalid state, which might be unacceptable.

~This probably needs an FCP since it changes a trait bound, but (as far as I know?) that change cannot break any external code.~
2020-01-30 01:46:38 +01:00
Stein Somers ba87a50332 BTreeMap: tag and explain unsafe internal functions or assert preconditions
Co-Authored-By: Mark Rousskov <mark.simulacrum@gmail.com>
2020-01-29 11:52:21 +01:00
Yuki Okushi 8d3273cdba
Rollup merge of #68592 - pdbrito:master, r=jonas-schievink
fix: typo in vec.rs
2020-01-29 09:34:51 +09:00
Yuki Okushi af0c280be6
Rollup merge of #68378 - billyrieger:btreemap-remove-entry, r=KodrAus
Add BTreeMap::remove_entry

Implements https://github.com/rust-lang/rust/issues/66714.
2020-01-29 09:34:45 +09:00
Lukas Lueg b4c96a9199 Refine [Arc/Rc]::from_raw() docs 2020-01-28 22:28:13 +01:00
Charles Gleason 6c3e477d13 Reformat truncation section of clone_from 2020-01-28 13:00:06 -05:00
Charles Gleason 81b6f8c3fc Add private is_empty method to RangeMut 2020-01-28 11:46:49 -05:00
Charles Gleason 60a7c9421e Include empty BTreeMap in clone_from tests 2020-01-28 11:39:48 -05:00
Charles Gleason 3caa17b468
Format safety comment as per tidy
Co-Authored-By: Ashley Mannix <ashleymannix@live.com.au>
2020-01-28 11:27:31 -05:00
bors b181835a6b Auto merge of #68529 - TimDiekmann:rename-alloc, r=Amanieu
Rename `Alloc` to `AllocRef`

The allocator-wg has decided to merge this change upstream in https://github.com/rust-lang/wg-allocators/issues/8#issuecomment-577122958.

This renames `Alloc` to `AllocRef` because types that implement `Alloc` are a reference, smart pointer, or ZSTs. It is not possible to have an allocator like `MyAlloc([u8; N])`, that owns the memory and also implements `Alloc`, since that would mean, that moving a `Vec<T, MyAlloc>` would need to correct the internal pointer, which is not possible as we don't have move constructors.

For further explanation please see https://github.com/rust-lang/wg-allocators/issues/8#issuecomment-489464843 and the comments after that one.

Additionally it clarifies the semantics of `Clone` on an allocator. In the case of `AllocRef`, it is clear that the cloned handle still points to the same allocator instance, and that you can free data allocated from one handle with another handle.

The initial proposal was to rename `Alloc` to `AllocHandle`, but `Ref` expresses the semantics better than `Handle`. Also, the only appearance of `Handle` in `std` are for windows specific resources, which might be confusing.

Blocked on https://github.com/rust-lang/miri/pull/1160
2020-01-28 08:44:20 +00:00
Pedro de Brito 074cfcbf2d fix: typo in vec.rs 2020-01-28 07:59:07 +01:00
Billy Rieger 5654305a7b Add BTreeMap::remove_entry
Mainly for API parity with HashMap.

- Add BTreeMap::remove_entry
- Rewrite BTreeMap::remove to use remove_entry
- Use btreemap_remove_entry feature in doc comment
2020-01-28 00:43:18 -05:00
bors 82018665a5 Auto merge of #68234 - CAD97:slice-from-raw-parts, r=KodrAus
Stabilize ptr::slice_from_raw_parts[_mut]

Closes #36925, the tracking issue.
Initial impl: #60667

r? @rust-lang/libs

In addition to stabilizing, I've adjusted the example of `ptr::slice_from_raw_parts` to use `slice_from_raw_parts` instead of `slice_from_raw_parts_mut`, which was unnecessary for the example as written.
2020-01-28 05:31:34 +00:00
Tim Diekmann 7ca25db816
Rename `Alloc` to `AllocRef` 2020-01-27 21:39:51 +01:00
Mazdak Farrokhzad d532a04a1c
Rollup merge of #67686 - ssomers:keys_start_slasher, r=Mark-Simulacrum
Simplify NodeHeader by avoiding slices in BTreeMaps with shared roots

Simplify a complicated piece of code that creates slices of keys in node leaves.
2020-01-21 19:42:17 +01:00
Stein Somers 3e947ef031 Declare unsafe functions that can no longer handle shared roots 2020-01-21 16:12:19 +01:00
Stein Somers 1b800a5671 trade in outdated comments for correct ones 2020-01-21 10:30:14 +01:00
bors 8c73fa70f3 Auto merge of #68154 - ssomers:btreemap_navigation_benches, r=Mark-Simulacrum
Add more BTreeMap/BTreeSet benchmarks regarding iteration

Serving #67073 or other developments
2020-01-21 03:07:01 +00:00
Jonas Schievink e5987a062f Format 2020-01-19 20:50:00 +01:00
Jonas Schievink 52d6c90488 Update comments in `Drain`s `Drop` impl 2020-01-19 20:28:47 +01:00
Jonas Schievink 75f721df97 Move VecDeque Drain iterator to new file 2020-01-19 20:28:47 +01:00
Jonas Schievink 1f373f4aeb Add test for panic in LL DrainFilter predicate 2020-01-19 20:24:36 +01:00
Jonas Schievink 0ae16b47ff Avoid leak in DrainFilter when a drop panics 2020-01-19 20:24:36 +01:00
Jonas Schievink 163ed23f00 Fix leak in vec::IntoIter when a destructor panics 2020-01-19 20:24:08 +01:00
Jonas Schievink b04ca13873 Fix leak in VecDeque::drain when drop panics 2020-01-19 20:24:08 +01:00
Jonas Schievink dc492452da Fix leak in btree_map::IntoIter when drop panics 2020-01-19 20:24:08 +01:00
Jonas Schievink 5d04790dd2 Avoid leak in `vec::Drain` when item drop panics 2020-01-19 20:23:41 +01:00
Jonas Schievink 3e5eb2634c Fix `VecDeque::truncate` leak on drop panic 2020-01-19 20:23:07 +01:00
Jonas Schievink a859ca5c87 Fix `binary_heap::DrainSorted` drop leak on panics 2020-01-19 20:23:07 +01:00
bors 6250d56355 Auto merge of #67758 - ssomers:testing_range, r=Mark-Simulacrum
More thorough testing of BTreeMap::range

Test more of the paths in the `range_search` function in map.rs
2020-01-19 04:40:21 +00:00
Stein Somers 4dbae1e8ba Allow added string.insert benchmarks to compile 2020-01-17 01:05:49 +01:00
bors e02c475da5 Auto merge of #67339 - CAD97:rc-provenance, r=sfackler
Use pointer offset instead of deref for A/Rc::into_raw

Internals thread: https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/2?u=cad97

The current implementation of (`A`)`Rc::into_raw` uses the `Deref::deref` implementation to get the pointer-to-data that is returned. This is problematic in the proposed Stacked Borrow rules, as this only gets shared provenance over the data location. (Note that the strong/weak counts are `UnsafeCell` (`Cell`/`Atomic`) so shared provenance can still mutate them, but the data itself is not.) When promoted back to a real reference counted pointer, the restored pointer can be used for mutation through `::get_mut` (if it is the only surviving reference). However, this mutates through a pointer ultimately derived from a `&T` borrow, violating the Stacked Borrow rules.

There are three known potential solutions to this issue:

- Stacked Borrows is wrong, liballoc is correct.
- Fully admit (`A`)`Rc` as an "internal mutability" type and store the data payload in an `UnsafeCell` like the strong/weak counts are. (Note: this is not needed generally since the `RcBox`/`ArcInner` is stored behind a shared `NonNull` which maintains shared write provenance as a raw pointer.)
- Adjust `into_raw` to do direct manipulation of the pointer (like `from_raw`) so that it maintains write provenance and doesn't derive the pointer from a reference.

This PR implements the third option, as recommended by @RalfJung.

Potential future work: provide `as_raw` and `clone_raw` associated functions to allow the [`&T` -> (`A`)`Rc<T>` pattern](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/2?u=cad97) to be used soundly without creating (`A`)`Rc` from references.
2020-01-16 00:47:45 +00:00
Dylan DPC 1b7b8cbed3
Rollup merge of #68123 - crlf0710:linked_list_cursor, r=Amanieu
Implement Cursor for linked lists. (RFC 2570).

cc. #58533 cc. @Gankra

r? @Amanieu
2020-01-15 22:49:24 +05:30
CAD97 f76177ce43 Stabilize ptr::slice_from_raw_parts[_mut] 2020-01-14 20:15:39 -05:00
Charles Lew 06b9a73cfa Update APIs according to RFC change suggestions. 2020-01-14 20:11:52 +08:00