Use generic `NonZero` everywhere in `std`.

This commit is contained in:
Markus Reiter 2024-02-19 06:02:17 +01:00
parent 14ed426eec
commit e0732e42d8
No known key found for this signature in database
GPG Key ID: 245293B51702655B
3 changed files with 12 additions and 12 deletions

View File

@ -73,7 +73,7 @@
//! union Repr {
//! // holds integer (Simple/Os) variants, and
//! // provides access to the tag bits.
//! bits: NonZeroU64,
//! bits: NonZero<u64>,
//! // Tag is 0, so this is stored untagged.
//! msg: &'static SimpleMessage,
//! // Tagged (offset) `Box<Custom>` pointer.
@ -93,7 +93,7 @@
//! `io::Result<()>` and `io::Result<usize>` larger, which defeats part of
//! the motivation of this bitpacking.
//!
//! Storing everything in a `NonZeroUsize` (or some other integer) would be a
//! Storing everything in a `NonZero<usize>` (or some other integer) would be a
//! bit more traditional for pointer tagging, but it would lose provenance
//! information, couldn't be constructed from a `const fn`, and would probably
//! run into other issues as well.

View File

@ -111,7 +111,7 @@ use crate::ffi::OsStr;
use crate::fmt;
use crate::fs;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
use crate::num::NonZeroI32;
use crate::num::NonZero;
use crate::path::Path;
use crate::str;
use crate::sys::pipe::{read2, AnonPipe};
@ -1775,9 +1775,9 @@ impl ExitStatusError {
self.code_nonzero().map(Into::into)
}
/// Reports the exit code, if applicable, from an `ExitStatusError`, as a `NonZero`
/// Reports the exit code, if applicable, from an `ExitStatusError`, as a [`NonZero`].
///
/// This is exactly like [`code()`](Self::code), except that it returns a `NonZeroI32`.
/// This is exactly like [`code()`](Self::code), except that it returns a <code>[NonZero]<[i32]></code>.
///
/// Plain `code`, returning a plain integer, is provided because it is often more convenient.
/// The returned value from `code()` is indeed also nonzero; use `code_nonzero()` when you want
@ -1786,17 +1786,17 @@ impl ExitStatusError {
/// # Examples
///
/// ```
/// #![feature(exit_status_error)]
/// #![feature(exit_status_error, generic_nonzero)]
/// # if cfg!(unix) {
/// use std::num::NonZeroI32;
/// use std::num::NonZero;
/// use std::process::Command;
///
/// let bad = Command::new("false").status().unwrap().exit_ok().unwrap_err();
/// assert_eq!(bad.code_nonzero().unwrap(), NonZeroI32::try_from(1).unwrap());
/// assert_eq!(bad.code_nonzero().unwrap(), NonZero::new(1).unwrap());
/// # } // cfg!(unix)
/// ```
#[must_use]
pub fn code_nonzero(&self) -> Option<NonZeroI32> {
pub fn code_nonzero(&self) -> Option<NonZero<i32>> {
self.0.code()
}

View File

@ -165,7 +165,7 @@ use crate::fmt;
use crate::io;
use crate::marker::PhantomData;
use crate::mem::{self, forget};
use crate::num::{NonZero, NonZeroU64, NonZeroUsize};
use crate::num::NonZero;
use crate::panic;
use crate::panicking;
use crate::pin::Pin;
@ -1222,7 +1222,7 @@ impl ThreadId {
/// change across Rust versions.
#[must_use]
#[unstable(feature = "thread_id_value", issue = "67939")]
pub fn as_u64(&self) -> NonZeroU64 {
pub fn as_u64(&self) -> NonZero<u64> {
self.0
}
}
@ -1784,6 +1784,6 @@ fn _assert_sync_and_send() {
#[doc(alias = "hardware_concurrency")] // Alias for C++ `std::thread::hardware_concurrency`.
#[doc(alias = "num_cpus")] // Alias for a popular ecosystem crate which provides similar functionality.
#[stable(feature = "available_parallelism", since = "1.59.0")]
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
imp::available_parallelism()
}