Commit Graph

2297 Commits

Author SHA1 Message Date
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
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
Ralf Jung 032d3cd553 fix BTreeMap test compilation with Miri 2020-03-30 12:04:05 +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