Commit Graph

292 Commits

Author SHA1 Message Date
Travis Cross 73f5fe78d4 Revise Result/Option guarantee docs
The notation used here (e.g. "transmute `t: T` to `Option<T>`") felt
maybe a bit heavy, in the context of the library documentation, so
let's elaborate this a bit.

Also, in the `Option` docs, we talk about this being true for "the
following types `T`", but it felt this caveat might get a bit lost in
the next sentence that talks about the valid transmutations, so let's
reiterate the caveat.

While we're touching the line, we can improve:

> The only difference is the implied semantics:

This sentence was a bit awkward due to the mismatched plurality and
not identifying the difference being spoken to.  We'll reword this to
make it more clear.

We'll wrap to 80, since the existing text and most of the doc comments
in these files are wrapped this way.
2025-10-18 00:59:17 +00:00
Ralf Jung 29a19f84c2 Result/Option layout guarantee clarifications 2025-10-17 23:18:14 +00:00
Ben Kimock df58fd8cf7 Change the cfg to a dash 2025-09-21 13:12:20 -04:00
Ben Kimock 888679013d Add panic=immediate-abort 2025-09-21 13:12:18 -04:00
Evgenii Zheltonozhskii ff9b1c1d28 Constify Eq, Ord, PartialOrd 2025-09-12 12:39:31 +03:00
Nathaniel McCallum 00fcb14e4e clean up some old const trait impl syntax 2025-09-06 04:35:52 +02:00
ltdk 1c64d3e6d1 Constify conversion traits 2025-09-01 21:38:26 -04:00
Matthias Krüger 1e90922864 Rollup merge of #144274 - Qelxiros:option-reduce, r=tgross35
add Option::reduce

Tracking issue: rust-lang/rust#144273
2025-08-27 11:26:48 +02:00
Stuart Cook 0671b2fe49 Rollup merge of #142871 - chenyukang:yukang-fix-doc-for-transpose, r=ibraheemdev
Trivial improve doc for transpose

When I saw old doc, I felt a little confused.
Seems it would be clearer this way.
2025-08-19 14:18:17 +10:00
Boxy 7bc34622f0 tidy 2025-08-06 16:55:50 +01:00
Jeremy Smart 613080b5f1 add Option::reduce 2025-07-31 23:46:04 -04:00
许杰友 Jieyou Xu (Joe) 1f8b531b54 Rollup merge of #143768 - Randl:const-try, r=oli-obk
Constify Try, From, TryFrom and relevant traits
2025-07-22 00:54:26 +08:00
Evgenii Zheltonozhskii 3c76e3d002 Constify Try, From, TryFrom 2025-07-21 12:49:45 +03:00
Ralf Jung d2be1c8d00 Option::as_slice: fix comment 2025-07-20 22:39:10 +02:00
Matthias Krüger 82fbbddf63 Rollup merge of #143925 - oli-obk:slice-const-partialeq, r=fee1-dead
Make slice comparisons const

This needed a fix for `derive_const`, too, as it wasn't usable in libcore anymore as trait impls need const stability attributes. I think we can't use the same system as normal trait impls while `const_trait_impl` is still unstable.

r? ```@fee1-dead```

cc rust-lang/rust#143800
2025-07-18 14:49:19 +02:00
Oli Scherer 2f5722cf1b Make Option const PartialEq 2025-07-17 15:42:54 +00:00
Oli Scherer 853333d4bd constify Option methods 2025-07-17 09:11:05 +00:00
Deadbeef 6b02597ed3 update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
Trevor Gross 0d99b5585b Rollup merge of #143426 - hkBst:clippy-fix-indent-1, r=jhpratt
clippy fix: indentation

Fixes indentation of markdown comments.
2025-07-08 22:50:27 -05:00
Esteban Küber c3301503b9 Make Default const and add some const Default impls
Full list of `impl const Default` types:

- ()
- bool
- char
- Cell
- std::ascii::Char
- usize
- u8
- u16
- u32
- u64
- u128
- i8
- i16
- i32
- i64
- i128
- f16
- f32
- f64
- f128
- std::marker::PhantomData<T>
- Option<T>
- std::iter::Empty<T>
- std::ptr::Alignment
- &[T]
- &mut [T]
- &str
- &mut str
- String
- Vec<T>
2025-07-07 22:09:37 +00:00
Marijn Schouten 1a1b52acbf clippy fix: indentation 2025-07-04 11:52:17 +00:00
yukang 06ae1bee20 Make doc for transpose api better 2025-06-22 20:43:31 +08:00
Matthias Krüger fac011eb2d Rollup merge of #142158 - xizheyin:141617, r=jdonszelmann
Tracking the old name of renamed unstable library features

This PR resolves the first problem of rust-lang/rust#141617 : tracking renamed unstable features. The first commit is to add a ui test, and the second one tracks the changes. I will comment on the code for clarification.

r? `@jdonszelmann`
There have been a lot of PR's reviewed by you lately, thanks for your time!

cc `@jyn514`
2025-06-13 05:16:56 +02:00
xizheyin b8066f94fd Tracking the old name of renamed unstable library attribute
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-06-12 19:24:11 +08:00
Guillaume Gomez 00db3457a6 Rollup merge of #141447 - y86-dev:option-layout-docs, r=RalfJung
Document representation of `Option<unsafe fn()>`

https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/Option.20Layout.20with.20.60fn.60.20pointers/with/520055652
2025-06-07 22:22:56 +02:00
tk eed065958b feat: map_or_default for result and option 2025-05-27 19:47:14 +02:00
Matthias Krüger 334d7bd698 Rollup merge of #137323 - joshlf:transmute-npo, r=RalfJung
Guarantee behavior of transmuting `Option::<T>::None` subject to NPO

In https://github.com/rust-lang/rust/pull/115333, we added a guarantee that transmuting from `[0u8; N]` to `Option<P>` is sound where `P` is a pointer type subject to the null pointer optimization (NPO). It would be useful to be able to guarantee the inverse - that a `None::<P>` value can be transmutes to an array and that will yield `[0u8; N]`.

Closes #117591
2025-05-24 16:08:45 +02:00
Benno Lossin a08f6f10d4 document representation of Option<unsafe fn()> 2025-05-23 17:37:53 +02:00
Hegui Dai d0ac97d649 add examples using .as_ref() for is_some_and and is_none_or 2025-04-24 10:22:49 +08:00
Hegui Dai c0446d3cbb keep the original text for is_some and is_none 2025-04-24 10:12:14 +08:00
Hegui Dai ea0b6cba06 Solved suggestions 2025-04-21 09:54:11 +08:00
Hegui Dai 159ae8fcee Keeps the inspect() classification the same as in Result 2025-03-26 14:50:03 +08:00
Hegui Dai fc126df5a7 Update the index.html of Option to make the summary more comprehensive 2025-03-26 08:23:43 +08:00
许杰友 Jieyou Xu (Joe) e0846806db Rollup merge of #138082 - thaliaarchi:slice-cfg-not-test, r=thomcc
Remove `#[cfg(not(test))]` gates in `core`

These gates are unnecessary now that unit tests for `core` are in a separate package, `coretests`, instead of in the same files as the source code. They previously prevented the two `core` versions from conflicting with each other.
2025-03-16 09:40:05 +08:00
Thalia Archibald 638b226a6a Remove #[cfg(not(test))] gates in core
These gates are unnecessary now that unit tests for `core` are in a
separate package, `coretests`, instead of in the same files as the
source code. They previously prevented the two `core` versions from
conflicting with each other.
2025-03-06 13:21:59 -08:00
Santiago Pastorino dcdfd551f0 Add UseCloned trait related code 2025-03-06 17:58:32 -03:00
Joshua Liebow-Feeser 0fdeab9525 Guarantee behavior of transmuting Option::<T>::None subject to NPO 2025-02-20 08:23:08 -08:00
Kornel 7b42bc0c79 Less unwrap() in documentation 2024-12-21 01:26:47 +00:00
Boxy 22998f0785 update cfgs 2024-11-27 15:14:54 +00:00
Boxy 174ad448c7 replace placeholder version 2024-11-27 12:10:21 +00:00
Ralf Jung 324d059962 stabilize const_option_ext 2024-11-12 21:42:15 +01:00
Michael Howell 12dc24f460 rustdoc-search: simplify rules for generics and type params
This commit is a response to feedback on the displayed type
signatures results, by making generics act stricter.

Generics are tightened by making order significant. This means
`Vec<Allocator>` now matches only with a true vector of allocators,
instead of matching the second type param. It also makes unboxing
within generics stricter, so `Result<A, B>` only matches if `B`
is in the error type and `A` is in the success type. The top level
of the function search is unaffected.

Find the discussion on:

* <https://rust-lang.zulipchat.com/#narrow/stream/393423-t-rustdoc.2Fmeetings/topic/meeting.202024-07-08/near/449965149>
* <https://github.com/rust-lang/rust/pull/124544#issuecomment-2204272265>
* <https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/deciding.20on.20semantics.20of.20generics.20in.20rustdoc.20search/near/476841363>
2024-10-30 12:27:48 -07:00
Ralf Jung 854e3c43e0 library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
Josh Stone acb09bf741 update bootstrap configs 2024-10-15 20:30:23 -07:00
Josh Stone f204e2c23b replace placeholder version
(cherry picked from commit 567fd9610c)
2024-10-15 20:13:55 -07:00
Trevor Gross 19f6c17df4 Stabilize const_option
This makes the following API stable in const contexts:

    impl<T> Option<T> {
        pub const fn as_mut(&mut self) -> Option<&mut T>;
        pub const fn expect(self, msg: &str) -> T;
        pub const fn unwrap(self) -> T;
        pub const unsafe fn unwrap_unchecked(self) -> T;
        pub const fn take(&mut self) -> Option<T>;
        pub const fn replace(&mut self, value: T) -> Option<T>;
    }

    impl<T> Option<&T> {
        pub const fn copied(self) -> Option<T>
        where T: Copy;
    }

    impl<T> Option<&mut T> {
        pub const fn copied(self) -> Option<T>
        where T: Copy;
    }

    impl<T, E> Option<Result<T, E>> {
        pub const fn transpose(self) -> Result<Option<T>, E>
    }

    impl<T> Option<Option<T>> {
        pub const fn flatten(self) -> Option<T>;
    }

The following functions make use of the unstable
`const_precise_live_drops` feature:

- `expect`
- `unwrap`
- `unwrap_unchecked`
- `transpose`
- `flatten`

Fixes: <https://github.com/rust-lang/rust/issues/67441>
2024-10-12 17:07:13 -04:00
bors 9e3e517446 Auto merge of #130829 - Urgau:option_array_transpose, r=ibraheemdev
Add `[Option<T>; N]::transpose`

This PR as a new unstable libs API, `[Option<T>; N]::transpose`, which permits going from `[Option<T>; N]` to `Option<[T; N]>`.

This new API doesn't have an ACP as it was directly asked by T-libs-api in https://github.com/rust-lang/rust/issues/97601#issuecomment-2372109119:

> [..] but it'd be trivial to provide a helper method `.transpose()` that turns array-of-Option into Option-of-array (**and we think that method should exist**; it already does for array-of-MaybeUninit).

r? libs
2024-10-02 04:31:15 +00:00
Matthias Krüger e76eb96a00 Rollup merge of #129087 - slanterns:option_get_or_insert_default, r=dtolnay
Stabilize `option_get_or_insert_default`

Closes: https://github.com/rust-lang/rust/issues/82901.

`@rustbot` label: +T-libs-api

r? libs-api
2024-09-27 19:07:58 +02:00
Urgau 1e24104587 Add [Option<T>; N]::transpose 2024-09-26 10:43:56 +02:00
Ralf Jung 89c3cbafb8 make unstable Result::flatten a const fn 2024-09-22 08:40:25 +02:00