rust/library/core/src
Jubilee 480a72d601
Rollup merge of #134340 - Urgau:stabilize-num_midpoint_signed, r=scottmcm
Stabilize `num_midpoint_signed` feature

This PR proposes that we stabilize the signed variants of [`iN::midpoint`](https://github.com/rust-lang/rust/issues/110840#issue-1684506201), the operation is equivalent to doing `(a + b) / 2` in a sufficiently large number.

The stabilized API surface would be:

```rust
/// Calculates the middle point of `self` and `rhs`.
///
/// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
/// sufficiently-large signed integer type. This implies that the result is
/// always rounded towards zero and that no overflow will ever occur.

impl i{8,16,32,64,128,size} {
    pub const fn midpoint(self, rhs: Self) -> Self;
}
```

T-libs-api previously stabilized the unsigned (and float) variants in #131784, the signed variants were left out because of the rounding that should be used in case of negative midpoint.

This stabilization proposal proposes that we round towards zero because:
 - it makes the obvious `(a + b) / 2` in a sufficiently-large number always true
   - using another rounding for the positive result would be inconsistent with the unsigned variants
 - it makes `midpoint(-a, -b)` == `-midpoint(a, b)` always true
 - it is consistent with `midpoint(a as f64, b as f64) as i64`
 - it makes it possible to always suggest `midpoint` as a replacement for `(a + b) / 2` expressions *(which we may want to do as a future work given the 21.2k hits on [GitHub Search](https://github.com/search?q=lang%3Arust+%2F%5C%28%5Ba-zA-Z_%5D*+%5C%2B+%5Ba-zA-Z_%5D*%5C%29+%5C%2F+2%2F&type=code&p=1))*

`@scottmcm` mentioned a drawback in https://github.com/rust-lang/rust/pull/132191#issuecomment-2439891200:
> I'm torn, because rounding towards zero makes it "wider" than other values, which `>> 1` avoids -- `(a + b) >> 1` has the nice behaviour that `midpoint(a, b) + 2 == midpoint(a + 2, b + 2)`.
>
> But I guess overall sticking with `(a + b) / 2` makes sense as well, and I do like the negation property 🤷

Which I think is outweigh by the advantages cited above.

Closes #110840
cc `@rust-lang/libs-api`
cc `@scottmcm`
r? `@dtolnay`
2025-02-20 14:58:16 -08:00
..
alloc core: Apply unsafe_op_in_unsafe_fn 2025-02-14 07:36:17 -08:00
array Rollup merge of #135489 - RalfJung:TryFromSliceError, r=tgross35 2025-01-24 08:08:06 +01:00
ascii Add more precondition check tests 2024-10-09 19:34:27 -04:00
async_iter Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
cell OnceCell & OnceLock docs: Using (un)initialized consistently 2025-02-03 17:48:39 +05:30
char Rollup merge of #120580 - HTGAzureX1212:HTGAzureX1212/issue-45795, r=m-ou-se 2025-02-19 21:16:01 +01:00
clone CloneToUninit: use a private specialization trait 2024-07-29 20:44:43 +03:00
cmp Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
convert rustdoc-search: let From and Into be unboxed 2024-12-13 11:05:30 -07:00
ffi Mark extern blocks as unsafe 2025-02-09 17:11:13 +00:00
fmt Rollup merge of #120580 - HTGAzureX1212:HTGAzureX1212/issue-45795, r=m-ou-se 2025-02-19 21:16:01 +01:00
future update cfg(bootstrap) 2025-01-08 21:26:39 +01:00
hash fmt 2025-01-08 22:11:33 +01:00
intrinsics core: Apply unsafe_op_in_unsafe_fn 2025-02-14 07:36:17 -08:00
io Add inherent versions of MaybeUninit methods for slices 2025-01-11 23:57:00 -05:00
iter Fix typo in hidden internal docs of TrustedRandomAccess 2025-02-18 17:54:56 +01:00
macros Rename rustc_contract to contract 2025-02-03 13:55:15 -08:00
marker Implement phantom variance markers 2025-01-26 04:57:11 +00:00
mem Rollup merge of #136986 - ehuss:library-unsafe-fun, r=Noratrieb 2025-02-16 17:14:03 +01:00
net Auto merge of #134286 - Urgau:unreach_pub-std, r=ibraheemdev 2025-01-20 23:34:04 +00:00
num Rollup merge of #134340 - Urgau:stabilize-num_midpoint_signed, r=scottmcm 2025-02-20 14:58:16 -08:00
ops add IntoBounds trait 2025-02-12 17:38:44 -07:00
panic replace placeholder version 2024-11-27 12:10:21 +00:00
prelude Remove the common prelude module 2025-02-11 13:04:27 -08:00
ptr core: Apply unsafe_op_in_unsafe_fn 2025-02-14 07:36:17 -08:00
range Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
slice Auto merge of #136771 - scottmcm:poke-slice-iter-next, r=joboet 2025-02-20 18:20:40 +00:00
str Auto merge of #137295 - matthiaskrgr:rollup-tdu3t39, r=matthiaskrgr 2025-02-20 02:39:28 +00:00
sync atomic: extend compare_and_swap migration docs 2025-01-30 14:40:08 +01:00
task docs: fix broken intra-doc links that never worked 2025-02-15 12:21:38 -07:00
unicode core: add #![warn(unreachable_pub)] 2025-01-20 18:35:32 +01:00
any.rs include note on variance and example 2025-02-11 09:20:59 +01:00
arch.rs core: #[allow(unreachable_pub)] on unreachable pub use 2025-01-20 18:35:32 +01:00
ascii.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
asserting.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
bool.rs Rollup merge of #133964 - joboet:select_unpredictable, r=tgross35 2025-01-04 09:54:36 +01:00
borrow.rs Suggest borrowing on fn argument that is impl AsRef 2024-05-09 23:25:31 +00:00
bstr.rs Add doc aliases for BStr and BString 2025-01-22 09:19:24 +02:00
cell.rs Rustfmt 2025-02-08 22:12:13 +00:00
clone.rs Implement CloneToUninit for ByteStr 2025-01-22 17:53:39 +02:00
cmp.rs implement all min/max fns in terms of </is_lt 2025-01-31 05:45:12 +01:00
contracts.rs Rename rustc_contract to contract 2025-02-03 13:55:15 -08:00
default.rs update cfgs 2024-09-05 17:24:01 +01:00
error.md Mention core's PanicInfo in error.md. 2024-06-11 15:47:00 +02:00
error.rs Add an example for std::error::Error 2025-02-16 08:14:41 +00:00
escape.rs core: add #![warn(unreachable_pub)] 2025-01-20 18:35:32 +01:00
hint.rs core: Apply unsafe_op_in_unsafe_fn 2025-02-14 07:36:17 -08:00
internal_macros.rs Fix doc nits 2024-07-26 13:26:33 +01:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Rollup merge of #134090 - veluca93:stable-tf11, r=oli-obk 2025-02-12 20:09:56 -05:00
marker.rs Implement and use BikeshedGuaranteedNoDrop for union/unsafe field validity 2025-02-13 03:45:04 +00:00
option.rs Less unwrap() in documentation 2024-12-21 01:26:47 +00:00
panic.rs update cfgs 2024-11-27 15:14:54 +00:00
panicking.rs Mark extern blocks as unsafe 2025-02-09 17:11:13 +00:00
pat.rs Rename core_pattern_type and core_pattern_types lib feature gates to pattern_type_macro 2024-12-04 16:16:24 +00:00
pin.rs docs: fix typo in std::pin overview 2025-01-24 23:26:02 -07:00
primitive.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
primitive_docs.rs Docs for f16 and f128: correct a typo and add details 2025-02-02 19:48:57 -08:00
random.rs random: add tracking issue, address other comments 2024-09-23 10:36:16 +02:00
range.rs add IntoBounds trait 2025-02-12 17:38:44 -07:00
result.rs Less unwrap() in documentation 2024-12-21 01:26:47 +00:00
time.rs Update a bunch of library types for MCP807 2025-01-09 23:47:11 -08:00
tuple.rs update cfgs 2024-09-05 17:24:01 +01:00
ub_checks.rs Make ub_check message clear that it's not an assert 2025-02-16 00:56:09 +00:00
unit.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00
unsafe_binder.rs Add unwrap_unsafe_binder and wrap_unsafe_binder macro operators 2024-12-12 16:29:40 +00:00