Commit graph

1990 commits

Author SHA1 Message Date
bors
1c0485610e Auto merge of #87598 - ccqpein:master, r=yaahc
Add doctests for HashMap's into_values and into_keys methods

Fixes #87591
2021-08-23 05:06:29 +00:00
ccQpein
6eefee1077 Add doctests for 's into_values and into_keys methods 2021-08-22 09:21:00 -04:00
bors
2ad56d5c90 Auto merge of #85166 - mbhall88:file-prefix, r=dtolnay
add file_prefix method to std::path

This is an initial implementation of `std::path::Path::file_prefix`. It is effectively a "left" variant of the existing [`file_stem`](https://doc.rust-lang.org/std/path/struct.Path.html#method.file_stem) method. An illustration of the difference is

```rust
use std::path::Path;

let path = Path::new("foo.tar.gz");
assert_eq!(path.file_stem(), Some("foo.tar"));
assert_eq!(path.file_prefix(), Some("foo"));
```

In my own development, I generally find I almost always want the prefix, rather than the stem, so I thought it might be best to suggest it's addition to libstd.

Of course, as this is my first contribution, I expect there is probably more work that needs to be done. Additionally, if the libstd team feel this isn't appropriate then so be it.

There has been some [discussion about this on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/file_lstem/near/238076313) and a user there suggested I open a PR to see whether someone in the libstd team thinks it is worth pursuing.
2021-08-22 05:19:48 +00:00
bors
a0035916e0 Auto merge of #83093 - the8472:smaller-instant-hammer, r=Amanieu
where available use AtomicU{64,128} instead of mutex for Instant backsliding protection

This decreases the overhead of backsliding protection on x86 systems with unreliable TSC, e.g. windows. And on aarch64 systems where 128bit atomics are available.

The following benchmarks were taken on x86_64 linux though by overriding `actually_monotonic()`, the numbers may look different on other platforms

```
# actually_monotonic() == true
test time::tests::instant_contention_01_threads                   ... bench:          44 ns/iter (+/- 0)
test time::tests::instant_contention_02_threads                   ... bench:          44 ns/iter (+/- 0)
test time::tests::instant_contention_04_threads                   ... bench:          44 ns/iter (+/- 0)
test time::tests::instant_contention_08_threads                   ... bench:          44 ns/iter (+/- 0)
test time::tests::instant_contention_16_threads                   ... bench:          44 ns/iter (+/- 0)

# 1x AtomicU64
test time::tests::instant_contention_01_threads                   ... bench:          65 ns/iter (+/- 0)
test time::tests::instant_contention_02_threads                   ... bench:         157 ns/iter (+/- 20)
test time::tests::instant_contention_04_threads                   ... bench:         281 ns/iter (+/- 53)
test time::tests::instant_contention_08_threads                   ... bench:         555 ns/iter (+/- 77)
test time::tests::instant_contention_16_threads                   ... bench:         883 ns/iter (+/- 107)

# mutex
test time::tests::instant_contention_01_threads                   ... bench:          60 ns/iter (+/- 2)
test time::tests::instant_contention_02_threads                   ... bench:         770 ns/iter (+/- 231)
test time::tests::instant_contention_04_threads                   ... bench:       1,347 ns/iter (+/- 45)
test time::tests::instant_contention_08_threads                   ... bench:       2,693 ns/iter (+/- 114)
test time::tests::instant_contention_16_threads                   ... bench:       5,244 ns/iter (+/- 487)
```

Since I don't have an arm machine with 128bit atomics I wasn't able to benchmark the AtomicU128 implementation.
2021-08-20 19:06:46 +00:00
The8472
cd82b4246e fix tests on wasm targets that have 32bit time_t and don't have threads 2021-08-20 20:34:23 +02:00
bors
521734787e Auto merge of #87329 - sunfishcode:sunfishcode/io-safety, r=joshtriplett
I/O safety.

Introduce `OwnedFd` and `BorrowedFd`, and the `AsFd` trait, and
implementations of `AsFd`, `From<OwnedFd>` and `From<T> for OwnedFd`
for relevant types, along with Windows counterparts for handles and
sockets.

Tracking issue: <https://github.com/rust-lang/rust/issues/87074>

RFC: <https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md>

Highlights:
 - The doc comments at the top of library/std/src/os/unix/io/mod.rs and library/std/src/os/windows/io/mod.rs
 - The new types and traits in library/std/src/os/unix/io/fd.rs and library/std/src/os/windows/io/handle.rs
 - The removal of the `RawHandle` struct the Windows impl, which had the same name as the `RawHandle` type alias, and its functionality is now folded into `Handle`.

Managing five levels of wrapping (File wraps sys::fs::File wraps sys::fs::FileDesc wraps OwnedFd wraps RawFd, etc.) made for a fair amount of churn and verbose as/into/from sequences in some places. I've managed to simplify some of them, but I'm open to ideas here.

r? `@joshtriplett`
2021-08-20 11:00:55 +00:00
bors
bcfd3f7e88 Auto merge of #86898 - the8472:path-cmp, r=dtolnay
Add fast path for Path::cmp that skips over long shared prefixes

```
# before
test path::tests::bench_path_cmp_fast_path_buf_sort               ... bench:      60,811 ns/iter (+/- 865)
test path::tests::bench_path_cmp_fast_path_long                   ... bench:       6,459 ns/iter (+/- 275)
test path::tests::bench_path_cmp_fast_path_short                  ... bench:       1,777 ns/iter (+/- 34)

# after
test path::tests::bench_path_cmp_fast_path_buf_sort               ... bench:      38,140 ns/iter (+/- 211)
test path::tests::bench_path_cmp_fast_path_long                   ... bench:       1,471 ns/iter (+/- 24)
test path::tests::bench_path_cmp_fast_path_short                  ... bench:       1,106 ns/iter (+/- 9)
```
2021-08-20 05:00:45 +00:00
Dan Gohman
b4dfa198bf Fix doc test failures on Windows. 2021-08-19 16:15:29 -07:00
bors
6d64f7f695 Auto merge of #88165 - GuillaumeGomez:rollup-4o0v2ps, r=GuillaumeGomez
Rollup of 8 pull requests

Successful merges:

 - #86123 (Preserve more spans in internal `rustc_queries!` macro)
 - #87874 (Add TcpStream type to TcpListener::incoming docs)
 - #88034 (rustc_privacy: Replace `HirId`s and `DefId`s with `LocalDefId`s where possible)
 - #88050 (Remove `HashStable` impls for `FileName` and `RealFileName`)
 - #88093 ([rustdoc] Wrap code blocks in `<code>` tag)
 - #88146 (Add tests for some `feature(const_evaluatable_checked)` incr comp issues)
 - #88153 (Update .mailmap)
 - #88159 (Use a trait instead of the now disallowed missing trait there)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2021-08-19 20:31:05 +00:00
Dan Gohman
e555003e6d Factor out a common RawFd/AsRawFd/etc for Unix and WASI. 2021-08-19 13:27:19 -07:00
Dan Gohman
0377a63352 Fix syntax for non-doc comments, and use crate:: instead of std::. 2021-08-19 12:23:04 -07:00
Dan Gohman
187ee5c824 Add I/O safety trait impls for process::Stdio and process::Child. 2021-08-19 12:02:41 -07:00
Dan Gohman
6f872880b4 Use the correct into_* on Windows to avoid dropping a stdio handle.
Use `into_raw_handle()` rather than `into_inner()` to completely consume a
`Handle` without dropping its contained handle.
2021-08-19 12:02:41 -07:00
Dan Gohman
9b99f8c454 Remove the #![feature(io_safety)] from lib.rs. 2021-08-19 12:02:40 -07:00
Dan Gohman
a7d9ab5835 Fix an unused import warning. 2021-08-19 12:02:40 -07:00
Dan Gohman
cada5fb336 Update PidFd for the new I/O safety APIs. 2021-08-19 12:02:40 -07:00
Dan Gohman
1ae1eeec25 Rename OptionFileHandle to HandleOrInvalid and make it just wrap an Option<OwnedHandle>
The name (and updated documentation) make the FFI-only usage clearer, and wrapping Option<OwnedHandle> avoids the need to write a separate Drop or Debug impl.

Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2021-08-19 12:02:40 -07:00
Dan Gohman
18a9f4628a Don't encourage migration until io_safety is stablized. 2021-08-19 12:02:40 -07:00
Dan Gohman
1dbd6d60f0 Factor out Unix and WASI fd code into a common module. 2021-08-19 12:02:40 -07:00
Dan Gohman
71dab738ac Synchronize minor differences between Unix and WASI implementations. 2021-08-19 12:02:40 -07:00
Dan Gohman
907f00be30 Add more comments about the INVALID_HANDLE_VALUE situation. 2021-08-19 12:02:40 -07:00
Dan Gohman
ab08639e59 Add comments about impls for File, TcpStream, ChildStdin, etc. 2021-08-19 12:02:40 -07:00
Dan Gohman
68964a7d68 Fix copypasta of "Unix" within the WASI directory. 2021-08-19 12:02:40 -07:00
Dan Gohman
1b35f7405a Reword the description of dup2/dup3. 2021-08-19 12:02:40 -07:00
Dan Gohman
6d7211738d Add Safety comments to the As* for Owned* implementations. 2021-08-19 12:02:40 -07:00
Dan Gohman
6486f89cbc Add Owned*, Borrowed*, and As* to the preludes. 2021-08-19 12:02:39 -07:00
Dan Gohman
0cb69dec57 Rename OwnedFd's private field to match it's debug output. 2021-08-19 12:02:39 -07:00
Dan Gohman
45b5de3376 Delete a spurious empty comment line. 2021-08-19 12:02:39 -07:00
Dan Gohman
926344a80f Add a comment about how OwnedHandle should not be used with registry handles. 2021-08-19 12:02:39 -07:00
Dan Gohman
31f7bf8271 Add a comment about OptionFileHandle. 2021-08-19 12:02:39 -07:00
Dan Gohman
6b4dbdbf47 Be more precise about mmap and undefined behavior.
`mmap` doesn't *always* cause undefined behavior; it depends on the
details of how you use it.
2021-08-19 12:02:39 -07:00
Dan Gohman
1f8a450cdd Add a test to ensure that RawFd is the size we assume it is. 2021-08-19 12:02:39 -07:00
Dan Gohman
1c6bf04edb Update library/std/src/os/windows/io/socket.rs
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2021-08-19 12:02:39 -07:00
Dan Gohman
a23ca7ceb1 Update library/std/src/os/windows/io/handle.rs
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2021-08-19 12:02:39 -07:00
Dan Gohman
3a38511ab3 Update library/std/src/os/unix/io/fd.rs
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2021-08-19 12:02:39 -07:00
Dan Gohman
d15418586c I/O safety.
Introduce `OwnedFd` and `BorrowedFd`, and the `AsFd` trait, and
implementations of `AsFd`, `From<OwnedFd>` and `From<T> for OwnedFd`
for relevant types, along with Windows counterparts for handles and
sockets.

Tracking issue:
 - <https://github.com/rust-lang/rust/issues/87074>

RFC:
 - <https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md>
2021-08-19 12:02:39 -07:00
Guillaume Gomez
6ce8a371bd
Rollup merge of #87874 - schneems:schneems/tcpstream-iterator-type, r=Mark-Simulacrum
Add TcpStream type to TcpListener::incoming docs

## Context

While going through the "The Rust Programming Language" book (Klabnik & Nichols), the TCP server example directs us to use TcpListener::incoming. I was curious how I could pass this value to a function (before reading ahead in the book), so I looked up the docs to determine the signature.

When I opened the docs, I found https://doc.rust-lang.org/std/net/struct.TcpListener.html#method.incoming, which didn't mention TcpStream anywhere in the example.

Eventually, I clicked on https://doc.rust-lang.org/std/net/struct.TcpListener.html#method.accept in the docs (after clicking a few other locations first), and was able to surmise that the value contained TcpStream.

## Opportunity

While this type is mentioned several times in this doc, I feel that someone should be able to fully use the results of the TcpListner::incoming iterator based solely on the docs of just this method.

## Implementation

I took the code from the top-level TcpListener https://doc.rust-lang.org/std/net/struct.TcpListener.html#method.incoming and blended it with the existing docs for TcpListener::incoming https://doc.rust-lang.org/std/net/struct.TcpListener.html#method.incoming.

It does make the example a little longer, and it also introduces a little duplication. It also gives the reader the type signatures they need to move on to the next step.

## Additional considerations

I noticed that in this doc, `handle_connection` and `handle_client` are both used to accept a TcpStream in the docs on this page. I want to standardize on one function name convention, so readers don't accidentally think two different concepts are being referenced. I didn't want to cram do too much in one PR, I can update this PR to make that change, or I could send another PR (if you would like).

First attempted contribution to Rust (and I'm also still very new, hence reading through the rust book for the first time)! Would you please let me know what you think?
2021-08-19 19:30:05 +02:00
bors
7960030d69 Auto merge of #88151 - alexcrichton:update-backtrace, r=Mark-Simulacrum
Update the backtrace crate in libstd

This commit updates the backtrace crate in libstd now that dependencies
have been updated to use `memchr` from the standard library as well.
This is mostly just making sure deps are up-to-date and have all the
latest-and-greatest fixes and such.

Closes rust-lang/backtrace-rs#432
2021-08-19 17:20:59 +00:00
Alex Crichton
4a3e73643a Update the backtrace crate in libstd
This commit updates the backtrace crate in libstd now that dependencies
have been updated to use `memchr` from the standard library as well.
This is mostly just making sure deps are up-to-date and have all the
latest-and-greatest fixes and such.

Closes rust-lang/backtrace-rs#432
2021-08-19 07:31:49 -07:00
bors
a9ab2e5539 Auto merge of #88002 - hermitcore:unbox-mutex, r=dtolnay
Unbox mutexes, condvars and rwlocks on hermit

[RustyHermit](https://github.com/hermitcore/rusty-hermit) provides now movable synchronization primitives and we are able to unbox mutexes and condvars.
2021-08-19 09:08:11 +00:00
Guillaume Gomez
fbaa4a2a17
Rollup merge of #88109 - inquisitivecrystal:env-docs, r=m-ou-se
Fix environment variable getter docs

`@RalfJung` pointed out a number of errors and suboptimal choices I made in my documentation for #86183. This PR should (hopefully) fix the problems they've identified.
2021-08-18 19:55:02 +02:00
Guillaume Gomez
627bc60702
Rollup merge of #88012 - sunfishcode:sunfishcode/wasi-raw-fd-c-int, r=alexcrichton
Change WASI's `RawFd` from `u32` to `c_int` (`i32`).

WASI previously used `u32` as its `RawFd` type, since its "file descriptors"
are unsigned table indices, and there's no fundamental reason why WASI can't
have more than 2^31 handles.

However, this creates myriad little incompability problems with code
that also supports Unix platforms, where `RawFd` is `c_int`. While WASI
isn't a Unix, it often shares code with Unix, and this difference made
such shared code inconvenient. #87329 is the most recent example of such
code.

So, switch WASI to use `c_int`, which is `i32`. This will mean that code
intending to support WASI should ideally avoid assuming that negative file
descriptors are invalid, even though POSIX itself says that file descriptors
are never negative.

This is a breaking change, but `RawFd` is considerd an experimental
feature in [the documentation].

[the documentation]: https://doc.rust-lang.org/stable/std/os/wasi/io/type.RawFd.html

r? `@alexcrichton`
2021-08-18 19:54:56 +02:00
the8472
6c92bae7fa
[review] fix comment
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2021-08-17 19:31:32 +02:00
inquisitivecrystal
fdf09130df Fix environment variable getter docs 2021-08-17 00:37:52 -07:00
Deadbeef
b5afa6807b
Constified Default implementations
The libs-api team agrees to allow const_trait_impl to appear in the
standard library as long as stable code cannot be broken (they are
properly gated) this means if the compiler teams thinks it's okay, then
it's okay.

My priority on constifying would be:

	1. Non-generic impls (e.g. Default) or generic impls with no
	   bounds
	2. Generic functions with bounds (that use const impls)
	3. Generic impls with bounds
	4. Impls for traits with associated types

For people opening constification PRs: please cc me and/or oli-obk.
2021-08-17 07:15:54 +00:00
Michael Hall
51cf318dbc remove unnecessary empty check 2021-08-17 12:26:24 +10:00
The8472
ff12ab2d99 correct overflows in the backslide case, add test 2021-08-16 22:15:52 +02:00
Joshua Nelson
03df65497e feature gate doc(primitive) 2021-08-16 05:41:16 +00:00
the8472
7256a6a86d
Apply suggestions from code review
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2021-08-16 00:01:41 +02:00
Dan Gohman
35de5c9b35 Change WASI's RawFd from u32 to c_int (i32).
WASI previously used `u32` as its `RawFd` type, since its "file descriptors"
are unsigned table indices, and there's no fundamental reason why WASI can't
have more than 2^31 handles.

However, this creates myriad little incompability problems with code
that also supports Unix platforms, where `RawFd` is `c_int`. While WASI
isn't a Unix, it often shares code with Unix, and this difference made
such shared code inconvenient. #87329 is the most recent example of such
code.

So, switch WASI to use `c_int`, which is `i32`. This will mean that code
intending to support WASI should ideally avoid assuming that negative file
descriptors are invalid, even though POSIX itself says that file descriptors
are never negative.

This is a breaking change, but `RawFd` is considerd an experimental
feature in [the documentation].

[the documentation]: https://doc.rust-lang.org/stable/std/os/wasi/io/type.RawFd.html
2021-08-13 09:10:22 -07:00