Commit Graph

2470 Commits

Author SHA1 Message Date
Josh Soref 6c8c3f8ac4
grammar: dealing-with 2020-05-06 19:01:27 -04:00
Josh Soref eb12784dc4
grammar: simplify to avoid that 2020-05-06 19:01:05 -04:00
Josh Soref 5d2d7e7725
grammar: stray comma 2020-05-06 19:00:40 -04:00
Josh Soref 488e660728
grammar: noun not verb 2020-05-06 19:00:15 -04:00
Josh Soref c8aba78613
grammar: subject-verb not subject-verb-verb 2020-05-06 18:59:52 -04:00
Josh Soref 5f54ce7ec9
grammar: disambiguate space-character 2020-05-06 18:58:45 -04:00
Josh Soref 39b5b7000a
grammar: count-agreement default ... is 2020-05-06 18:58:12 -04:00
Josh Soref 642541307f
grammar: which vs that 2020-05-06 18:56:25 -04:00
Dylan DPC 78a25cb10e
Rollup merge of #71510 - ssomers:btreemap_iter_intertwined, r=Mark-Simulacrum
Btreemap iter intertwined

3 commits:

1. Introduced benchmarks for `BTreeMap::iter()`. Benchmarks named `iter_20` were of the whole iteration process, so I renamed them. Also the benchmarks of `range` that I wrote earlier weren't very good. I included an (awkwardly named) one that compares `iter()` to `range(..)` on the same set, because the contrast is surprising:
```
 name                                           ns/iter
 btree::map::range_unbounded_unbounded          28,176
 btree::map::range_unbounded_vs_iter            89,369
```
Both dig up the same pair of leaf edges. `range(..)` also checks that some keys are correctly ordered, the only thing `iter()` does more is to copy the map's length.

2. Slightly refactoring the code to what I find more readable (not in chronological order of discovery), boosts performance:
```
>cargo-benchcmp.exe benchcmp a1 a2 --threshold 5
 name                                   a1 ns/iter  a2 ns/iter  diff ns/iter   diff %  speedup
 btree::map::find_rand_100              18          17                    -1   -5.56%   x 1.06
 btree::map::first_and_last_10k         64          71                     7   10.94%   x 0.90
 btree::map::iter_0                     2,939       2,209               -730  -24.84%   x 1.33
 btree::map::iter_1                     6,845       2,696             -4,149  -60.61%   x 2.54
 btree::map::iter_100                   8,556       3,672             -4,884  -57.08%   x 2.33
 btree::map::iter_10k                   9,292       5,884             -3,408  -36.68%   x 1.58
 btree::map::iter_1m                    10,268      6,510             -3,758  -36.60%   x 1.58
 btree::map::iteration_mut_100000       478,575     453,050          -25,525   -5.33%   x 1.06
 btree::map::range_unbounded_unbounded  28,176      36,169             7,993   28.37%   x 0.78
 btree::map::range_unbounded_vs_iter    89,369      38,290           -51,079  -57.16%   x 2.33
 btree::set::clone_100_and_remove_all   4,801       4,245               -556  -11.58%   x 1.13
 btree::set::clone_10k_and_remove_all   529,450     496,030          -33,420   -6.31%   x 1.07
```
But you can tell from the `range_unbounded_*` lines that, despite an unwarranted, vengeful attack on the range_unbounded_unbounded benchmark, this change still doesn't allow `iter()` to catch up with `range(..)`.

3. I guess that `range(..)` copes so well because it intertwines the leftmost and rightmost descend towards leaf edges, doing the two root node accesses close together, perhaps exploiting a CPU's internal pipelining? So the third commit distils a version of `range_search` (which we can't use directly because of the `Ord` bound), and we get another boost:
```
cargo-benchcmp.exe benchcmp a2 a3 --threshold 5
 name                                   a2 ns/iter  a3 ns/iter  diff ns/iter   diff %  speedup
 btree::map::first_and_last_100         40          43                     3    7.50%   x 0.93
 btree::map::first_and_last_10k         71          64                    -7   -9.86%   x 1.11
 btree::map::iter_0                     2,209       1,719               -490  -22.18%   x 1.29
 btree::map::iter_1                     2,696       2,205               -491  -18.21%   x 1.22
 btree::map::iter_100                   3,672       2,943               -729  -19.85%   x 1.25
 btree::map::iter_10k                   5,884       3,929             -1,955  -33.23%   x 1.50
 btree::map::iter_1m                    6,510       5,532               -978  -15.02%   x 1.18
 btree::map::iteration_mut_100000       453,050     476,667           23,617    5.21%   x 0.95
 btree::map::range_included_excluded    405,075     371,297          -33,778   -8.34%   x 1.09
 btree::map::range_included_included    427,577     397,440          -30,137   -7.05%   x 1.08
 btree::map::range_unbounded_unbounded  36,169      28,175            -7,994  -22.10%   x 1.28
 btree::map::range_unbounded_vs_iter    38,290      30,838            -7,452  -19.46%   x 1.24
```
But I think this is just fake news from the microbenchmarking media. `iter()` is still trying to catch up with `range(..)`. And we can sure do without another function. So I would skip this 3rd commit.

r? @Mark-Simulacrum
2020-05-06 13:22:05 +02:00
Dylan DPC ac84daf930
Rollup merge of #71892 - integer32llc:btreemap-entry-vacant-docs, r=jonas-schievink
Update btree_map::VacantEntry::insert docs to actually call insert

It looks like they were copied from the `or_insert` docs. This change
makes the example more like the hash_map::VacantEntry::insert docs.
2020-05-05 01:49:49 +02:00
Carol (Nichols || Goulding) d02128f92f
Update btree_map::VacantEntry::insert docs to actually call insert
It looks like they were copied from the `or_insert` docs. This change
makes the example more like the hash_map::VacantEntry::insert docs.
2020-05-04 15:49:15 -04:00
main() c5cdf7fe92
whoops 2020-05-04 16:10:59 +02:00
main() 1593e2b7df
Add remove_current_as_list to LinkedList's CursorMut
The `remove_current` method only returns the inner `T` and deallocates the list node. This is unnecessary for move operations, where the element is going to be linked back into this (or even a different) `LinkedList`. The `remove_current_as_list` method avoids this by returning the unlinked list node as a new single-element `LinkedList` structure .
2020-05-04 15:53:02 +02:00
Luca Gladiator aab345794f Make BTreeMap::new and BTreeSet::new const 2020-05-03 14:02:30 +02:00
Ralf Jung c66d02e3ba liballoc tests: Miri supports threads now 2020-05-01 11:16:38 +02:00
Tyler Mandry 4adebb9f29
Rollup merge of #71148 - bluss:vec-drop-raw-slice, r=RalfJung
Vec drop and truncate: drop using raw slice *mut [T]

By creating a *mut [T] directly (without going through &mut [T]), avoid
questions of validity of the contents of the slice.

Consider the following risky code:

```rust
unsafe {
    let mut v = Vec::<bool>::with_capacity(16);
    v.set_len(16);
}
```

The intention is that with this change, we avoid one of the soundness
questions about the above snippet, because Vec::drop no longer
produces a mutable slice of the vector's contents.

r? @RalfJung
2020-04-30 15:23:08 -07:00
cohenarthur eda7f8fdff rename-unique: Rename Unique::empty() to Unique::dangling()
rename-unique: Change calls and doc in raw_vec.rs

rename-unique: Change empty() -> dangling() in const-ptr-unique-rpass.rs
2020-04-30 11:00:45 +02:00
Ulrik Sverdrup f654daf3a6 Vec IntoIter: Drop using raw slice
Update Vec drop with a comment to explain why we want to use a raw
slice, and extend this pattern to also include the Vec's IntoIter.
2020-04-28 23:31:32 +02:00
Dylan DPC cddbed0003
Rollup merge of #71589 - RalfJung:unique-no-shr, r=SimonSapin
remove Unique::from for shared pointer types

r? @SimonSapin
2020-04-27 03:26:18 +02:00
Dylan DPC 398d3eeca1
Rollup merge of #71421 - elichai:2020-04-boxed-slice, r=sfackler
Add a function to turn Box<T> into Box<[T]>

Hi,
I think this is very useful, as currently it's not possible in safe rust to do this without re-allocating.
an alternative implementation of the same function can be:
```rust
pub fn into_boxed_slice<T>(boxed: Box<T>) -> Box<[T]> {
    unsafe {
        let slice = slice::from_raw_parts_mut(Box::into_raw(boxed), 1);
        Box::from_raw(slice)
    }
}
```

The only thing that makes me a little uncomfortable is this line :
> The alignment of array types is greater or equal to the alignment of its element type

from https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html

But then I see:
> The alignment of &T, &mut T, *const T and *mut T are the same, and are at least the word size.
> The alignment of &[T] is the word size.

from https://rust-lang.github.io/unsafe-code-guidelines/layout/pointers.html#representation

So I do believe this is valid(FWIW it also passes in miri https://play.rust-lang.org/?gist=c002b99364ee6b29862aeb3565a91c19)
2020-04-26 21:02:32 +02:00
Ralf Jung 7aebdb639a remove Unique::from for shared pointer types 2020-04-26 19:00:57 +02:00
Matthew Jasper cb2703945c Use min_specialization in liballoc
- Remove a type parameter from `[A]RcFromIter`.
- Remove an implementation of `[A]RcFromIter` that didn't actually
  specialize anything.
- Remove unused implementation of `IsZero` for `Option<&mut T>`.
- Change specializations of `[A]RcEqIdent` to use a marker trait version
of `Eq`.
- Remove `BTreeClone`. I couldn't find a way to make this work with
  `min_specialization`.
- Add `rustc_unsafe_specialization_marker` to `Copy` and `TrustedLen`.
2020-04-26 16:27:13 +01:00
Elichai Turkel 6f31f05aaf
Add a function to turn Box<T> into Box<[T]> (into_boxed_slice) 2020-04-26 15:42:39 +03:00
Dylan DPC aa9dc69522
Rollup merge of #71575 - jplatte:patch-4, r=Mark-Simulacrum
Fix stable(since) attribute for BTreeMap::remove_entry

Stabilized in #70712.

Maybe checking that the since attributes are added correctly should be automated through tidy? This is the third PR I'm opening that fixes a stable(since) attribute for something meant to be stabilized in 1.43 / 1.44 initially but then only stabilized in 1.45. (the other two are #71571, #71574)
2020-04-26 13:42:35 +02:00
Jonas Platte 35eae4bad2
Fix stable(since) attribute for BTreeMap::remove_entry 2020-04-26 12:31:05 +02:00
Matthias Krüger 8862f829bb fix more clippy warnings
clippy::{redundant_pattern_matching, clone_on_copy, iter_cloned_collect, option_as_ref_deref, match_ref_pats}
2020-04-26 02:24:01 +02:00
bors 0862458dad Auto merge of #71556 - Dylan-DPC:rollup-9ll4shr, r=Dylan-DPC
Rollup of 7 pull requests

Successful merges:

 - #69041 (proc_macro: Stabilize `Span::resolved_at` and `Span::located_at`)
 - #69813 (Implement BitOr and BitOrAssign for the NonZero integer types)
 - #70712 (stabilize BTreeMap::remove_entry)
 - #71168 (Deprecate `{Box,Rc,Arc}::into_raw_non_null`)
 - #71544 (Replace filter_map().next() calls with find_map())
 - #71545 (Fix comment in docstring example for Error::kind)
 - #71548 (Add missing Send and Sync impls for linked list Cursor and CursorMut.)

Failed merges:

r? @ghost
2020-04-25 17:49:00 +00:00
Dylan DPC 82642d708f
Rollup merge of #71548 - crlf0710:cursor_bounds, r=Amanieu
Add missing Send and Sync impls for linked list Cursor and CursorMut.

Someone pointed out these to me, and i think it's indeed reasonable to add those impl.

r? @Amanieu
2020-04-25 18:30:34 +02:00
Dylan DPC 939c93208c
Rollup merge of #71168 - SimonSapin:into_raw_non_null, r=Amanieu
Deprecate `{Box,Rc,Arc}::into_raw_non_null`

Per ongoing FCP at https://github.com/rust-lang/rust/issues/47336#issuecomment-586589016
See also https://github.com/rust-lang/rust/issues/47336#issuecomment-614054164
2020-04-25 18:30:29 +02:00
Dylan DPC 29fd528114
Rollup merge of #70712 - :stabilize-remove-entry, r=Amanieu
stabilize BTreeMap::remove_entry

This PR stabilizes `BTreeMap::remove_entry` as implemented in https://github.com/rust-lang/rust/pull/68378.

Closes https://github.com/rust-lang/rust/issues/66714
2020-04-25 18:30:27 +02:00
Charles Lew 78a034d168
Use the correct bound for `Cursor` `Send`
Co-Authored-By: Amanieu d'Antras <amanieu@gmail.com>
2020-04-25 22:48:16 +08:00
bors 659951c4a0 Auto merge of #71439 - Mark-Simulacrum:stage0-next, r=jonas-schievink
Bump bootstrap compiler

This bumps the bootstrap compiler and the rustfmt that x.py fmt uses.
2020-04-25 14:15:10 +00:00
Mark Rousskov 93eed402ad Bump bootstrap compiler 2020-04-25 09:25:33 -04:00
Dylan DPC 62b362472d
Rollup merge of #71523 - Mark-Simulacrum:alloc-inline-dup, r=Amanieu
Take a single root node in range_search

The unsafe code can be justified within range_search, as it makes sure to not
overlap the returned references, but from the callers perspective it's an
entirely safe algorithm and there's no need for the caller to know about the
duplication.

cc @ssomers
r? @Amanieu
2020-04-25 11:25:53 +02:00
Charles Lew b1fbd797c0 Add missing Send and Sync bounds for linked list Cursor and CursorMut. 2020-04-25 16:33:11 +08:00
Dylan DPC e20ca112cc
Rollup merge of #71485 - arlopurcell:binary_heap_retain, r=Amanieu
Add BinaryHeap::retain as suggested in #42849

This PR implements retain for BinaryHeap as suggested in #42849.

This is my first PR for Rust, so please let me know if I should be doing anything differently, thanks!
2020-04-25 01:35:59 +02:00
Stein Somers 873022797a Speed up BTreeMap iteration by intertwined descend to the initial leaf edges 2020-04-25 00:05:10 +02:00
Stein Somers 8f5c66c6a3 Introduce BTreeMap benches of iter itself 2020-04-25 00:05:10 +02:00
Mark Rousskov e6cf6a7bfe Take a single root node in range_search
The unsafe code can be justified within range_search, as it makes sure to not
overlap the returned references, but from the callers perspective it's an
entirely safe algorithm and there's no need for the caller to know about the
duplication.
2020-04-24 13:22:52 -04:00
Dylan DPC 08ca447267
Rollup merge of #71476 - RalfJung:miri-test-sizes, r=kennytm
more compact way to adjust test sizes for Miri

Inspired by @dtolnay
2020-04-24 13:14:26 +02:00
arlo 787eddc1ab Add BinaryHeap::retain as suggested in #42849 2020-04-24 04:44:20 -05:00
Ralf Jung eb1de2ff39 liballoc: more compact way to adjust test sizes for Miri 2020-04-23 20:05:01 +02:00
Tyler Ruckinger 547219c7e4 Fix doc link errors 2020-04-23 10:40:28 -04:00
Josh Stone 23f71fe5b5 Add tests from Cow 2020-04-22 14:16:21 -07:00
Josh Stone 6d40751b37 impl From<Cow> for Rc and Arc
These forward `Borrowed`/`Owned` values to existing `From` impls.

    impl<'a, B> From<Cow<'a, B>> for Rc<B>
    where
        B: ToOwned + ?Sized,
        Rc<B>: From<&'a B> + From<B::Owned>,

    impl<'a, B> From<Cow<'a, B>> for Arc<B>
    where
        B: ToOwned + ?Sized,
        Arc<B>: From<&'a B> + From<B::Owned>,
2020-04-22 13:33:42 -07:00
Josh Stone b0fb57bd8d impl From<Cow> for boxed slices and strings
These forward `Borrowed`/`Owned` values to existing `Box::from` impls.

- `From<Cow<'_, [T]>> for Box<[T]>`
- `From<Cow<'_, str>> for Box<str>`
- `From<Cow<'_, CStr>> for Box<CStr>`
- `From<Cow<'_, OsStr>> for Box<OsStr>`
- `From<Cow<'_, Path>> for Box<Path>`
2020-04-22 13:03:05 -07:00
Philipp Hansch 23b9f46fff
More diagnostic items for Clippy usage
This adds a couple of more diagnostic items to be used in Clippy.
I chose these particular ones because they were the types which we seem
to check for the most in Clippy. I'm not sure if the
`cfg_attr(not(test))` is needed, but it was also used for `Vec` and a
few other types.
2020-04-22 07:57:56 +02:00
Dylan DPC 4d11c3fe30
Rollup merge of #71107 - vorner:weak-into-raw-dangling, r=Amanieu
Address concerns of weak-into-raw

This should address the standing concerns in https://github.com/rust-lang/rust/issues/60728#issuecomment-612525616.

I've still left the ability to create a new dangling pointer from `null`, as I feel like this is the natural behaviour to expect, but I'm fine removing that too. I've modified the documentation to allow the `as_ptr` or `into_ptr` to return whatever garbage in case of a dangling pointer. I've also removed the guarantee to be able to do `from_raw(as_ptr)` from the documentation (but it would still work right now).

I've renamed the method and added implementations for `Rc`/`Arc`.

I've also tried if I can just „enable“ unsized types. I believe the current interface is compatible with them. But the inner implementation will be a bit challenging ‒ I can't use the `data_offset` as is used by `Rc` or `Arc` because it AFAIK „touches“ (creates a reference to) the live value of `T` ‒ and in case of `Weak`, it might be completely bogus or already dead ‒ so that would be UB.

`./x.py test tidy` is completely mad on my own system all over the code base :-(. I'll just hope it goes through CI, or will fix as necessary.

Is it OK if I ask @Amanieu for review, as the concerns are from you?

~r @Amanieu
2020-04-19 15:12:36 +02:00
Michal 'vorner' Vaner f4ded11b49
weak-into-raw: Add {Arc,Rc}::as_ptr
For consistency with Weak
2020-04-19 09:38:16 +02:00
Yuki Okushi 1a46159828
Explain why we shouldn't add inline attr to into_vec 2020-04-19 01:03:43 +09:00
bors 9d430cb351 Auto merge of #71204 - JohnTitor:into-vec, r=wesleywiser
perf: Remove inline attribute from `into_vec()`

It was introduced in #70565 and is likely related to this perf results: https://perf.rust-lang.org/compare.html?start=1edcfc83c6a08ddc5e63fc652b149baea0236e7c&end=d249d756374737eb014079901ac132f1e1ed905e&stat=instructions:u
Let's check if it's related to that.
r? @wesleywiser could you kick off perf check? I don't think I can do it.
2020-04-18 08:03:38 +00:00
Dylan DPC d5d9bf0406
Rollup merge of #71167 - RalfJung:big-o, r=shepmaster
big-O notation: parenthesis for function calls, explicit multiplication

I saw `O(n m log n)` in the docs and found that really hard to parse. In particular, I don't think we should use blank space as syntax for *both* multiplication and function calls, that is just confusing.

This PR makes both multiplication and function calls explicit using Rust-like syntax. If you prefer, I can also leave one of them implicit, but I believe explicit is better here.

While I was at it I also added backticks consistently.
2020-04-17 23:56:00 +02:00
Charles Lew 22e51cd78a Implement `Clone` for `liballoc::collections::linked_list::Cursor`. 2020-04-17 21:31:59 +08:00
Dylan DPC 28964b4ef2
Rollup merge of #71220 - cuviper:std_or_patterns, r=Mark-Simulacrum
Dogfood or_patterns in the standard library

We can start using `or_patterns` in the standard library as a step toward stabilization.

cc #54883 @Centril
2020-04-17 03:05:19 +02:00
Dylan DPC 53f3130bdb
Rollup merge of #71219 - JOE1994:patch-4, r=Mark-Simulacrum
Minor fixes to doc comments of 'VecDeque'

1. Changed descriptions of `fn get` & `fn get_mut`.
  Since both of these functions are returning references, and not the owned value, I thought the doc comments could be fixed to be consistent with doc comments of `fn front` & `fn front_mut`.

2. Other changes are minor fixes or additions for clarification.

Thank you for taking a look :)
2020-04-16 23:34:45 +02:00
Youngsuk Kim 9707bec3c5
Minor fixes to doc comments of 'VecDequeue'
1. Changed descriptions of `fn get` & `fn get_mut`.
  Since both of these functions are returning references, and not the owned value, I thought the doc comments could be fixed to be consistent with doc comments of `fn front` & `fn front_mut`.

2. Other changes are minor fixes or additions for clarification.

Thank you for taking a look :)
2020-04-16 16:26:14 -04:00
Josh Stone 2edd123a23 Dogfood or_patterns in the standard library 2020-04-16 12:44:57 -07:00
Yuki Okushi c2f24a1d94
Remove inline attribute from `into_vec()` 2020-04-17 00:23:44 +09:00
Simon Sapin 7709d205dd Implement `Box::into_raw` based on `Box::leak`
… instead of the other way around.
2020-04-16 17:20:53 +02:00
Matthias Krüger 139c646251 Fix clippy warnings
clippy::{filter_next,single_char_pattern,unit_arg,identity_conversion,nonminimal_bool}
2020-04-15 23:38:48 +02:00
Simon Sapin 9a1c7dba32
Apply suggestions from code review
Co-Authored-By: Ralf Jung <post@ralfj.de>
2020-04-15 18:32:56 +02:00
Ralf Jung 818bef5558 don't specify log base in big-O 2020-04-15 17:07:13 +02:00
Simon Sapin b359fe1eea Deprecate `Rc::into_raw_non_null` and `Arc::into_raw_non_null` 2020-04-15 16:18:33 +02:00
Simon Sapin cdb6bef4fb Deprecate `Box::into_raw_non_null`
Per https://github.com/rust-lang/rust/issues/47336#issuecomment-586589016
2020-04-15 16:18:33 +02:00
Ralf Jung 88612e3657 big-O notation: parenthesis, multiplication and backticks 2020-04-15 14:33:45 +02:00
Dylan DPC 54b160d764
Rollup merge of #71133 - MiSawa:fix-sort-by-key-doc, r=Dylan-DPC
Tighten time complexity on the doc of sort_by_key

Fixes #71132
2020-04-14 23:29:59 +02:00
Dylan DPC e2f24230a2
Rollup merge of #70949 - WaffleLapkin:simlify_vec_macro, r=petrochenkov
simplify `vec!` macro

Simplify `vec!` macro by replacing 2 following branches:
- `($($x:expr),*) => (...)`
- `($($x:expr,)*) => (...)`
with one:
- `($($x:expr),* $(,)?) => (...)`

This is a minor change, however, this will make the documentation cleaner
2020-04-14 23:29:53 +02:00
Ulrik Sverdrup 7612ad7797 Vec drop and truncate: drop using raw slice *mut [T]
By creating a *mut [T] directly (without going through &mut [T]), avoid
questions of validity of the contents of the slice.

Consider the following risky code:

```rust
unsafe {
    let mut v = Vec::<bool>::with_capacity(16);
    v.set_len(16);
}
```

The intention is that with this change, the above snippet will be
sound because Vec::drop does no longer produces a mutable slice of
the vector's contents.
2020-04-14 21:51:48 +02:00
mi_sawa 408dc36980 Tighten time complexity on the doc 2020-04-14 23:51:03 +09:00
Dylan DPC 340b7314e3
Rollup merge of #71121 - AnthonyMikh:fix_string_doc_link, r=Dylan-DPC
Fix broken link in documentation for String::from_utf8
2020-04-14 15:35:32 +02:00
AnthonyMikh 15ba31d010
Fix broken link in documentation for String::from_utf8 2020-04-14 15:07:57 +03:00
Waffle 2c23bd4914 make `vec![,]` uncompilable
Fix regression introduced in commit #3ae2d21
2020-04-14 10:27:55 +03:00
Michal 'vorner' Vaner 80ccddc0ed
weak-into-raw: as_raw -> as_ptr + dangling garbage
* Rename Weak::as_raw to Weak::as_ptr for consistency with some other
  types.
* The as_ptr for a dangling Weak pointer might return whatever garbage
  (and takes that advantage to avoid a conditional).
* Don't guarantee to be able to do `Weak::from_raw(weak.as_ptr())` (even
  though it'll still work fine).
2020-04-13 17:17:34 +02:00
Ivan Tham 7a22cf6415 Add examples to Pattern docs 2020-04-13 22:25:15 +08:00
Ivan Tham cbe96b04ad Add period to Pattern docs 2020-04-13 22:24:58 +08:00
Dylan DPC 03a724bd48
Rollup merge of #70996 - ChaiTRex:master, r=Amanieu
Add or_insert_with_key to Entry of HashMap/BTreeMap

Going along with `or_insert_with`, `or_insert_with_key` provides the `Entry`'s key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time.

This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda.

---

I'm fairly new to Rust, so any optimizations, corrections to types, better names, better documentation, or whatever else would be appreciated. I'd like to thank Arnavion on freenode for helping me to implement a very similar method when I found that `or_insert_with_key` was unavailable.

As a somewhat-related note, this implements https://github.com/rust-lang/rfcs/issues/1202 from 2015, so if this pull request is accepted, that should be closed.
2020-04-11 17:52:11 +02:00
Chai T. Rex db0c39fba5 Change issue number to point to tracking issue 2020-04-11 08:46:12 -04:00
Chai T. Rex 78102377d0 Fixed doc tests for added methods 2020-04-10 13:45:34 -04:00
Chai T. Rex 921579cc3c Add or_insert_with_key to Entry of HashMap/BTreeMap
Going along with or_insert_with, or_insert_with_key provides the
Entry's key to the lambda, avoiding the need to either clone the
key or the need to reimplement this body of this method from
scratch each time.

This is useful when the initial value for a map entry is derived
from the key. For example, the introductory Rust book has an
example Cacher struct that takes an expensive-to-compute lambda and
then can, given an argument to the lambda, produce either the
cached result or execute the lambda.
2020-04-10 12:54:09 -04:00
Mazdak Farrokhzad f699a55e4b
Rollup merge of #70981 - ssomers:btreemap_into_into_iter, r=Mark-Simulacrum
Rearrange BTreeMap::into_iter to match range_mut.

r? @Mark-Simulacrum
I wondered why you catered for the optional root differently in `into_iter` than in `range_mut`.
2020-04-10 18:15:24 +02:00
Mazdak Farrokhzad 277ce9e249
Rollup merge of #70979 - ssomers:btreemap_the_alice_merton_variations, r=Amanieu
Follow up on BTreeMap comments

r? @Amanieu (for the first commit)
2020-04-10 18:15:22 +02:00
Mazdak Farrokhzad 74e93bb8e6
Rollup merge of #70913 - eddyb:rc-arc-diagnostic-items, r=matthewjasper
Replace "rc"/"arc" lang items with Rc/Arc diagnostic items.

`Rc`/`Arc` should have no special semantics, so it seems appropriate for them to not be lang items.

r? @matthewjasper
2020-04-10 12:48:45 +02:00
Mazdak Farrokhzad 81a360fe9a
Rollup merge of #70843 - ssomers:btree_drain_filter_epilogue, r=Amanieu
Remove the Ord bound that was plaguing drain_filter

Now that  #70795 made it superfluous. Also removes superfluous lifetime specifiers (at least I think they are).
2020-04-10 12:48:44 +02:00
Stein Somers 4ade06bab8 Rearrange BTreeMap::into_iter to match range_mut. 2020-04-10 10:58:17 +02:00
Stein Somers de39a4f621 Respect the comment: no root unless the borrow type is `Mut` 2020-04-09 21:37:30 +02:00
Stein Somers 607315b2c3 Kill comment left behind by a last minute change in #70795 2020-04-09 20:16:30 +02:00
Mazdak Farrokhzad 2c3147f018
Rollup merge of #70958 - Amanieu:android_try_reserve, r=Mark-Simulacrum
Disable try_reserve tests on Android

Calling `realloc` with large sizes seems to be broken on older Android versions that use dlmalloc as the default allocator. This is not an issue for modern Android versions that use jemalloc.

Fixes #55861
2020-04-09 18:17:22 +02:00
Amanieu d'Antras 7060a9e683 Disable try_reserve tests on Android 2020-04-09 15:55:12 +01:00
Waffle 3ae2d21c12 simplify `vec!` macro
Simplify `vec!` macro by replacing 2 following branches:
- `($($x:expr),*) => (...)`
- `($($x:expr,)*) => (...)`
with one:
- `($($x:expr),* $(,)?) => (...)`
2020-04-09 11:03:57 +03:00
Dylan DPC 5848209b64
Rollup merge of #70930 - lcnr:patch-1, r=Dylan-DPC
add tracking issue to `VecDeque::make_contiguous`

The tracking issue is https://github.com/rust-lang/rust/issues/70929
2020-04-08 23:33:48 +02:00
Dylan DPC 3cae0e479e
Rollup merge of #70565 - Zoxc:inlines-query-system, r=davidtwco
Add inline attributes for functions used in the query system
2020-04-08 23:33:40 +02:00
Dylan DPC b9bb12640e
Rollup merge of #70850 - ssomers:btreemap_first_last, r=Amanieu
BTreeMap first last proposal tweaks

Clean-up and following up on a request in #62924.

Trying the reviewer of the original code #65637...
r? @scottmcm
2020-04-08 18:37:22 +02:00
Bastian Kauschke 839d229550
add `VecDeque::make_contiguous` tracking issue 2020-04-08 16:53:04 +02:00
Eduard-Mihai Burtescu 9d13520a6b Replace "rc"/"arc" lang items with Rc/Arc diagnostic items. 2020-04-08 10:47:41 +03:00
Dylan DPC 89d661f15d
Rollup merge of #70857 - faern:use-assoc-int-float-consts, r=dtolnay
Don't import integer and float modules, use assoc consts 2

Follow up to #70777. I missed quite a lot of places. Partially because I wanted to keep the size of the last PR down, and partially because my regexes were not good enough :)

r? @dtolnay
2020-04-07 14:46:59 +02:00
Josh Stone e8339e820b Use split_at in slice's ToOwned::clone_into
It appears to codegen slightly more efficiently with `split_at` taking
two slices at once, rather than slicing across different calls.
2020-04-06 15:50:59 -07:00
Linus Färnstrand e4fc04b6df Use usize::MAX as assoc const in liballoc 2020-04-06 23:07:38 +02:00
Linus Färnstrand 65e10e3436 Use assoc const f32::NAN in liballoc 2020-04-06 22:44:04 +02:00
Stein Somers 8212b9772e BTreeMap first/last: add pop methods 2020-04-06 19:56:29 +02:00
Stein Somers c23ee767d9 BTreeMap first/last: make examples more to the point 2020-04-06 19:03:18 +02:00