rust/library/core/src
bors 2fab321435 Auto merge of #79134 - ohadravid:nzint-div, r=dtolnay
Add `impl Div<NonZeroU{0}> for u{0}` which cannot panic

Dividing an unsigned int by a `NonZeroUxx` requires a user to write (for example, in [this SO question](https://stackoverflow.com/questions/64855738/how-to-inform-the-optimizer-that-nonzerou32get-will-never-return-zero)):

```
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
    x / y.get()
}
```

which generates a panicking-checked-div [assembly](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(fontScale:14,j:1,lang:rust,selection:(endColumn:2,endLineNumber:6,positionColumn:2,positionLineNumber:6,selectionStartColumn:2,selectionStartLineNumber:6,startColumn:2,startLineNumber:6),source:%27pub+fn+div(x:+u32,+y:+u32)+-%3E+u32+%7B%0A++++x+/+y%0A%7D%0Apub+fn+safe_div(x:+u32,+y:+std::num::NonZeroU32)+-%3E+u32+%7B%0A++++x+/+y.get()+//+an+unchecked+division+expected%0A%7D%27),l:%275%27,n:%270%27,o:%27Rust+source+%231%27,t:%270%27)),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27),(g:!((h:compiler,i:(compiler:r1470,filters:(b:%270%27,binary:%271%27,commentOnly:%270%27,demangle:%270%27,directives:%270%27,execute:%271%27,intel:%270%27,libraryCode:%271%27,trim:%271%27),fontScale:14,j:1,lang:rust,libs:!(),options:%27-O%27,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:%275%27,n:%270%27,o:%27rustc+1.47.0+(Editor+%231,+Compiler+%231)+Rust%27,t:%270%27)),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27)),l:%272%27,n:%270%27,o:%27%27,t:%270%27)),version:4).
Avoiding the `panic` currently requires `unsafe` code.

This PR adds an `impl Div<NonZeroU{0}> for u{0}` (and `impl Rem<NonZeroU{0}> for u{0}`) which calls the `unchecked_div` (and `unchecked_rem`) intrinsic without any additional checks,
making the following code compile:

```
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
    x / y
}

pub fn safe_rem(x: u32, y: std::num::NonZeroU32) -> u32 {
    x % y
}
```

The doc is set to match the regular div impl [docs](https://doc.rust-lang.org/beta/src/core/ops/arith.rs.html#460).

I've marked these as stable because (as I understand it) trait impls are automatically stable. I'm happy to change it to unstable if needed.

Following `@dtolnay` template from a similar issue:
this adds the following **stable** impls, which rely on dividing unsigned integers by nonzero integers being well defined and previously would have involved unsafe code to encode that knowledge:
```
impl Div<NonZeroU8> for u8 {
    type Output = u8;
}

impl Rem<NonZeroU8> for u8 {
    type Output = u8;
}
```
and equivalent for u16, u32, u64, u128, usize, but **not** for i8, i16, i32, i64, i128, isize (since -1/MIN is undefined).

r? `@dtolnay`
2020-12-27 13:11:06 +00:00
..
alloc Auto merge of #79045 - oli-obk:dont_rely_on_alloc_happening_for_soundness, r=TimDiekmann 2020-12-26 19:43:12 +00:00
array Added reference to tracking issue 2020-12-17 00:27:21 +01:00
char Back to #method for links on char 2020-11-30 21:18:56 +01:00
convert Rollup merge of #77339 - fusion-engineering-forks:tryfrom-nonzero-to-nonzero, r=dtolnay 2020-10-23 18:26:16 +09:00
fmt Use more std:: instead of core:: in docs for consistency, add more intra doc links 2020-12-02 00:41:53 +01:00
future Use more std:: instead of core:: in docs for consistency, add more intra doc links 2020-12-02 00:41:53 +01:00
hash fix aliasing issues in SipHasher 2020-10-31 16:26:06 +01:00
iter Rollup merge of #79360 - wchargin:wchargin-doc-iter-by-reference, r=m-ou-se 2020-12-13 11:05:22 +09:00
macros Clean up core macros documentation 2020-11-23 11:28:25 -08:00
mem write_slice(_cloned) 2020-12-15 12:21:33 -08:00
num Auto merge of #79134 - ohadravid:nzint-div, r=dtolnay 2020-12-27 13:11:06 +00:00
ops small TypeVisitor refactor 2020-12-07 15:52:59 +01:00
prelude mv std libs to library/ 2020-07-27 19:51:13 -05:00
ptr Rename AllocRef to Allocator and (de)alloc to (de)allocate 2020-12-04 14:47:15 +01:00
slice Auto merge of #80209 - erikdesjardins:ptrcmp, r=Mark-Simulacrum 2020-12-26 06:43:51 +00:00
str Add a doctest example of str::split on a slice of chars 2020-12-02 08:54:42 -08:00
sync Improve documentation on success and failure arguments 2020-12-22 12:19:46 +01:00
task Use more std:: instead of core:: in docs for consistency, add more intra doc links 2020-12-02 00:41:53 +01:00
unicode Privatize some of libcore unicode_internals 2020-12-07 16:16:42 +03:00
any.rs update tracking issue for const_type_id 2020-09-24 09:00:04 +10:00
ascii.rs Fixed some intra-docs links in library/core 2020-09-18 07:49:29 +08:00
bool.rs Stabilise then 2020-11-22 13:45:14 +00:00
borrow.rs Remove AsRef link as it is in the prelude 2020-08-10 20:29:20 +02:00
cell.rs Auto merge of #79485 - EllenNyan:stabilize_unsafe_cell_get_mut, r=m-ou-se 2020-12-18 11:39:26 +00:00
clone.rs Improve readability 2020-09-01 19:56:32 +02:00
cmp.rs Optimization for bool's PartialOrd impl 2020-12-14 23:32:52 +02:00
default.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
ffi.rs Properly define va_arg and va_list for aarch64-apple-darwin 2020-10-25 21:37:01 -04:00
hint.rs Stabilize hint::spin_loop 2020-11-06 23:41:55 +08:00
internal_macros.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
intrinsics.rs Auto merge of #79621 - usbalbin:constier_maybe_uninit, r=RalfJung 2020-12-10 10:46:38 +00:00
lazy.rs Capitalize safety comments 2020-09-08 22:26:44 -04:00
lib.rs Auto merge of #79621 - usbalbin:constier_maybe_uninit, r=RalfJung 2020-12-10 10:46:38 +00:00
marker.rs Auto merge of #77893 - petertodd:2020-impl-default-for-phantompinned, r=dtolnay 2020-11-23 07:00:30 +00:00
option.rs Improve wording of flatten() docs 2020-12-10 20:36:12 -08:00
panic.rs Use more std:: instead of core:: in docs for consistency, add more intra doc links 2020-12-02 00:41:53 +01:00
panicking.rs Bump bootstrap compiler version 2020-11-19 19:23:36 -05:00
pin.rs Add tracking issue number for pin_static_ref. 2020-10-21 16:30:41 +02:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
raw.rs Use intra-doc-links in core::{raw, ffi, pin} 2020-08-22 22:25:27 +02:00
result.rs Improve wording of flatten() docs 2020-12-10 20:36:12 -08:00
time.rs Remove Duration::MIN entirely 2020-10-27 15:48:58 -07:00
tuple.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
unit.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00