Commit Graph

701 Commits

Author SHA1 Message Date
Waffle
d5c221107e add Vec::extend_from_within method
Implement <https://github.com/rust-lang/rfcs/pull/2714>, changes from the RFC:
- Rename the method `append_from_within` => `extend_from_within`
- Loose :Copy bound => :Clone
- Specialize in case of :Copy

This commit also adds `Vec::split_at_spare` private method and use it to implement
`Vec::spare_capacity_mut` and `Vec::extend_from_within`. This method returns 2
slices - initialized elements (same as `&mut vec[..]`) and uninitialized but
allocated space (same as `vec.spare_capacity_mut()`).
2021-01-31 22:30:19 +03:00
Mara Bos
dd86fc6228
Rollup merge of #81069 - ogoffart:rc_new_cyclic_doc, r=Mark-Simulacrum
Add sample code for Rc::new_cyclic
2021-01-16 17:30:15 +00:00
Mara Bos
5702cfa255
Rollup merge of #80764 - CAD97:weak-unsized-as-ptr-again, r=RalfJung
Re-stabilize Weak::as_ptr and friends for unsized T

As per [T-lang consensus](https://hackmd.io/7r3_is6uTz-163fsOV8Vfg), this uses a branch to handle the dangling case. The discussed optimization of only doing the branch in the T: ?Sized case is left for a followup patch, as doing so is not trivial (as it requires specialization) and not _obviously_ better (as it requires using `wrapping_offset` rather than `offset` more).

<details><summary>Basically said optimization</summary>

Specialize on `T: Sized`:

```rust
fn as_ptr(&self) -> *const T {
    if [ T is Sized ] || !is_dangling(ptr) {
        (ptr as *mut T).set_ptr_value( (ptr as *mut u8).wrapping_offset(data_offset) )
    } else {
        ptr::null()
    }
}

fn from_raw(*const T) -> Self {
    if [ T is Sized ] || !ptr.is_null() {
        let ptr = (ptr as *mut RcBox).set_ptr_value( (ptr as *mut u8).wrapping_offset(-data_offset) );
        Weak { ptr }
    } else {
        Weak::new()
    }
}
```

(but with more `set_ptr_value` to avoid `Sized` restrictions and maintain metadata.)

Written in this fashion, this is not a correctness-critical specialization (i.e. so long as `[ T is Sized ]` is false for unsized `T`, it can be `rand()` for sized `T` without breaking correctness), but it's still touchy, so I'd rather do it in another PR with separate review.

---
</details>

This effectively reverts #80422 and re-establishes #74160. T-libs [previously signed off](https://github.com/rust-lang/rust/pull/74160#issuecomment-660539373) on this stable API change in #74160.
2021-01-16 17:29:56 +00:00
Mara Bos
40d2506cab
Rollup merge of #80681 - ChrisJefferson:logic-error-doc, r=m-ou-se
Clarify what the effects of a 'logic error' are

This clarifies what a 'logic error' is (which is a term used to describe what happens if you put things in a hash table or btree and then use something like a refcell to break the internal ordering). This tries to be as vague as possible, as we don't really want to promise what happens, except "bad things, but not UB". This was discussed in #80657
2021-01-16 17:29:53 +00:00
bors
410a546fc5 Auto merge of #77435 - hanmertens:binary_heap_append, r=scottmcm
Always use extend in BinaryHeap::append

This is faster, see #77433.

Fixes #77433
2021-01-16 11:10:13 +00:00
Chris Jefferson
78d919280d Clarify what the effects of a 'logic error' are 2021-01-16 09:36:28 +00:00
Olivier Goffart
9952632a2f Add sample code for Rc::new_cyclic 2021-01-16 10:29:21 +01:00
bors
efdb859dcd Auto merge of #80873 - ssomers:btree_cleanup_slices_4, r=Mark-Simulacrum
BTreeMap: tougher checks on code using raw into_kv_pointers

r? `@Mark-Simulacrum`
2021-01-16 07:12:12 +00:00
Han Mertens
32a20f4433 Change rebuild heuristic in BinaryHeap::append
See #77433 for why the new heuristic was chosen.

Fixes #77433
2021-01-15 21:50:05 +01:00
Yuki Okushi
1b8fd02daa
Rollup merge of #80834 - bugadani:vecdeque, r=oli-obk
Remove unreachable panics from VecDeque::{front/back}[_mut]

`VecDeque`'s `front`, `front_mut`, `back` and `back_mut` methods are implemented in terms of the index operator, which causes these functions to contain [unreachable panic calls](https://rust.godbolt.org/z/MTnq1o).

This PR reimplements these methods in terms of `get[_mut]` instead.
2021-01-15 18:26:11 +09:00
Dániel Buga
744f885e2a Remove unreachable panics from VecDeque 2021-01-14 19:31:56 +01:00
Mara Bos
9bfe6f1b2c
Rollup merge of #80972 - KodrAus:deprecate/remove_item, r=nagisa
Remove unstable deprecated Vec::remove_item

Closes #40062

The `Vec::remove_item` method was deprecated in `1.46.0` (in August of 2020). This PR now removes that unstable method entirely.
2021-01-14 18:00:18 +00:00
Mara Bos
7855a730b9
Rollup merge of #80966 - KodrAus:deprecate/spin_loop_hint, r=m-ou-se
Deprecate atomic::spin_loop_hint in favour of hint::spin_loop

For https://github.com/rust-lang/rust/issues/55002

We wanted to leave `atomic::spin_loop_hint` alone when stabilizing `hint::spin_loop` so folks had some time to migrate. This now deprecates `atomic_spin_loop_hint`.
2021-01-14 18:00:14 +00:00
Christopher Durham
c14e919f1e
Apply suggestions from code review
Co-authored-by: Ralf Jung <post@ralfj.de>
2021-01-13 17:21:23 -05:00
bors
116d1a7056 Auto merge of #80824 - cuviper:heap-clones, r=kennytm
Try to avoid locals when cloning into Box/Rc/Arc

For generic `T: Clone`, we can allocate an uninitialized box beforehand,
which gives the optimizer a chance to create the clone directly in the
heap. For `T: Copy`, we can go further and do a simple memory copy,
regardless of optimization level.

The same applies to `Rc`/`Arc::make_mut` when they must clone the data.
2021-01-13 11:11:34 +00:00
bors
9f3998b4aa Auto merge of #77858 - ijackson:split-inclusive, r=KodrAus
Stabilize split_inclusive

### Contents of this MR

This stabilises:

 * `slice::split_inclusive`
 * `slice::split_inclusive_mut`
 * `str::split_inclusive`

Closes #72360.

### A possible concern

The proliferation of `split_*` methods is not particularly pretty.  The existence of `split_inclusive` seems to invite the addition of `rsplit_inclusive`, `splitn_inclusive`, etc.  We could instead have a more general API, along these kinds of lines maybe:
```
   pub fn split_generic('a,P,H>(&'a self, pat: P, how: H) -> ...
       where P: Pattern
       where H: SplitHow;

   pub fn split_generic_mut('a,P,H>(&'a mut self, pat: P, how: H) -> ...
       where P: Pattern
       where H: SplitHow;

   trait SplitHow {
       fn reverse(&self) -> bool;
       fn inclusive -> bool;
       fn limit(&self) -> Option<usize>;
   }

   pub struct SplitFwd;
   ...
   pub struct SplitRevInclN(pub usize);
```
But maybe that is worse.

### Let us defer that? ###

This seems like a can of worms.  I think we can defer opening it now; if and when we have something more general, these two methods can become convenience aliases.  But I thought I would mention it so the lang API team can consider it and have an opinion.
2021-01-13 07:38:58 +00:00
Ashley Mannix
d65cb6ebce deprecate atomic::spin_loop_hint in favour of hint::spin_loop 2021-01-13 16:30:29 +10:00
Ashley Mannix
7e83fece91 remove unstable deprecated Vec::remove_item 2021-01-13 15:14:11 +10:00
Josh Stone
1f1a3b4857 move WriteCloneIntoRaw into alloc::alloc 2021-01-12 12:24:28 -08:00
Josh Stone
f89f30fb2c Move directly when Rc/Arc::make_mut splits from Weak
When only other `Weak` references remain, we can directly move the data
into the new unique allocation as a plain memory copy.
2021-01-11 17:46:49 -08:00
Josh Stone
d85df44e8d Specialize Rc/Arc::make_mut clones to try to avoid locals
As we did with `Box`, we can allocate an uninitialized `Rc` or `Arc`
beforehand, giving the optimizer a chance to skip the local value for
regular clones, or avoid any local altogether for `T: Copy`.
2021-01-11 17:43:10 -08:00
Josh Stone
9aa7dd1e6a Specialize Box clones to try to avoid locals
For generic `T: Clone`, we can allocate an uninitialized box beforehand,
which gives the optimizer a chance to create the clone directly in the
heap. For `T: Copy`, we can go further and do a simple memory copy,
regardless of optimization level.
2021-01-11 17:43:10 -08:00
CAD97
b5b6760c03 Weak::into_raw shouldn't translate sentinel value 2021-01-10 23:27:32 -05:00
Yuki Okushi
39e1331cfa Add another test case for #79808
Taken from #80293.
2021-01-11 12:10:16 +09:00
Stein Somers
c1dfb4a9c4 BTreeMap: tougher checks on code using raw into_kv_pointers 2021-01-10 14:40:21 +01:00
bors
fd34606ddf Auto merge of #80391 - ssomers:btree_cleanup_slices_3, r=Mark-Simulacrum
BTreeMap: tougher checking on most uses of copy_nonoverlapping

Miri checks pointer provenance and destination, but we can check it in debug builds already.
Also, we can let Miri confirm we don't mistake imprints of moved keys and values as genuine.
r? `@Mark-Simulacrum`
2021-01-10 10:48:55 +00:00
Camelid
befd153098
Add comment to Vec::truncate explaining > vs >=
Hopefully this will prevent people from continuing to ask about this
over and over again :)

See [this Zulip discussion][1] for more.

[1]: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation
2021-01-09 12:35:47 -08:00
CAD97
747dbcb325 Provide reasoning for rc data_offset safety 2021-01-09 14:32:55 -05:00
Stein Somers
26b94626a1 BTreeMap: tougher checks on most uses of copy_nonoverlapping 2021-01-08 19:58:05 +01:00
CAD97
4901c55af7 Replace set_data_ptr with pointer::set_ptr_value 2021-01-07 13:40:57 -05:00
CAD97
1e578c9fb0 Reclarify Weak<->raw pointer safety comments 2021-01-07 12:53:04 -05:00
CAD97
b10b9e25ff Remove "pointer describes" terminology 2021-01-07 12:41:58 -05:00
CAD97
f00b458903 Tighten/clarify documentation of rc data_offset 2021-01-07 12:32:42 -05:00
bors
b5c496de37 Auto merge of #79863 - JohnTitor:compiler-builtins, r=bjorn3
Update `compiler_builtins` to 0.1.39

This version contains the fixes of https://github.com/rust-lang/compiler-builtins/issues/390 and https://github.com/rust-lang/compiler-builtins/issues/391.
Also, rename features following https://github.com/rust-lang/compiler-builtins/pull/386.
2021-01-07 11:22:42 +00:00
Yuki Okushi
6275a29dbe Update compiler_builtins to 0.1.39 2021-01-07 16:16:36 +09:00
CAD97
6bc772cdc0 Re-stabilize Weak::as_ptr &friends for unsized T
As per T-lang consensus, this uses a branch to handle the dangling case.
The discussed optimization of only doing the branch in the T: ?Sized
case is left for a followup patch, as doing so is not trivial
(as it requires specialization for correctness, not just optimization).
2021-01-06 19:30:22 -05:00
Yuki Okushi
cda26a6b15
Rollup merge of #80666 - jjlin:master, r=Dylan-DPC
Fix missing link for "fully qualified syntax"

This issue can currently be seen at https://doc.rust-lang.org/stable/std/rc/index.html#toggle-all-docs:~:text=%5B-,fully%20qualified%20syntax

It originates from #76138, where the link was added to `library/alloc/src/sync.rs`, but not `library/alloc/src/rc.rs`.
2021-01-05 09:52:47 +09:00
Yuki Okushi
bdf8bbde1d
Rollup merge of #80442 - steffahn:mention_arc_in_cow, r=Mark-Simulacrum
Mention Arc::make_mut and Rc::make_mut in the documentation of Cow

Following this discussion: https://users.rust-lang.org/t/should-the-cow-documentation-mention-arc/53341

_Rendered (the last paragraph is new):_

![Screenshot_20201228_171551](https://user-images.githubusercontent.com/3986214/103228135-5d72e200-4930-11eb-89e1-38b5c86b08c7.png)

`@rustbot` modify labels: T-doc, T-libs
2021-01-05 09:52:33 +09:00
Ian Jackson
be226e49e4 Stabilize split_inclusive
Closes #72360.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-01-04 16:20:08 +00:00
Jeremy Lin
6d45d055a1 Fix missing link for "fully qualified syntax" 2021-01-03 13:59:02 -08:00
Guillaume Gomez
2072e11730
Rollup merge of #80591 - lcnr:incomplete-features, r=RalfJung
remove allow(incomplete_features) from std

cc https://github.com/rust-lang/rust/pull/80349#issuecomment-753357123

> Now I am somewhat concerned that the standard library uses some of these features...

I think it is theoretically ok to use incomplete features in the standard library or the compiler if we know that there is an already working subset and we explicitly document what we have to be careful about. Though at that point it is probably better to try and split the incomplete feature into two separate ones, similar to `min_specialization`.

Will be interesting once `feature(const_evaluatable_checked)` works well enough to imo be used in the compiler but not yet well enough to be removed from `INCOMPLETE_FEATURES`.

r? `@RalfJung`
2021-01-03 17:09:08 +01:00
bors
0876f59b97 Auto merge of #77832 - camelid:remove-manual-link-resolves, r=jyn514
Remove many unnecessary manual link resolves from library

Now that #76934 has merged, we can remove a lot of these! E.g, this is
no longer necessary:

    [`Vec<T>`]: Vec

cc `@jyn514`
2021-01-02 01:31:03 +00:00
Bastian Kauschke
6cf47ff4f0 remove incomplete features from std 2021-01-01 19:57:10 +01:00
bors
18d27b2c94 Auto merge of #80310 - Manishearth:box-try-alloc, r=kennytm
Add fallible Box, Arc, and Rc allocator APIs

cc https://github.com/rust-lang/rust/issues/48043

It was suggested in https://github.com/rust-lang/rust/issues/48043#issuecomment-748008486 that `Box::try_*` follows the spirit of RFC 2116. This PR is an attempt to add the relevant APIs, tied to the same feature gate. Happy to make any changes or turn this into an RFC if necessary.

cc `@rust-lang/wg-allocators`
2021-01-01 10:29:43 +00:00
Camelid
0506789014 Remove many unnecessary manual link resolves from library
Now that #76934 has merged, we can remove a lot of these! E.g, this is
no longer necessary:

    [`Vec<T>`]: Vec
2020-12-31 11:54:32 -08:00
Manish Goregaokar
375e7c5864 More inline, doc fixes 2020-12-31 16:49:44 +00:00
Manish Goregaokar
8f3cb7d75d Make [A]Rc::allocate_for_layout() use try_allocate_for_layout() 2020-12-31 16:36:28 +00:00
bors
b33e234155 Auto merge of #79895 - Kerollmops:slice-group-by, r=m-ou-se
The return of the GroupBy and GroupByMut iterators on slice

According to https://github.com/rust-lang/rfcs/pull/2477#issuecomment-742034372, I am opening this PR again, this time I implemented it in safe Rust only, it is therefore much easier to read and is completely safe.

This PR proposes to add two new methods to the slice, the `group_by` and `group_by_mut`. These two methods provide a way to iterate over non-overlapping sub-slices of a base slice that are separated by the predicate given by the user (e.g. `Partial::eq`, `|a, b| a.abs() < b.abs()`).

```rust
let slice = &[1, 1, 1, 3, 3, 2, 2, 2];

let mut iter = slice.group_by(|a, b| a == b);
assert_eq!(iter.next(), Some(&[1, 1, 1][..]));
assert_eq!(iter.next(), Some(&[3, 3][..]));
assert_eq!(iter.next(), Some(&[2, 2, 2][..]));
assert_eq!(iter.next(), None);
```

[An RFC](https://github.com/rust-lang/rfcs/pull/2477) was open 2 years ago but wasn't necessary.
2020-12-31 12:00:43 +00:00
Clément Renault
8b53be6604
Replace the tracking issue for the slice_group_by feature 2020-12-31 12:13:03 +01:00
Manish Goregaokar
589aa8e29c Reuse Box::try_new_*_in() in Box::new_*_in() 2020-12-31 08:43:30 +00:00