Commit Graph

6342 Commits

Author SHA1 Message Date
Matthias Krüger
13e284033e
Rollup merge of #92444 - dtolnay:coremethods, r=joshtriplett
Consolidate Result's and Option's methods into fewer impl blocks

`Result`'s and `Option`'s methods have historically been separated up into `impl` blocks based on their trait bounds, with the bounds specified on type parameters of the impl block. I find this unhelpful because closely related methods, like `unwrap_or` and `unwrap_or_default`, end up disproportionately far apart in source code and rustdocs:

<pre>
impl&lt;T&gt; Option&lt;T&gt; {
    pub fn unwrap_or(self, default: T) -&gt; T {
        ...
    }

    <img alt="one eternity later" src="https://user-images.githubusercontent.com/1940490/147780325-ad4e01a4-c971-436e-bdf4-e755f2d35f15.jpg" width="750">
}

impl&lt;T: Default&gt; Option&lt;T&gt; {
    pub fn unwrap_or_default(self) -&gt; T {
        ...
    }
}
</pre>

I'd prefer for method to be in as few impl blocks as possible, with the most logical grouping within each impl block. Any bounds needed can be written as `where` clauses on the method instead:

```rust
impl<T> Option<T> {
    pub fn unwrap_or(self, default: T) -> T {
        ...
    }

    pub fn unwrap_or_default(self) -> T
    where
        T: Default,
    {
        ...
    }
}
```

*Warning: the end-to-end diff of this PR is computed confusingly by git / rendered confusingly by GitHub; it's practically impossible to review that way. I've broken the PR into commits that move small groups of methods for which git behaves better &mdash; these each should be easily individually reviewable.*
2022-01-03 14:44:21 +01:00
Matthias Krüger
92f28bda38
Rollup merge of #92409 - bjorn3:libtest_cleanups, r=m-ou-se
Couple of libtest cleanups

Remove the unnecessary `TDynBenchFn` trait and remove a couple of unused attributes and feature gates.
2022-01-03 14:44:19 +01:00
bors
82418f93ac Auto merge of #91961 - kornelski:track_split_caller, r=joshtriplett
Track caller of slice split and swap

Improves error location for `slice.split_at*()` and `slice.swap()`.

These are generic inline functions, so the `#[track_caller]` on them is free — only changes a value of an argument already passed to panicking code.
2022-01-02 09:35:24 +00:00
bors
7b13c628a2 Auto merge of #92482 - matthiaskrgr:rollup-uso1zi0, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - #84083 (Clarify the guarantees that ThreadId does and doesn't make.)
 - #91593 (Remove unnecessary bounds for some Hash{Map,Set} methods)
 - #92297 (Reduce compile time of rustbuild)
 - #92332 (Add test for where clause order)
 - #92438 (Enforce formatting for rustc_codegen_cranelift)
 - #92463 (Remove pronunciation guide from Vec<T>)
 - #92468 (Emit an error for `--cfg=)`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-01-02 00:20:04 +00:00
Matthias Krüger
aa31c9726d
Rollup merge of #92463 - thomcc:thats-not-how-its-pronounced, r=joshtriplett
Remove pronunciation guide from Vec<T>

I performed an extremely scientific poll on twitter, and determined this is not how it's pronounced: https://twitter.com/at_tcsc/status/1476643344285581315
2022-01-01 22:49:52 +01:00
Matthias Krüger
5137f7c9db
Rollup merge of #91593 - upsuper-forks:hashmap-set-methods-bound, r=dtolnay
Remove unnecessary bounds for some Hash{Map,Set} methods

This PR moves `HashMap::{into_keys,into_values,retain}` and `HashSet::retain` from `impl` blocks with `K: Eq + Hash, S: BuildHasher` into the blocks without them. It doesn't seem to me there is any reason these methods need to be bounded by that. This change brings `HashMap::{into_keys,into_values}` on par with `HashMap::{keys,values,values_mut}` which are not bounded either.
2022-01-01 22:49:48 +01:00
Matthias Krüger
30ec1f0384
Rollup merge of #84083 - ltratt:threadid_doc_tweak, r=dtolnay
Clarify the guarantees that ThreadId does and doesn't make.

The existing documentation does not spell out whether `ThreadId`s are unique during the lifetime of a thread or of a process. I had to examine the source code to realise (pleasingly!) that they're unique for the lifetime of a process. That seems worth documenting clearly, as it's a strong guarantee.

Examining the way `ThreadId`s are created also made me realise that the `as_u64` method on `ThreadId` could be a trap for the unwary on those platforms where the platform's notion of a thread identifier is also a 64 bit integer (particularly if they happen to use a similar identifier scheme to `ThreadId`). I therefore think it's worth being even clearer that there's no relationship between the two.
2022-01-01 22:49:47 +01:00
bors
dd3ac41495 Auto merge of #92396 - xfix:remove-commandenv-apply, r=Mark-Simulacrum
Remove CommandEnv::apply

It's not being used and uses unsound set_var and remove_var functions. This is an internal function that isn't exported (even with `process_internals` feature), so this shouldn't break anything.

Also see #92365. Note that this isn't the only use of those methods in standard library, so that particular pull request will need more changes than just this to work (in particular, `test_capture_env_at_spawn` is using `set_var` and `remove_var`).
2022-01-01 20:45:37 +00:00
Matthias Krüger
4bd4e271e4
Rollup merge of #92469 - joshtriplett:test-number-fix, r=Mark-Simulacrum
Make tidy check for magic numbers that spell things

Remove existing problematic cases.

r? `@Mark-Simulacrum`
2022-01-01 10:48:58 +01:00
Matthias Krüger
a6e4d684aa
Rollup merge of #92097 - saethlin:split-without-deref, r=the8472
Implement split_at_spare_mut without Deref to a slice so that the spare slice is valid

~I'm not sure I understand what's going on here correctly. And I'm pretty sure this safety comment needs to be changed. I'm just referring to the same thing that `as_mut_ptr_range` does.~ (Thanks `@RalfJung` for the guidance and clearing things up)

I tried to run https://github.com/rust-lang/miri-test-libstd on alloc with -Zmiri-track-raw-pointers, and got a failure on the test `vec::test_extend_from_within`.

I minimized the test failure into this program:
```rust
#![feature(vec_split_at_spare)]
fn main() {
    Vec::<i32>::with_capacity(1).split_at_spare_mut();
}
```

The problem is that the existing implementation is actually getting a pointer range where both pointers are derived from the initialized region of the Vec's allocation, but we need the second one to be valid for the region between len and capacity. (thanks Ralf for clearing this up)
2022-01-01 10:48:54 +01:00
Josh Triplett
0d55bd1100 Make tidy check for magic numbers that spell things
Remove existing problematic cases.
2021-12-31 21:13:07 -08:00
Ben Kimock
777c853b4a Clarify safety comment 2021-12-31 18:03:07 -05:00
Matthias Krüger
8322603970
Rollup merge of #92338 - Xuanwo:try_reserve, r=dtolnay
Add try_reserve and  try_reserve_exact for OsString

Add `try_reserve` and `try_reserve_exact` for OsString.

Part of https://github.com/rust-lang/rust/issues/91789

I will squash the commits after PR is ready to merge.

Signed-off-by: Xuanwo <github@xuanwo.io>
2021-12-31 23:14:46 +01:00
Thom Chiovoloni
51a1681b69 Remove pronunciation guide from Vec<T> 2021-12-31 16:04:13 -05:00
David Tolnay
d29941e724
Remove needless allocation from example code of OsString 2021-12-30 12:45:02 -08:00
David Tolnay
1f62c24d5a
Fix some copy/paste hysteresis in OsString try_reserve docs
It appears `find_max_slow` comes from the BinaryHeap docs, where the
try_reserve example is a slow implementation of find_max. It has no
relevance to this code in OsString though.
2021-12-30 12:41:26 -08:00
David Tolnay
dc3291614a
Consolidate impl Option<&mut T> 2021-12-30 10:37:53 -08:00
David Tolnay
538fe4b28d
Consolidate impl Option<&T> 2021-12-30 10:37:27 -08:00
David Tolnay
9d65bc51c1
Move Option::as_deref_mut 2021-12-30 10:36:55 -08:00
David Tolnay
48a91a08d1
Move Option::as_deref 2021-12-30 10:36:37 -08:00
David Tolnay
bbcf09f2fb
Move Option::unwrap_or_default 2021-12-30 10:34:35 -08:00
David Tolnay
b7a0ab18f6
Consolidate impl Result<&mut T, E> 2021-12-30 10:31:26 -08:00
David Tolnay
e63e2680da
Consolidate impl Result<&T, E> 2021-12-30 10:30:28 -08:00
David Tolnay
b2df61fa9f
Move Result::into_err 2021-12-30 10:28:54 -08:00
David Tolnay
778ca204a6
Move Result::into_ok 2021-12-30 10:28:23 -08:00
David Tolnay
06ea5ebe4e
Move Result::expect_err and Result::unwrap_err 2021-12-30 10:27:43 -08:00
David Tolnay
aa2aca2c8c
Move Result::unwrap_or_default 2021-12-30 10:26:36 -08:00
David Tolnay
15f57a6c59
Move Result::expect and Result::unwrap 2021-12-30 10:25:42 -08:00
David Tolnay
5aa8f91ff0
Move Result::as_deref_mut 2021-12-30 10:24:23 -08:00
David Tolnay
eda61d8d8a
Move Result::as_deref 2021-12-30 10:23:46 -08:00
Chayim Refael Friedman
e86ecdf9fe
Use UnsafeCell::get_mut() in core::lazy::OnceCell::get_mut()
This removes one unnecessary `unsafe` block.
2021-12-30 05:04:44 +02:00
bjorn3
e015b9ee80 Remove unused allow deprecated 2021-12-29 18:06:01 +01:00
bjorn3
f710bc5fc0 Remove unused feature gates from libtest 2021-12-29 16:36:22 +01:00
bjorn3
1ea4810c84 Remove #![crate_name] attribute from libtest
The crate name is already set in Cargo.toml. The comment says there is
some logic in the compiler that reads #![crate_name] and not
--crate-name, but I can't find it. Removing it seems to work fine.
2021-12-29 16:36:08 +01:00
bjorn3
75d8339cdd Replace TDynBenchFn with Fn(&mut Bencher) 2021-12-29 16:17:50 +01:00
Konrad Borowski
14fc9dcbba Remove CommandEnv::apply
It's not being used and uses unsound set_var and remove_var
functions.
2021-12-29 10:07:44 +01:00
Xuanwo
b07ae1c4d5
Address comments
Signed-off-by: Xuanwo <github@xuanwo.io>
2021-12-29 14:02:20 +08:00
bors
b70cc6422c Auto merge of #92291 - AngelicosPhosphoros:typeid_inline_revert_92135, r=joshtriplett
Reverts #92135 because perf regression

Please, start a perf test for this.

r? `@joshtriplett` You approved original PR.
2021-12-29 05:53:19 +00:00
Xuanwo
9166428be1
Update library/std/src/ffi/os_str.rs
Co-authored-by: David Tolnay <dtolnay@gmail.com>
2021-12-29 13:49:39 +08:00
Matthias Krüger
c9cc9e589c
Rollup merge of #92335 - ecstatic-morse:std-column-unicode, r=Manishearth
Document units for `std::column`

Fixes #92301.

r? ``@Manishearth`` (for the terminology and the Chinese)
2021-12-28 13:59:26 +01:00
Xuanwo
27b92c9f98
Implement support in wtf8
Signed-off-by: Xuanwo <github@xuanwo.io>
2021-12-28 11:53:14 +08:00
Xuanwo
013fbc6187
Fix windows build
Signed-off-by: Xuanwo <github@xuanwo.io>
2021-12-28 11:40:58 +08:00
Xuanwo
c40ac57efb
Add try_reserve for OsString
Signed-off-by: Xuanwo <github@xuanwo.io>
2021-12-28 11:28:05 +08:00
Dylan MacKenzie
3115d8413a Document units for std::column 2021-12-27 15:39:35 -08:00
Matthias Krüger
eab81297f7
Rollup merge of #92307 - hiroshi-maybe:fix-minor-typos, r=camelid
Fix minor typos
2021-12-27 21:42:30 +01:00
Matthias Krüger
4d8ba2193f
Rollup merge of #92264 - Shadlock0133:patch-1, r=the8472
Remove `maybe_uninit_extra` feature from Vec docs

In `Vec`, two doc tests are using `MaybeUninit::write` , stabilized in 1.55. This makes docs' usage of `maybe_uninit_extra` feature unnecessary.
2021-12-27 21:42:28 +01:00
Hiroshi Kori
7a3a668bc9 fix typo: intialized -> initialized 2021-12-26 18:37:11 -08:00
Hiroshi Kori
7ddad349b1 fix typo: the use f.pad -> then use f.pad 2021-12-26 17:44:53 -08:00
AngelicosPhosphoros
72b0c8f233 Reverts #92135 because perf regression 2021-12-26 16:02:33 +03:00
Laurence Tratt
d66a9e16ba Language tweak. 2021-12-25 15:18:55 +00:00