Commit graph

5601 commits

Author SHA1 Message Date
Matthias Krüger
dfed1a6c07
Rollup merge of #89898 - Amanieu:remove_alloc_prelude, r=joshtriplett
Remove alloc::prelude

As per the libs team decision in #58935.

Closes #58935
2021-10-16 08:02:21 +02:00
Matthias Krüger
9ae0804859
Rollup merge of #89509 - jhpratt:stabilize-const_unreachable_unchecked, r=oli-obk
Stabilize `unreachable_unchecked` as `const fn`

Closes #53188

This PR stabilizes `core::hint::unreachable_unchecked` as `const fn`. MIRI is able to detect when this method is called. Stabilization was delayed until `const_panic` was stabilized so as to avoid users calling this method in its place (thus resulting in runtime UB). With #89508, that is no longer an issue.

````@rustbot```` label +A-const-eval +A-const-fn +T-lang +S-blocked

(not sure why it's T-lang, but that's what the tracking issue is)
2021-10-16 08:02:20 +02:00
bors
6cc0a764e0 Auto merge of #85379 - mdaverde:uds-abstract, r=joshtriplett
Add abstract namespace support for Unix domain sockets

Hello! The other day I wanted to mess around with UDS in Rust and found that abstract namespaces ([unix(7)](https://man7.org/linux/man-pages/man7/unix.7.html)) on Linux still needed development. I took the approach of adding `_addr` specific public functions to reduce conflicts.

Feature name: `unix_socket_abstract`
Tracking issue: #85410
Further context: #42048

## Non-platform specific additions

`UnixListener::bind_addr(&SocketAddr) -> Result<UnixListener>`

`UnixStream::connect_addr(&SocketAddr) -> Result<()>`

`UnixDatagram::bind_addr(&SocketAddr) -> Result<UnixDatagram>`

`UnixDatagram::connect_addr(&SocketAddr) -> Result<()>`

`UnixDatagram::send_to_addr(&self, &[u8], &SocketAddr) -> Result<usize>`

## Platform-specific (Linux) additions

`SocketAddr::from_abstract_namespace(&[u8]) -> SocketAddr`

`SockerAddr::as_abstract_namespace() -> Option<&[u8]>`

## Example

```rust
#![feature(unix_socket_abstract)]
use std::os::unix::net::{UnixListener, SocketAddr};

fn main() -> std::io::Result<()> {
    let addr = SocketAddr::from_abstract_namespace(b"namespace")?; // Linux only
    let listener = match UnixListener::bind_addr(&addr) {
        Ok(sock) => sock,
        Err(err) => {
            println!("Couldn't bind: {:?}", err);
            return Err(err);
        }
    };
    Ok(())
}
```

## Further Details

The main inspiration for the implementation came from the [nix-rust](https://github.com/nix-rust/nix/blob/master/src/sys/socket/addr.rs#L558) crate but there are also other [historical](c4db0685b1) [attempts](https://github.com/tormol/uds/blob/master/src/addr.rs#L324) with similar approaches.

A comment I did have was with this change, we now allow a `SocketAddr` to be constructed explicitly rather than just used almost as a handle for the return of `peer_addr` and `local_addr`. We could consider adding other explicit constructors (e.g. `SocketAddr::from_pathname`, `SockerAddr::from_unnamed`).

Cheers!
2021-10-15 22:31:53 +00:00
bors
c1026539bd Auto merge of #84096 - m-ou-se:windows-bcrypt-random, r=dtolnay
Use BCryptGenRandom instead of RtlGenRandom on Windows.

This removes usage of RtlGenRandom on Windows, in favour of BCryptGenRandom.

BCryptGenRandom isn't available on XP, but we dropped XP support a while ago.
2021-10-15 19:03:57 +00:00
bors
265fef45f2 Auto merge of #89337 - mbrubeck:vec-leak, r=m-ou-se
Avoid allocations and copying in Vec::leak

The [`Vec::leak`] method (#62195) is currently implemented by calling `Vec::into_boxed_slice` and `Box::leak`.  This shrinks the vector before leaking it, which potentially causes a reallocation and copies the vector's contents.

By avoiding the conversion to `Box`, we can instead leak the vector without any expensive operations, just by returning a slice reference and forgetting the `Vec`.  Users who *want* to shrink the vector first can still do so by calling `shrink_to_fit` explicitly.

**Note:**  This could break code that uses `Box::from_raw` to “un-leak” the slice returned by `Vec::leak`.  However, the `Vec::leak` docs explicitly forbid this, so such code is already incorrect.

[`Vec::leak`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.leak
2021-10-15 15:55:08 +00:00
bors
af9b508e1d Auto merge of #88717 - tabokie:vecdeque-fast-append, r=m-ou-se
Optimize VecDeque::append

Optimize `VecDeque::append` to do unsafe copy rather than iterating through each element.

On my `Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz`, the benchmark shows 37% improvements:
```
Master:
custom-bench vec_deque_append 583164 ns/iter
custom-bench vec_deque_append 550040 ns/iter

Patched:
custom-bench vec_deque_append 349204 ns/iter
custom-bench vec_deque_append 368164 ns/iter
```

Additional notes on the context: this is the third attempt to implement a non-trivial version of `VecDeque::append`, the last two are reverted due to unsoundness or regression, see:
- https://github.com/rust-lang/rust/pull/52553, reverted in https://github.com/rust-lang/rust/pull/53571
- https://github.com/rust-lang/rust/pull/53564, reverted in https://github.com/rust-lang/rust/pull/54851

Both cases are covered by existing tests.

Signed-off-by: tabokie <xy.tao@outlook.com>
2021-10-15 12:51:31 +00:00
Mara Bos
1ed123828c Use BCryptGenRandom instead of RtlGenRandom on Windows.
BCryptGenRandom isn't available on XP, but we dropped XP support a while
ago.
2021-10-15 13:22:28 +02:00
bors
1dafe6d1c3 Auto merge of #88540 - ibraheemdev:swap-unchecked, r=kennytm
add `slice::swap_unchecked`

An unsafe version of `slice::swap` that does not do bounds checking.
2021-10-15 09:35:45 +00:00
Amanieu d'Antras
8007dfa3b2 Remove alloc::prelude
As per the libs team decision in #58935.

Closes #58935
2021-10-15 01:41:31 +02:00
Matthias Krüger
d6eff5ac4c
Rollup merge of #89878 - GuillaumeGomez:add-missing-cfg-hide, r=notriddle
Fix missing remaining compiler specific cfg information

Follow-up of #89596. We forgot a few of them:

![Screenshot from 2021-10-14 11-36-44](https://user-images.githubusercontent.com/3050060/137292700-64ebc59f-d9d2-41f2-be3a-fa5bf211523c.png)
![Screenshot from 2021-10-14 11-36-56](https://user-images.githubusercontent.com/3050060/137292703-f63fa4e5-2c56-446b-9f86-3652f03dfe59.png)

r? `@notriddle`
2021-10-14 16:06:47 +02:00
Matthias Krüger
686857f5bf
Rollup merge of #89873 - askoufis:patch-1, r=Mark-Simulacrum
Add missing word to `FromStr` trait documentation

The doc comment is getting a bit wide, let me know if I should restructure it/add a new line.
2021-10-14 16:06:46 +02:00
Matthias Krüger
d177791791
Rollup merge of #89433 - arlosi:stdin-fix, r=joshtriplett
Fix ctrl-c causing reads of stdin to return empty on Windows.

Pressing ctrl+c (or ctrl+break) on Windows caused a blocking read of stdin to unblock and return empty, unlike other platforms which continue to block.

On ctrl-c, `ReadConsoleW` will return success, but also set `LastError` to `ERROR_OPERATION_ABORTED`.

This change detects this case, and re-tries the call to `ReadConsoleW`.

Fixes #89177. See issue for further details.

Tested on Windows 7 and Windows 10 with both MSVC and GNU toolchains
2021-10-14 16:06:44 +02:00
Ibraheem Ahmed
cf12732a38
don't duplicate slice panic_bounds_check 2021-10-14 09:31:34 -04:00
bors
c34ac8747c Auto merge of #89247 - fee1-dead:const-eval-select, r=oli-obk
Add `const_eval_select` intrinsic

Adds an intrinsic that calls a given function when evaluated at compiler time, but generates a call to another function when called at runtime.

See https://github.com/rust-lang/const-eval/issues/7 for previous discussion.

r? `@oli-obk.`
2021-10-14 10:06:30 +00:00
Guillaume Gomez
30a20f8c83 Fix missing remaining compiler specific cfg information 2021-10-14 11:39:30 +02:00
Deadbeef
26b78ccd31
Fix const stability 2021-10-14 07:07:34 +00:00
Deadbeef
6770dbd4b5
Avoid tupling at the callee 2021-10-14 06:18:53 +00:00
Adam Skoufis
4b59b35b76
Add missing word to FromStr trait docs 2021-10-14 13:47:54 +11:00
Matthias Krüger
06110c0c46
Rollup merge of #89670 - yoshuawuyts:available-parallelism-docs, r=joshtriplett
Improve `std:🧵:available_parallelism` docs

_Tracking issue: https://github.com/rust-lang/rust/issues/74479_

This PR reworks the documentation of `std:🧵:available_parallelism`, as requested [here](https://github.com/rust-lang/rust/pull/89324#issuecomment-934343254).

## Changes

The following changes are made:

- We've removed prior mentions of "hardware threads" and instead centers the docs around "parallelism" as a resource available to a program.
- We now provide examples of when `available_parallelism` may return numbers that differ from the number of CPU cores in the host machine.
- We now mention that the amount of available parallelism may change over time.
- We make note of which platform components we don't take into account which more advanced users may want to take note of.
- The example has been updated, which should be a bit easier to use.
- We've added a docs alias to `num-cpus` which provides similar functionality to `available_parallelism`, and is one of the most popular crates on crates.io.

---

Thanks!

r? `@BurntSushi`
2021-10-13 22:51:01 +02:00
Yoshua Wuyts
21429eda2d Improve std:🧵:available_parallelism docs 2021-10-13 17:57:05 +02:00
Yuki Okushi
59ebfdd7e0
Rollup merge of #89817 - m-ou-se:int-log-10-inline, r=the8472
Add #[inline] to int log10 functions.
2021-10-13 21:55:16 +09:00
Yuki Okushi
06e4aee220
Rollup merge of #89814 - jkugelman:must-use-string-transforms-typo, r=joshtriplett
Fix uppercase/lowercase error

Fix https://github.com/rust-lang/rust/pull/89694#discussion_r726829890

r? ````@joshtriplett````
2021-10-13 21:55:14 +09:00
Yuki Okushi
c1bde6e4b6
Rollup merge of #89794 - jkugelman:must-use-to_value-conversions, r=joshtriplett
Add #[must_use] to to_value conversions

`NonNull<T>::cast` snuck in when I wasn't looking. What a scamp!

Parent issue: #89692

r? ````@joshtriplett````
2021-10-13 21:55:13 +09:00
Yuki Okushi
f759fff447
Rollup merge of #89781 - Wilfred:patch-2, r=JohnTitor
Add missing words in `Infallible` docs

This sentence was previously incomplete.
2021-10-13 21:55:11 +09:00
bors
ef4b3069ba Auto merge of #89774 - the8472:inline-mut-iter-next, r=m-ou-se
inline next() on &mut Iterator impl

In [#87431](https://github.com/rust-lang/rust/pull/87431/files#diff-79a6b417b85ecf4f1a4ef2235135fedf540199caf6e9e1d154ac6a413b40a757R132-R136)   I found that `(&mut range).fold` doesn't optimize well because the default impl for for `fold` on `&mut Iterator` doesn't inline `next`. In that particular case it was worked around by using `try_fold` which takes a `&mut self` instead of `self`.

Let's see if this can be fixed more broadly.
2021-10-12 23:59:48 +00:00
Mara Bos
a6bb1fb641 Add #[inline] to int log10 functions. 2021-10-12 15:21:14 +02:00
Mara Bos
df15b289f3 Remove potentially unsound note on reconstructing a leaked Vec. 2021-10-12 15:02:52 +02:00
John Kugelman
81eeb3e775 Fix uppercase/lowercase error 2021-10-12 08:53:54 -04:00
the8472
7017410e5d
Rollup merge of #89799 - ast-ral:ready-method-spellck, r=joshtriplett
fix minor spelling error in Poll::ready docs

Fixes minor spelling error in the proposed `Poll::ready` docs. Not that my opinion matters, but +1 on the original PR (#89651), it reads much nicer to me than the `ready!` macro.
2021-10-12 14:53:11 +02:00
the8472
4cf0f1fede
Rollup merge of #89797 - jkugelman:must-use-is_condition-tests, r=joshtriplett
Add #[must_use] to is_condition tests

I threw in `std::path::Path::has_root` for funsies.

A continuation of #89718.

Parent issue: #89692

r? ```@joshtriplett```
2021-10-12 14:53:11 +02:00
the8472
a1bdd48106
Rollup merge of #89796 - jkugelman:must-use-non-mutating-verb-methods, r=joshtriplett
Add #[must_use] to non-mutating verb methods

These are methods that could be misconstrued to mutate their input, similar to #89694. I gave each one a different custom message.

I wrote that `upgrade` and `downgrade` don't modify the input pointers. Logically they don't, but technically they do...

Parent issue: #89692

r? ```@joshtriplett```
2021-10-12 14:53:10 +02:00
the8472
b55a3c5d15
Rollup merge of #89778 - jkugelman:must-use-as_type-conversions, r=joshtriplett
Add #[must_use] to as_type conversions

Clippy missed these:

```rust
alloc::string::String   fn as_mut_str(&mut self) -> &mut str;
core::mem::NonNull<T>   unsafe fn as_uninit_mut<'a>(&mut self) -> &'a MaybeUninit<T>;
str                     unsafe fn as_bytes_mut(&mut self) -> &mut [u8];
str                     fn as_mut_ptr(&mut self) -> *mut u8;
```

Parent issue: #89692

r? ````@joshtriplett````
2021-10-12 14:53:08 +02:00
Mara Bos
129af049fe Mention Rust version in Vec::leak docs. 2021-10-12 14:50:46 +02:00
bors
9475e609b8 Auto merge of #89770 - jkugelman:must-use-from-and-into, r=joshtriplett
Add #[must_use] to From::from and Into::into

Risk of churn: **High**
Magic 8-Ball says: **Outlook not so good**

I figured I'd put this out there. If we don't do it now maybe we save it for a rainy day.

Parent issue: #89692

r? `@joshtriplett`
2021-10-12 09:43:37 +00:00
bors
02f2b31e61 Auto merge of #89769 - jkugelman:must-use-maybe-uninit-new, r=joshtriplett
Add #[must_use] to MaybeUninit::new

As discussed in https://github.com/rust-lang/rust/pull/89729#issuecomment-939775659.

Parent issue: #89692

r? `@joshtriplett`
2021-10-12 07:02:53 +00:00
Deadbeef
5387b6542f
Add const_eval_select intrinsic 2021-10-12 05:42:23 +00:00
ast-ral
5100630dcd
fix minor spelling error in Poll::ready docs 2021-10-11 21:00:02 -07:00
bors
ffdf18d144 Auto merge of #88788 - falk-hueffner:speedup-int-log10-branchless, r=joshtriplett
Speedup int log10 branchless

This is achieved with a branchless bit-twiddling implementation of the case x < 100_000, and using this as building block.

Benchmark on an Intel i7-8700K (Coffee Lake):

```
name                                   old ns/iter  new ns/iter  diff ns/iter   diff %  speedup
num::int_log::u8_log10_predictable     165          169                     4    2.42%   x 0.98
num::int_log::u8_log10_random          438          423                   -15   -3.42%   x 1.04
num::int_log::u8_log10_random_small    438          423                   -15   -3.42%   x 1.04
num::int_log::u16_log10_predictable    633          417                  -216  -34.12%   x 1.52
num::int_log::u16_log10_random         908          471                  -437  -48.13%   x 1.93
num::int_log::u16_log10_random_small   945          471                  -474  -50.16%   x 2.01
num::int_log::u32_log10_predictable    1,496        1,340                -156  -10.43%   x 1.12
num::int_log::u32_log10_random         1,076        873                  -203  -18.87%   x 1.23
num::int_log::u32_log10_random_small   1,145        874                  -271  -23.67%   x 1.31
num::int_log::u64_log10_predictable    4,005        3,171                -834  -20.82%   x 1.26
num::int_log::u64_log10_random         1,247        1,021                -226  -18.12%   x 1.22
num::int_log::u64_log10_random_small   1,265        921                  -344  -27.19%   x 1.37
num::int_log::u128_log10_predictable   39,667       39,579                -88   -0.22%   x 1.00
num::int_log::u128_log10_random        6,456        6,696                 240    3.72%   x 0.96
num::int_log::u128_log10_random_small  4,108        3,903                -205   -4.99%   x 1.05
```

Benchmark on an M1 Mac Mini:

```
name                                   old ns/iter  new ns/iter  diff ns/iter   diff %  speedup
num::int_log::u8_log10_predictable     143          130                   -13   -9.09%   x 1.10
num::int_log::u8_log10_random          375          325                   -50  -13.33%   x 1.15
num::int_log::u8_log10_random_small    376          325                   -51  -13.56%   x 1.16
num::int_log::u16_log10_predictable    500          322                  -178  -35.60%   x 1.55
num::int_log::u16_log10_random         794          405                  -389  -48.99%   x 1.96
num::int_log::u16_log10_random_small   1,035        405                  -630  -60.87%   x 2.56
num::int_log::u32_log10_predictable    1,144        894                  -250  -21.85%   x 1.28
num::int_log::u32_log10_random         832          786                   -46   -5.53%   x 1.06
num::int_log::u32_log10_random_small   832          787                   -45   -5.41%   x 1.06
num::int_log::u64_log10_predictable    2,681        2,057                -624  -23.27%   x 1.30
num::int_log::u64_log10_random         1,015        806                  -209  -20.59%   x 1.26
num::int_log::u64_log10_random_small   1,004        795                  -209  -20.82%   x 1.26
num::int_log::u128_log10_predictable   56,825       56,526               -299   -0.53%   x 1.01
num::int_log::u128_log10_random        9,056        8,861                -195   -2.15%   x 1.02
num::int_log::u128_log10_random_small  1,528        1,527                  -1   -0.07%   x 1.00
```

The 128 bit case remains ridiculously slow because llvm fails to optimize division by a constant 128-bit value to multiplications. This could be worked around but it seems preferable to fix this in llvm.

From u32 up, table lookup (like suggested [here](https://github.com/rust-lang/rust/issues/70887#issuecomment-881099813)) is still faster, but requires a hardware `leading_zeros` to be viable, and might clog up the cache.
2021-10-12 03:18:54 +00:00
John Kugelman
c3f0577002 Add #[must_use] to non-mutating verb methods 2021-10-11 21:21:32 -04:00
John Kugelman
01b439e764 Add #[must_use] to is_condition tests
A continuation of #89718.
2021-10-11 21:15:57 -04:00
John Kugelman
0cf84c8c19 Add #[must_use] to to_value conversions 2021-10-11 19:37:16 -04:00
John Kugelman
f9692b5619 Add #[must_use] to From::from and Into::into 2021-10-11 18:10:30 -04:00
Matthias Krüger
f94a325496
Rollup merge of #89785 - nbdd0121:master, r=Mark-Simulacrum
Fix ICE when compiling nightly std/rustc on beta compiler

Fix #89775

#89479 renames a lot of diagnostic items, but it happens that the beta compiler assumes that there must be DefId with `rustc_diagnostic_item = "send_trait"`, causing an ICE when compiling stage 0 std or stage 1 compiler. So gate it with `cfg(bootstrap)`.

The unwrap is also removed, so that existence of the diagnostic item is not required. I ripgreped the code base and this seems the only place where `unwrap` is called on the return value of `get_diagnostic_item`.
2021-10-11 23:45:53 +02:00
Matthias Krüger
d3984e16bf
Rollup merge of #89651 - ibraheemdev:poll-ready, r=dtolnay
Add `Poll::ready` and revert stabilization of `task::ready!`

This PR adds an inherent `ready` method to `Poll` that can be used with the `?` operator as an alternative to the `task::ready!` macro:
```rust
let val = ready!(fut.poll(cx));
let val = fut.poll(cx).ready()?;
```

I think this form is a nice, non-breaking middle ground between changing the `impl Try for Poll`, and adding a separate macro. It looks better than `ready!` in my opinion, and it composes well:

```rust
let elem = ready!(fut.poll(cx)).pop().unwrap();
let elem = fut.poll(cx).ready()?.pop().unwrap();
```

The planned stabilization of `ready!` in 1.56 has been reverted because I think this alternate approach is worth considering.

r? rust-lang/libs
2021-10-11 23:45:48 +02:00
The8472
f1c588f1ef use fold instead of try_fold now that .by_ref().next() has been inlined 2021-10-11 23:36:04 +02:00
Ibraheem Ahmed
c517a0de3e add slice::swap tests 2021-10-11 16:16:20 -04:00
Ibraheem Ahmed
2a8ff8df54 improve slice::swap panic message 2021-10-11 16:14:31 -04:00
ibraheemdev
33ecc33268 use swap_unchecked in slice::reverse 2021-10-11 16:14:31 -04:00
ibraheemdev
14769ce96f enable slice_swap_unchecked feature in doc test 2021-10-11 16:14:30 -04:00
ibraheemdev
1afe14ceed add slice::swap_unchecked 2021-10-11 16:14:30 -04:00