From 155c7fcf844760c1b8e2b210d83909cb589c4531 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 29 Jan 2024 23:59:09 +0100 Subject: [PATCH] Use generic `NonZero` internally. --- compiler/rustc_abi/src/layout.rs | 4 +- compiler/rustc_abi/src/lib.rs | 5 +- compiler/rustc_attr/src/builtin.rs | 6 +- compiler/rustc_attr/src/lib.rs | 1 + .../src/interpret/validity.rs | 4 +- .../rustc_const_eval/src/interpret/visitor.rs | 4 +- compiler/rustc_const_eval/src/lib.rs | 1 + compiler/rustc_data_structures/src/lib.rs | 1 + .../src/stable_hasher.rs | 5 +- .../src/sync/worker_local.rs | 6 +- .../src/tagged_ptr/copy.rs | 9 +- compiler/rustc_errors/src/diagnostic_impls.rs | 2 +- compiler/rustc_errors/src/lib.rs | 5 +- compiler/rustc_feature/src/lib.rs | 17 +- compiler/rustc_hir_analysis/src/check/mod.rs | 4 +- compiler/rustc_hir_analysis/src/lib.rs | 1 + compiler/rustc_interface/src/lib.rs | 1 + compiler/rustc_interface/src/tests.rs | 4 +- compiler/rustc_interface/src/util.rs | 2 +- compiler/rustc_lint/src/lib.rs | 1 + compiler/rustc_lint/src/lints.rs | 4 +- compiler/rustc_metadata/src/lib.rs | 1 + compiler/rustc_metadata/src/rmeta/decoder.rs | 14 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 8 +- compiler/rustc_metadata/src/rmeta/mod.rs | 20 +- compiler/rustc_metadata/src/rmeta/table.rs | 6 +- compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_middle/src/middle/stability.rs | 8 +- .../rustc_middle/src/mir/interpret/mod.rs | 8 +- .../rustc_middle/src/mir/interpret/pointer.rs | 6 +- compiler/rustc_middle/src/ty/consts/int.rs | 27 +- compiler/rustc_middle/src/ty/generic_args.rs | 4 +- compiler/rustc_middle/src/ty/layout.rs | 4 +- compiler/rustc_middle/src/ty/mod.rs | 4 +- compiler/rustc_passes/src/lib.rs | 1 + compiler/rustc_passes/src/stability.rs | 4 +- compiler/rustc_query_impl/src/lib.rs | 1 + compiler/rustc_query_impl/src/plumbing.rs | 4 +- compiler/rustc_query_system/src/lib.rs | 1 + compiler/rustc_query_system/src/query/job.rs | 4 +- compiler/rustc_serialize/src/lib.rs | 1 + compiler/rustc_serialize/src/serialize.rs | 7 +- compiler/rustc_session/src/config.rs | 4 +- compiler/rustc_session/src/errors.rs | 4 +- compiler/rustc_session/src/lib.rs | 1 + compiler/rustc_session/src/options.rs | 13 +- compiler/stable_mir/src/abi.rs | 4 +- compiler/stable_mir/src/lib.rs | 1 + .../alloc/src/collections/binary_heap/mod.rs | 10 +- .../src/collections/vec_deque/into_iter.rs | 10 +- .../alloc/src/collections/vec_deque/iter.rs | 6 +- .../src/collections/vec_deque/iter_mut.rs | 6 +- library/alloc/src/lib.rs | 1 + library/alloc/src/vec/into_iter.rs | 14 +- library/alloc/tests/vec.rs | 8 +- library/alloc/tests/vec_deque.rs | 4 +- library/core/src/array/iter.rs | 10 +- library/core/src/ascii.rs | 6 +- library/core/src/char/mod.rs | 6 +- library/core/src/cmp/bytewise.rs | 4 +- library/core/src/escape.rs | 6 +- .../core/src/iter/adapters/array_chunks.rs | 8 +- .../core/src/iter/adapters/by_ref_sized.rs | 6 +- library/core/src/iter/adapters/chain.rs | 10 +- library/core/src/iter/adapters/cloned.rs | 6 +- library/core/src/iter/adapters/copied.rs | 10 +- library/core/src/iter/adapters/cycle.rs | 6 +- library/core/src/iter/adapters/enumerate.rs | 10 +- library/core/src/iter/adapters/filter.rs | 6 +- library/core/src/iter/adapters/filter_map.rs | 6 +- library/core/src/iter/adapters/flatten.rs | 46 +-- library/core/src/iter/adapters/inspect.rs | 6 +- library/core/src/iter/adapters/map.rs | 6 +- library/core/src/iter/adapters/map_while.rs | 6 +- library/core/src/iter/adapters/mod.rs | 6 +- library/core/src/iter/adapters/rev.rs | 6 +- library/core/src/iter/adapters/scan.rs | 6 +- library/core/src/iter/adapters/skip.rs | 14 +- library/core/src/iter/adapters/skip_while.rs | 6 +- library/core/src/iter/adapters/take.rs | 14 +- library/core/src/iter/adapters/take_while.rs | 6 +- library/core/src/iter/adapters/zip.rs | 6 +- library/core/src/iter/range.rs | 26 +- library/core/src/iter/sources/repeat.rs | 6 +- library/core/src/iter/sources/repeat_n.rs | 8 +- library/core/src/iter/traits/double_ended.rs | 10 +- library/core/src/iter/traits/iterator.rs | 10 +- library/core/src/iter/traits/marker.rs | 6 +- library/core/src/num/nonzero.rs | 10 +- library/core/src/ops/index_range.rs | 10 +- library/core/src/ptr/alignment.rs | 4 +- library/core/src/ptr/non_null.rs | 4 +- library/core/src/slice/iter.rs | 2 +- library/core/src/slice/iter/macros.rs | 8 +- library/core/src/slice/mod.rs | 4 +- library/core/src/str/iter.rs | 4 +- library/core/tests/array.rs | 10 +- library/core/tests/iter/adapters/chain.rs | 17 +- library/core/tests/iter/adapters/enumerate.rs | 4 +- library/core/tests/iter/adapters/flatten.rs | 6 +- library/core/tests/iter/adapters/skip.rs | 8 +- library/core/tests/iter/adapters/take.rs | 16 +- library/core/tests/iter/range.rs | 4 +- library/core/tests/iter/traits/iterator.rs | 27 +- library/core/tests/lib.rs | 1 + library/core/tests/nonzero.rs | 263 +++++++++--------- library/core/tests/ptr.rs | 6 +- library/core/tests/result.rs | 13 +- library/core/tests/slice.rs | 6 +- library/proc_macro/src/bridge/handle.rs | 6 +- library/proc_macro/src/bridge/symbol.rs | 8 +- library/proc_macro/src/lib.rs | 1 + library/std/src/sys/pal/hermit/thread.rs | 6 +- library/std/src/sys/pal/sgx/abi/tls/mod.rs | 6 +- .../std/src/sys/pal/sgx/abi/usercalls/raw.rs | 10 +- library/std/src/sys/pal/sgx/rwlock.rs | 8 +- library/std/src/sys/pal/sgx/thread.rs | 4 +- library/std/src/sys/pal/sgx/waitqueue/mod.rs | 4 +- library/std/src/sys/pal/teeos/thread.rs | 4 +- library/std/src/sys/pal/unix/thread.rs | 18 +- library/std/src/sys/pal/unsupported/thread.rs | 4 +- library/std/src/sys/pal/wasi/thread.rs | 4 +- .../std/src/sys/pal/wasm/atomics/thread.rs | 4 +- library/std/src/sys/pal/windows/args.rs | 16 +- library/std/src/sys/pal/windows/thread.rs | 6 +- library/std/src/sys/pal/xous/thread.rs | 6 +- library/std/src/sys_common/wstr.rs | 13 +- library/std/src/thread/mod.rs | 9 +- .../clippy/clippy_lints/src/transmute/mod.rs | 2 +- .../tests/ui/arithmetic_side_effects.rs | 2 +- src/tools/clippy/tests/ui/eager_transmute.rs | 6 +- .../tests/ui/transmute_int_to_non_zero.rs | 20 +- src/tools/miri/src/borrow_tracker/mod.rs | 6 +- src/tools/miri/src/concurrency/sync.rs | 4 +- src/tools/miri/src/shims/foreign_items.rs | 2 +- src/tools/miri/tests/compiletest.rs | 2 +- .../function_pointers/abi_mismatch_repr_C.rs | 2 +- .../available-parallelism-miri-num-cpus.rs | 2 +- .../tests/pass/function_calls/abi_compat.rs | 6 +- tests/debuginfo/msvc-pretty-enums.rs | 4 +- tests/debuginfo/numeric-types.rs | 26 +- tests/ui/consts/ice-48279.rs | 2 +- tests/ui/consts/tuple-struct-constructors.rs | 2 +- .../non_zero_assigned_something.rs | 4 +- .../overflowing-neg-nonzero.rs | 2 +- tests/ui/print_type_sizes/niche-filling.rs | 2 +- tests/ui/structs-enums/type-sizes.rs | 2 +- .../core-std-import-order-issue-83564.rs | 2 +- 148 files changed, 657 insertions(+), 608 deletions(-) diff --git a/compiler/rustc_abi/src/layout.rs b/compiler/rustc_abi/src/layout.rs index ec3ab828b7194..7bfd7885fdbe2 100644 --- a/compiler/rustc_abi/src/layout.rs +++ b/compiler/rustc_abi/src/layout.rs @@ -7,7 +7,7 @@ use tracing::debug; use crate::{ Abi, AbiAndPrefAlign, Align, FieldsShape, IndexSlice, IndexVec, Integer, LayoutS, Niche, - NonZeroUsize, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout, + NonZero, Primitive, ReprOptions, Scalar, Size, StructKind, TagEncoding, TargetDataLayout, Variants, WrappingRange, }; @@ -324,7 +324,7 @@ pub trait LayoutCalculator { Some(LayoutS { variants: Variants::Single { index: VariantIdx::new(0) }, - fields: FieldsShape::Union(NonZeroUsize::new(only_variant.len())?), + fields: FieldsShape::Union(NonZero::::new(only_variant.len())?), abi, largest_niche: None, align, diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index c45a4a410f964..020b41d65d53d 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -1,10 +1,11 @@ +#![cfg_attr(feature = "nightly", feature(generic_nonzero))] #![cfg_attr(feature = "nightly", feature(step_trait))] #![cfg_attr(feature = "nightly", allow(internal_features))] #![cfg_attr(feature = "nightly", doc(rust_logo))] #![cfg_attr(feature = "nightly", feature(rustdoc_internals))] use std::fmt; -use std::num::{NonZeroUsize, ParseIntError}; +use std::num::{NonZero, ParseIntError}; use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub}; use std::str::FromStr; @@ -1124,7 +1125,7 @@ pub enum FieldsShape { Primitive, /// All fields start at no offset. The `usize` is the field count. - Union(NonZeroUsize), + Union(NonZero), /// Array/vector-like placement, with all fields of identical types. Array { stride: Size, count: u64 }, diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index a3783db5f80d6..f414ff746bb82 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -13,7 +13,7 @@ use rustc_session::parse::feature_err; use rustc_session::{RustcVersion, Session}; use rustc_span::hygiene::Transparency; use rustc_span::{symbol::sym, symbol::Symbol, Span}; -use std::num::NonZeroU32; +use std::num::NonZero; use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause}; @@ -113,7 +113,7 @@ pub enum StabilityLevel { /// Reason for the current stability level. reason: UnstableReason, /// Relevant `rust-lang/rust` issue. - issue: Option, + issue: Option>, is_soft: bool, /// If part of a feature is stabilized and a new feature is added for the remaining parts, /// then the `implied_by` attribute is used to indicate which now-stable feature previously @@ -442,7 +442,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil // is a name/value pair string literal. issue_num = match issue.unwrap().as_str() { "none" => None, - issue => match issue.parse::() { + issue => match issue.parse::>() { Ok(num) => Some(num), Err(err) => { sess.dcx().emit_err( diff --git a/compiler/rustc_attr/src/lib.rs b/compiler/rustc_attr/src/lib.rs index 3c5bcecddeaa4..8f995f756a3b3 100644 --- a/compiler/rustc_attr/src/lib.rs +++ b/compiler/rustc_attr/src/lib.rs @@ -7,6 +7,7 @@ #![allow(internal_features)] #![feature(rustdoc_internals)] #![doc(rust_logo)] +#![feature(generic_nonzero)] #![feature(let_chains)] #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index b5cd32595201f..0979177c0a4c5 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -5,7 +5,7 @@ //! to be const-safe. use std::fmt::Write; -use std::num::NonZeroUsize; +use std::num::NonZero; use either::{Left, Right}; @@ -780,7 +780,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> fn visit_union( &mut self, op: &OpTy<'tcx, M::Provenance>, - _fields: NonZeroUsize, + _fields: NonZero, ) -> InterpResult<'tcx> { // Special check for CTFE validation, preventing `UnsafeCell` inside unions in immutable memory. if self.ctfe_mode.is_some_and(|c| !c.allow_immutable_unsafe_cell()) { diff --git a/compiler/rustc_const_eval/src/interpret/visitor.rs b/compiler/rustc_const_eval/src/interpret/visitor.rs index de0590a4b14de..67f778230e1ed 100644 --- a/compiler/rustc_const_eval/src/interpret/visitor.rs +++ b/compiler/rustc_const_eval/src/interpret/visitor.rs @@ -7,7 +7,7 @@ use rustc_middle::ty; use rustc_target::abi::FieldIdx; use rustc_target::abi::{FieldsShape, VariantIdx, Variants}; -use std::num::NonZeroUsize; +use std::num::NonZero; use super::{InterpCx, MPlaceTy, Machine, Projectable}; @@ -43,7 +43,7 @@ pub trait ValueVisitor<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized { } /// Visits the given value as a union. No automatic recursion can happen here. #[inline(always)] - fn visit_union(&mut self, _v: &Self::V, _fields: NonZeroUsize) -> InterpResult<'tcx> { + fn visit_union(&mut self, _v: &Self::V, _fields: NonZero) -> InterpResult<'tcx> { Ok(()) } /// Visits the given value as the pointer of a `Box`. There is nothing to recurse into. diff --git a/compiler/rustc_const_eval/src/lib.rs b/compiler/rustc_const_eval/src/lib.rs index 946a49982aa1a..30d1d236749d6 100644 --- a/compiler/rustc_const_eval/src/lib.rs +++ b/compiler/rustc_const_eval/src/lib.rs @@ -11,6 +11,7 @@ Rust MIR: a lowered representation of Rust. #![feature(assert_matches)] #![feature(box_patterns)] #![feature(decl_macro)] +#![feature(generic_nonzero)] #![feature(let_chains)] #![feature(slice_ptr_get)] #![feature(never_type)] diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index 563d8501890ce..f499f2bfc312a 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -22,6 +22,7 @@ #![feature(cfg_match)] #![feature(core_intrinsics)] #![feature(extend_one)] +#![feature(generic_nonzero)] #![feature(hash_raw_entry)] #![feature(hasher_prefixfree_extras)] #![feature(lazy_cell)] diff --git a/compiler/rustc_data_structures/src/stable_hasher.rs b/compiler/rustc_data_structures/src/stable_hasher.rs index 52304c72a2f8d..15691804a94b2 100644 --- a/compiler/rustc_data_structures/src/stable_hasher.rs +++ b/compiler/rustc_data_structures/src/stable_hasher.rs @@ -6,6 +6,7 @@ use std::fmt; use std::hash::{BuildHasher, Hash, Hasher}; use std::marker::PhantomData; use std::mem; +use std::num::NonZero; #[cfg(test)] mod tests; @@ -338,14 +339,14 @@ impl HashStable for PhantomData { fn hash_stable(&self, _ctx: &mut CTX, _hasher: &mut StableHasher) {} } -impl HashStable for ::std::num::NonZeroU32 { +impl HashStable for NonZero { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { self.get().hash_stable(ctx, hasher) } } -impl HashStable for ::std::num::NonZeroUsize { +impl HashStable for NonZero { #[inline] fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { self.get().hash_stable(ctx, hasher) diff --git a/compiler/rustc_data_structures/src/sync/worker_local.rs b/compiler/rustc_data_structures/src/sync/worker_local.rs index b34d3dd904400..50a614a1b0279 100644 --- a/compiler/rustc_data_structures/src/sync/worker_local.rs +++ b/compiler/rustc_data_structures/src/sync/worker_local.rs @@ -1,7 +1,7 @@ use parking_lot::Mutex; use std::cell::Cell; use std::cell::OnceCell; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::ops::Deref; use std::ptr; use std::sync::Arc; @@ -31,7 +31,7 @@ impl RegistryId { } struct RegistryData { - thread_limit: NonZeroUsize, + thread_limit: NonZero, threads: Mutex, } @@ -61,7 +61,7 @@ thread_local! { impl Registry { /// Creates a registry which can hold up to `thread_limit` threads. - pub fn new(thread_limit: NonZeroUsize) -> Self { + pub fn new(thread_limit: NonZero) -> Self { Registry(Arc::new(RegistryData { thread_limit, threads: Mutex::new(0) })) } diff --git a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs index e893a2c781346..8af4042ad875b 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs @@ -4,7 +4,7 @@ use std::fmt; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::mem::ManuallyDrop; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::ops::{Deref, DerefMut}; use std::ptr::NonNull; @@ -134,7 +134,7 @@ where ptr.map_addr(|addr| { // Safety: - // - The pointer is `NonNull` => it's address is `NonZeroUsize` + // - The pointer is `NonNull` => it's address is `NonZero` // - `P::BITS` least significant bits are always zero (`Pointer` contract) // - `T::BITS <= P::BITS` (from `Self::ASSERTION`) // @@ -143,14 +143,15 @@ where // `{non_zero} | packed_tag` can't make the value zero. let packed = (addr.get() >> T::BITS) | packed_tag; - unsafe { NonZeroUsize::new_unchecked(packed) } + unsafe { NonZero::::new_unchecked(packed) } }) } /// Retrieves the original raw pointer from `self.packed`. #[inline] pub(super) fn pointer_raw(&self) -> NonNull { - self.packed.map_addr(|addr| unsafe { NonZeroUsize::new_unchecked(addr.get() << T::BITS) }) + self.packed + .map_addr(|addr| unsafe { NonZero::::new_unchecked(addr.get() << T::BITS) }) } /// This provides a reference to the `P` pointer itself, rather than the diff --git a/compiler/rustc_errors/src/diagnostic_impls.rs b/compiler/rustc_errors/src/diagnostic_impls.rs index df3fd4af4b436..4a6ebc64730cb 100644 --- a/compiler/rustc_errors/src/diagnostic_impls.rs +++ b/compiler/rustc_errors/src/diagnostic_impls.rs @@ -83,7 +83,7 @@ into_diagnostic_arg_using_display!( ast::ParamKindOrd, std::io::Error, Box, - std::num::NonZeroU32, + std::num::NonZero, hir::Target, Edition, Ident, diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 6d9208341a5c0..09ab12c0915e3 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -13,6 +13,7 @@ #![feature(box_patterns)] #![feature(error_reporter)] #![feature(extract_if)] +#![feature(generic_nonzero)] #![feature(let_chains)] #![feature(min_specialization)] #![feature(negative_impls)] @@ -73,7 +74,7 @@ use std::error::Report; use std::fmt; use std::hash::Hash; use std::io::Write; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::panic; use std::path::{Path, PathBuf}; @@ -536,7 +537,7 @@ pub struct DiagCtxtFlags { pub can_emit_warnings: bool, /// If Some, the Nth error-level diagnostic is upgraded to bug-level. /// (rustc: see `-Z treat-err-as-bug`) - pub treat_err_as_bug: Option, + pub treat_err_as_bug: Option>, /// Eagerly emit delayed bugs as errors, so that the compiler debugger may /// see all of the errors being emitted at once. pub eagerly_emit_delayed_bugs: bool, diff --git a/compiler/rustc_feature/src/lib.rs b/compiler/rustc_feature/src/lib.rs index cd1d9b13daa15..72c12e61c16b3 100644 --- a/compiler/rustc_feature/src/lib.rs +++ b/compiler/rustc_feature/src/lib.rs @@ -12,6 +12,7 @@ //! symbol to the `accepted` or `removed` modules respectively. #![allow(internal_features)] +#![feature(generic_nonzero)] #![feature(rustdoc_internals)] #![doc(rust_logo)] #![feature(lazy_cell)] @@ -27,13 +28,13 @@ mod unstable; mod tests; use rustc_span::symbol::Symbol; -use std::num::NonZeroU32; +use std::num::NonZero; #[derive(Debug, Clone)] pub struct Feature { pub name: Symbol, pub since: &'static str, - issue: Option, + issue: Option>, } #[derive(Copy, Clone, Debug)] @@ -87,7 +88,7 @@ impl UnstableFeatures { } } -fn find_lang_feature_issue(feature: Symbol) -> Option { +fn find_lang_feature_issue(feature: Symbol) -> Option> { // Search in all the feature lists. if let Some(f) = UNSTABLE_FEATURES.iter().find(|f| f.feature.name == feature) { return f.feature.issue; @@ -101,21 +102,21 @@ fn find_lang_feature_issue(feature: Symbol) -> Option { panic!("feature `{feature}` is not declared anywhere"); } -const fn to_nonzero(n: Option) -> Option { - // Can be replaced with `n.and_then(NonZeroU32::new)` if that is ever usable +const fn to_nonzero(n: Option) -> Option> { + // Can be replaced with `n.and_then(NonZero::new)` if that is ever usable // in const context. Requires https://github.com/rust-lang/rfcs/pull/2632. match n { None => None, - Some(n) => NonZeroU32::new(n), + Some(n) => NonZero::::new(n), } } pub enum GateIssue { Language, - Library(Option), + Library(Option>), } -pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option { +pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option> { match issue { GateIssue::Language => find_lang_feature_issue(feature), GateIssue::Library(lib) => lib, diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 28c8f846c23a9..cca97f20af2a7 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -74,7 +74,7 @@ pub mod wfcheck; pub use check::check_abi; -use std::num::NonZeroU32; +use std::num::NonZero; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::ErrorGuaranteed; @@ -270,7 +270,7 @@ fn default_body_is_unstable( item_did: DefId, feature: Symbol, reason: Option, - issue: Option, + issue: Option>, ) { let missing_item_name = tcx.associated_item(item_did).name; let (mut some_note, mut none_note, mut reason_str) = (false, false, String::new()); diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs index 454cb97ac148b..fd794beda5bd6 100644 --- a/compiler/rustc_hir_analysis/src/lib.rs +++ b/compiler/rustc_hir_analysis/src/lib.rs @@ -61,6 +61,7 @@ This API is completely unstable and subject to change. #![feature(rustdoc_internals)] #![allow(internal_features)] #![feature(control_flow_enum)] +#![feature(generic_nonzero)] #![feature(if_let_guard)] #![feature(is_sorted)] #![feature(iter_intersperse)] diff --git a/compiler/rustc_interface/src/lib.rs b/compiler/rustc_interface/src/lib.rs index 69414a1379515..6e05d3dd540d3 100644 --- a/compiler/rustc_interface/src/lib.rs +++ b/compiler/rustc_interface/src/lib.rs @@ -1,5 +1,6 @@ #![feature(decl_macro)] #![feature(error_iter)] +#![feature(generic_nonzero)] #![feature(lazy_cell)] #![feature(let_chains)] #![feature(thread_spawn_unchecked)] diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index 2d4963a8b901a..3f7452890f167 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -20,7 +20,7 @@ use rustc_span::{FileName, SourceFileHashAlgorithm}; use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel}; use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel}; use std::collections::{BTreeMap, BTreeSet}; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -826,7 +826,7 @@ fn test_unstable_options_tracking_hash() { tracked!(tls_model, Some(TlsModel::GeneralDynamic)); tracked!(translate_remapped_path_to_local_path, false); tracked!(trap_unreachable, Some(false)); - tracked!(treat_err_as_bug, NonZeroUsize::new(1)); + tracked!(treat_err_as_bug, NonZero::::new(1)); tracked!(tune_cpu, Some(String::from("abc"))); tracked!(uninit_const_chunk_threshold, 123); tracked!(unleash_the_miri_inside_of_you, true); diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 76b9e8de75fb0..00cf84138bac6 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -107,7 +107,7 @@ pub(crate) fn run_in_thread_pool_with_globals R + Send, R: Send>( use rustc_query_impl::QueryCtxt; use rustc_query_system::query::{deadlock, QueryContext}; - let registry = sync::Registry::new(std::num::NonZeroUsize::new(threads).unwrap()); + let registry = sync::Registry::new(std::num::NonZero::::new(threads).unwrap()); if !sync::is_dyn_thread_safe() { return run_in_thread_with_globals(edition, || { diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 1d9ce10bcaf38..70fd7a794392a 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -31,6 +31,7 @@ #![feature(array_windows)] #![feature(box_patterns)] #![feature(control_flow_enum)] +#![feature(generic_nonzero)] #![feature(if_let_guard)] #![feature(iter_order_by)] #![feature(let_chains)] diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index f916deb4a46d1..d179e6c0d1bc2 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1,6 +1,6 @@ #![allow(rustc::untranslatable_diagnostic)] #![allow(rustc::diagnostic_outside_of_impl)] -use std::num::NonZeroU32; +use std::num::NonZero; use crate::errors::RequestedLevel; use crate::fluent_generated as fluent; @@ -407,7 +407,7 @@ pub struct BuiltinIncompleteFeaturesHelp; #[derive(Subdiagnostic)] #[note(lint_note)] pub struct BuiltinFeatureIssueNote { - pub n: NonZeroU32, + pub n: NonZero, } pub struct BuiltinUnpermittedTypeInit<'a> { diff --git a/compiler/rustc_metadata/src/lib.rs b/compiler/rustc_metadata/src/lib.rs index 471425e80174f..6edeee003f090 100644 --- a/compiler/rustc_metadata/src/lib.rs +++ b/compiler/rustc_metadata/src/lib.rs @@ -5,6 +5,7 @@ #![feature(decl_macro)] #![feature(extract_if)] #![feature(coroutines)] +#![feature(generic_nonzero)] #![feature(iter_from_coroutine)] #![feature(let_chains)] #![feature(if_let_guard)] diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 20e3ae3ba9492..7ad4f391b2421 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -325,7 +325,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { } #[inline] - fn read_lazy_offset_then(&mut self, f: impl Fn(NonZeroUsize) -> T) -> T { + fn read_lazy_offset_then(&mut self, f: impl Fn(NonZero) -> T) -> T { let distance = self.read_usize(); let position = match self.lazy_state { LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"), @@ -336,7 +336,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { } LazyState::Previous(last_pos) => last_pos.get() + distance, }; - let position = NonZeroUsize::new(position).unwrap(); + let position = NonZero::::new(position).unwrap(); self.lazy_state = LazyState::Previous(position); f(position) } @@ -683,15 +683,17 @@ impl MetadataBlob { } pub(crate) fn get_rustc_version(&self) -> String { - LazyValue::::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 8).unwrap()) - .decode(self) + LazyValue::::from_position( + NonZero::::new(METADATA_HEADER.len() + 8).unwrap(), + ) + .decode(self) } - fn root_pos(&self) -> NonZeroUsize { + fn root_pos(&self) -> NonZero { let offset = METADATA_HEADER.len(); let pos_bytes = self.blob()[offset..][..8].try_into().unwrap(); let pos = u64::from_le_bytes(pos_bytes); - NonZeroUsize::new(pos as usize).unwrap() + NonZero::::new(pos as usize).unwrap() } pub(crate) fn get_header(&self) -> CrateHeader { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 542caf8622385..1e5accc09a112 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -421,7 +421,7 @@ macro_rules! record_defaulted_array { } impl<'a, 'tcx> EncodeContext<'a, 'tcx> { - fn emit_lazy_distance(&mut self, position: NonZeroUsize) { + fn emit_lazy_distance(&mut self, position: NonZero) { let pos = position.get(); let distance = match self.lazy_state { LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"), @@ -439,7 +439,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { position.get() - last_pos.get() } }; - self.lazy_state = LazyState::Previous(NonZeroUsize::new(pos).unwrap()); + self.lazy_state = LazyState::Previous(NonZero::::new(pos).unwrap()); self.emit_usize(distance); } @@ -447,7 +447,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { where T::Value<'tcx>: Encodable>, { - let pos = NonZeroUsize::new(self.position()).unwrap(); + let pos = NonZero::::new(self.position()).unwrap(); assert_eq!(self.lazy_state, LazyState::NoNode); self.lazy_state = LazyState::NodeStart(pos); @@ -466,7 +466,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { where T::Value<'tcx>: Encodable>, { - let pos = NonZeroUsize::new(self.position()).unwrap(); + let pos = NonZero::::new(self.position()).unwrap(); assert_eq!(self.lazy_state, LazyState::NoNode); self.lazy_state = LazyState::NodeStart(pos); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 2f77588269345..1dc3d76001069 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -37,7 +37,7 @@ use rustc_target::abi::{FieldIdx, VariantIdx}; use rustc_target::spec::{PanicStrategy, TargetTriple}; use std::marker::PhantomData; -use std::num::NonZeroUsize; +use std::num::NonZero; use decoder::DecodeContext; pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob}; @@ -83,7 +83,7 @@ pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_V /// order than they were encoded in. #[must_use] struct LazyValue { - position: NonZeroUsize, + position: NonZero, _marker: PhantomData T>, } @@ -92,7 +92,7 @@ impl ParameterizedOverTcx for LazyValue { } impl LazyValue { - fn from_position(position: NonZeroUsize) -> LazyValue { + fn from_position(position: NonZero) -> LazyValue { LazyValue { position, _marker: PhantomData } } } @@ -108,7 +108,7 @@ impl LazyValue { /// the minimal distance the length of the sequence, i.e. /// it's assumed there's no 0-byte element in the sequence. struct LazyArray { - position: NonZeroUsize, + position: NonZero, num_elems: usize, _marker: PhantomData T>, } @@ -119,12 +119,12 @@ impl ParameterizedOverTcx for LazyArray { impl Default for LazyArray { fn default() -> LazyArray { - LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0) + LazyArray::from_position_and_num_elems(NonZero::::new(1).unwrap(), 0) } } impl LazyArray { - fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray { + fn from_position_and_num_elems(position: NonZero, num_elems: usize) -> LazyArray { LazyArray { position, num_elems, _marker: PhantomData } } } @@ -135,7 +135,7 @@ impl LazyArray { /// `LazyArray`, but without requiring encoding or decoding all the values /// eagerly and in-order. struct LazyTable { - position: NonZeroUsize, + position: NonZero, /// The encoded size of the elements of a table is selected at runtime to drop /// trailing zeroes. This is the number of bytes used for each table element. width: usize, @@ -150,7 +150,7 @@ impl ParameterizedOverTcx for LazyTable LazyTable { fn from_position_and_encoded_size( - position: NonZeroUsize, + position: NonZero, width: usize, len: usize, ) -> LazyTable { @@ -187,11 +187,11 @@ enum LazyState { /// Inside a metadata node, and before any `Lazy`s. /// The position is that of the node itself. - NodeStart(NonZeroUsize), + NodeStart(NonZero), /// Inside a metadata node, with a previous `Lazy`s. /// The position is where that previous `Lazy` would start. - Previous(NonZeroUsize), + Previous(NonZero), } type SyntaxContextTable = LazyTable>>; diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 306bf07a97608..00752ad15a33a 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -339,7 +339,7 @@ impl FixedSizeEncoding for Option> { #[inline] fn from_bytes(b: &[u8; 8]) -> Self { - let position = NonZeroUsize::new(u64::from_bytes(b) as usize)?; + let position = NonZero::::new(u64::from_bytes(b) as usize)?; Some(LazyValue::from_position(position)) } @@ -366,7 +366,7 @@ impl LazyArray { } fn from_bytes_impl(position: &[u8; 8], meta: &[u8; 8]) -> Option> { - let position = NonZeroUsize::new(u64::from_bytes(position) as usize)?; + let position = NonZero::::new(u64::from_bytes(position) as usize)?; let len = u64::from_bytes(meta) as usize; Some(LazyArray::from_position_and_num_elems(position, len)) } @@ -497,7 +497,7 @@ impl> TableBui } LazyTable::from_position_and_encoded_size( - NonZeroUsize::new(pos).unwrap(), + NonZero::::new(pos).unwrap(), width, self.blocks.len(), ) diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index ddfb2ece39f10..e0c1718e37d61 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -33,6 +33,7 @@ #![feature(discriminant_kind)] #![feature(exhaustive_patterns)] #![feature(coroutines)] +#![feature(generic_nonzero)] #![feature(if_let_guard)] #![feature(inline_const)] #![feature(iter_from_coroutine)] diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index afb6937b43b94..15ef00629b986 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -21,7 +21,7 @@ use rustc_session::parse::feature_err_issue; use rustc_session::Session; use rustc_span::symbol::{sym, Symbol}; use rustc_span::Span; -use std::num::NonZeroU32; +use std::num::NonZero; #[derive(PartialEq, Clone, Copy, Debug)] pub enum StabilityLevel { @@ -102,7 +102,7 @@ pub fn report_unstable( sess: &Session, feature: Symbol, reason: Option, - issue: Option, + issue: Option>, suggestion: Option<(Span, String, String, Applicability)>, is_soft: bool, span: Span, @@ -235,7 +235,7 @@ pub enum EvalResult { Deny { feature: Symbol, reason: Option, - issue: Option, + issue: Option>, suggestion: Option<(Span, String, String, Applicability)>, is_soft: bool, }, @@ -433,7 +433,7 @@ impl<'tcx> TyCtxt<'tcx> { // the `-Z force-unstable-if-unmarked` flag present (we're // compiling a compiler crate), then let this missing feature // annotation slide. - if feature == sym::rustc_private && issue == NonZeroU32::new(27812) { + if feature == sym::rustc_private && issue == NonZero::::new(27812) { if self.sess.opts.unstable_opts.force_unstable_if_unmarked { return EvalResult::Allow; } diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 0da3524e05564..4ef02a86e30e2 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -122,7 +122,7 @@ mod value; use std::fmt; use std::io; use std::io::{Read, Write}; -use std::num::{NonZeroU32, NonZeroU64}; +use std::num::NonZero; use std::sync::atomic::{AtomicU32, Ordering}; use rustc_ast::LitKind; @@ -205,7 +205,7 @@ pub enum LitToConstError { } #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct AllocId(pub NonZeroU64); +pub struct AllocId(pub NonZero); // We want the `Debug` output to be readable as it is used by `derive(Debug)` for // all the Miri types. @@ -260,7 +260,7 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder>>( } // Used to avoid infinite recursion when decoding cyclic allocations. -type DecodingSessionId = NonZeroU32; +type DecodingSessionId = NonZero; #[derive(Clone)] enum State { @@ -500,7 +500,7 @@ impl<'tcx> AllocMap<'tcx> { AllocMap { alloc_map: Default::default(), dedup: Default::default(), - next_id: AllocId(NonZeroU64::new(1).unwrap()), + next_id: AllocId(NonZero::::new(1).unwrap()), } } fn reserve(&mut self) -> AllocId { diff --git a/compiler/rustc_middle/src/mir/interpret/pointer.rs b/compiler/rustc_middle/src/mir/interpret/pointer.rs index dabf6297aa9c8..15e12c456793b 100644 --- a/compiler/rustc_middle/src/mir/interpret/pointer.rs +++ b/compiler/rustc_middle/src/mir/interpret/pointer.rs @@ -3,7 +3,7 @@ use super::{AllocId, InterpResult}; use rustc_macros::HashStable; use rustc_target::abi::{HasDataLayout, Size}; -use std::{fmt, num::NonZeroU64}; +use std::{fmt, num::NonZero}; //////////////////////////////////////////////////////////////////////////////// // Pointer arithmetic @@ -129,7 +129,7 @@ pub trait Provenance: Copy + fmt::Debug + 'static { /// The type of provenance in the compile-time interpreter. /// This is a packed representation of an `AllocId` and an `immutable: bool`. #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct CtfeProvenance(NonZeroU64); +pub struct CtfeProvenance(NonZero); impl From for CtfeProvenance { fn from(value: AllocId) -> Self { @@ -155,7 +155,7 @@ impl CtfeProvenance { /// Returns the `AllocId` of this provenance. #[inline(always)] pub fn alloc_id(self) -> AllocId { - AllocId(NonZeroU64::new(self.0.get() & !IMMUTABLE_MASK).unwrap()) + AllocId(NonZero::::new(self.0.get() & !IMMUTABLE_MASK).unwrap()) } /// Returns whether this provenance is immutable. diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index 310cf113b11b6..25f67129b4211 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -4,7 +4,7 @@ use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_target::abi::Size; use std::fmt; -use std::num::NonZeroU8; +use std::num::NonZero; use crate::ty::TyCtxt; @@ -132,7 +132,7 @@ pub struct ScalarInt { /// The first `size` bytes of `data` are the value. /// Do not try to read less or more bytes than that. The remaining bytes must be 0. data: u128, - size: NonZeroU8, + size: NonZero, } // Cannot derive these, as the derives take references to the fields, and we @@ -161,14 +161,14 @@ impl Decodable for ScalarInt { let mut data = [0u8; 16]; let size = d.read_u8(); data[..size as usize].copy_from_slice(d.read_raw_bytes(size as usize)); - ScalarInt { data: u128::from_le_bytes(data), size: NonZeroU8::new(size).unwrap() } + ScalarInt { data: u128::from_le_bytes(data), size: NonZero::::new(size).unwrap() } } } impl ScalarInt { - pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZeroU8::new(1).unwrap() }; + pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZero::::new(1).unwrap() }; - pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZeroU8::new(1).unwrap() }; + pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZero::::new(1).unwrap() }; #[inline] pub fn size(self) -> Size { @@ -196,7 +196,7 @@ impl ScalarInt { #[inline] pub fn null(size: Size) -> Self { - Self { data: 0, size: NonZeroU8::new(size.bytes() as u8).unwrap() } + Self { data: 0, size: NonZero::::new(size.bytes() as u8).unwrap() } } #[inline] @@ -208,7 +208,7 @@ impl ScalarInt { pub fn try_from_uint(i: impl Into, size: Size) -> Option { let data = i.into(); if size.truncate(data) == data { - Some(Self { data, size: NonZeroU8::new(size.bytes() as u8).unwrap() }) + Some(Self { data, size: NonZero::::new(size.bytes() as u8).unwrap() }) } else { None } @@ -220,7 +220,7 @@ impl ScalarInt { // `into` performed sign extension, we have to truncate let truncated = size.truncate(i as u128); if size.sign_extend(truncated) as i128 == i { - Some(Self { data: truncated, size: NonZeroU8::new(size.bytes() as u8).unwrap() }) + Some(Self { data: truncated, size: NonZero::::new(size.bytes() as u8).unwrap() }) } else { None } @@ -367,7 +367,7 @@ macro_rules! from { fn from(u: $ty) -> Self { Self { data: u128::from(u), - size: NonZeroU8::new(std::mem::size_of::<$ty>() as u8).unwrap(), + size: NonZero::::new(std::mem::size_of::<$ty>() as u8).unwrap(), } } } @@ -410,7 +410,10 @@ impl TryFrom for bool { impl From for ScalarInt { #[inline] fn from(c: char) -> Self { - Self { data: c as u128, size: NonZeroU8::new(std::mem::size_of::() as u8).unwrap() } + Self { + data: c as u128, + size: NonZero::::new(std::mem::size_of::() as u8).unwrap(), + } } } @@ -437,7 +440,7 @@ impl From for ScalarInt { #[inline] fn from(f: Single) -> Self { // We trust apfloat to give us properly truncated data. - Self { data: f.to_bits(), size: NonZeroU8::new((Single::BITS / 8) as u8).unwrap() } + Self { data: f.to_bits(), size: NonZero::::new((Single::BITS / 8) as u8).unwrap() } } } @@ -453,7 +456,7 @@ impl From for ScalarInt { #[inline] fn from(f: Double) -> Self { // We trust apfloat to give us properly truncated data. - Self { data: f.to_bits(), size: NonZeroU8::new((Double::BITS / 8) as u8).unwrap() } + Self { data: f.to_bits(), size: NonZero::::new((Double::BITS / 8) as u8).unwrap() } } } diff --git a/compiler/rustc_middle/src/ty/generic_args.rs b/compiler/rustc_middle/src/ty/generic_args.rs index 5a4b86eeffd76..fa961810a1ca6 100644 --- a/compiler/rustc_middle/src/ty/generic_args.rs +++ b/compiler/rustc_middle/src/ty/generic_args.rs @@ -18,7 +18,7 @@ use core::intrinsics; use std::cmp::Ordering; use std::marker::PhantomData; use std::mem; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::ops::{ControlFlow, Deref}; use std::ptr::NonNull; @@ -144,7 +144,7 @@ impl<'tcx> GenericArg<'tcx> { #[inline] pub fn unpack(self) -> GenericArgKind<'tcx> { let ptr = unsafe { - self.ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() & !TAG_MASK)) + self.ptr.map_addr(|addr| NonZero::::new_unchecked(addr.get() & !TAG_MASK)) }; // SAFETY: use of `Interned::new_unchecked` here is ok because these // pointers were originally created from `Interned` types in `pack()`, diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 25473f52c039e..b24c38d089241 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -20,7 +20,7 @@ use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Targ use std::cmp; use std::fmt; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::ops::Bound; pub trait IntegerExt { @@ -761,7 +761,7 @@ where }; tcx.mk_layout(LayoutS { variants: Variants::Single { index: variant_index }, - fields: match NonZeroUsize::new(fields) { + fields: match NonZero::::new(fields) { Some(fields) => FieldsShape::Union(fields), None => FieldsShape::Arbitrary { offsets: IndexVec::new(), memory_index: IndexVec::new() }, }, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index dac60ae58a0ca..c598598d5f20c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -61,7 +61,7 @@ use std::fmt::Debug; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::mem; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::ops::ControlFlow; use std::ptr::NonNull; use std::{fmt, str}; @@ -929,7 +929,7 @@ impl<'tcx> Term<'tcx> { #[inline] pub fn unpack(self) -> TermKind<'tcx> { let ptr = unsafe { - self.ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() & !TAG_MASK)) + self.ptr.map_addr(|addr| NonZero::::new_unchecked(addr.get() & !TAG_MASK)) }; // SAFETY: use of `Interned::new_unchecked` here is ok because these // pointers were originally created from `Interned` types in `pack()`, diff --git a/compiler/rustc_passes/src/lib.rs b/compiler/rustc_passes/src/lib.rs index 862b76b1f6048..e8131f8cc86b0 100644 --- a/compiler/rustc_passes/src/lib.rs +++ b/compiler/rustc_passes/src/lib.rs @@ -8,6 +8,7 @@ #![doc(rust_logo)] #![feature(rustdoc_internals)] #![allow(internal_features)] +#![feature(generic_nonzero)] #![feature(let_chains)] #![feature(map_try_insert)] #![feature(min_specialization)] diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index c1fe8c2133b75..9ec0ce9579e28 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -27,7 +27,7 @@ use rustc_span::Span; use rustc_target::spec::abi::Abi; use std::mem::replace; -use std::num::NonZeroU32; +use std::num::NonZero; #[derive(PartialEq)] enum AnnotationKind { @@ -645,7 +645,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index { let stability = Stability { level: attr::StabilityLevel::Unstable { reason: UnstableReason::Default, - issue: NonZeroU32::new(27812), + issue: NonZero::::new(27812), is_soft: false, implied_by: None, }, diff --git a/compiler/rustc_query_impl/src/lib.rs b/compiler/rustc_query_impl/src/lib.rs index 818a67a7debf6..bffd3a965f592 100644 --- a/compiler/rustc_query_impl/src/lib.rs +++ b/compiler/rustc_query_impl/src/lib.rs @@ -3,6 +3,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![doc(rust_logo)] #![feature(rustdoc_internals)] +#![feature(generic_nonzero)] #![feature(min_specialization)] #![feature(rustc_attrs)] #![allow(rustc::potential_query_instability, unused_parens)] diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index a827717d9bbd1..8cbcce986a1a3 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -30,7 +30,7 @@ use rustc_serialize::Decodable; use rustc_serialize::Encodable; use rustc_session::Limit; use rustc_span::def_id::LOCAL_CRATE; -use std::num::NonZeroU64; +use std::num::NonZero; use thin_vec::ThinVec; #[derive(Copy, Clone)] @@ -68,7 +68,7 @@ impl QueryContext for QueryCtxt<'_> { #[inline] fn next_job_id(self) -> QueryJobId { QueryJobId( - NonZeroU64::new( + NonZero::::new( self.query_system.jobs.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ) .unwrap(), diff --git a/compiler/rustc_query_system/src/lib.rs b/compiler/rustc_query_system/src/lib.rs index 9b66b9a48d905..fa3698790947e 100644 --- a/compiler/rustc_query_system/src/lib.rs +++ b/compiler/rustc_query_system/src/lib.rs @@ -1,5 +1,6 @@ #![feature(assert_matches)] #![feature(core_intrinsics)] +#![feature(generic_nonzero)] #![feature(hash_raw_entry)] #![feature(min_specialization)] #![feature(let_chains)] diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs index 3ef9de7da74b2..a5acb1f91e180 100644 --- a/compiler/rustc_query_system/src/query/job.rs +++ b/compiler/rustc_query_system/src/query/job.rs @@ -11,7 +11,7 @@ use rustc_span::Span; use std::hash::Hash; use std::io::Write; -use std::num::NonZeroU64; +use std::num::NonZero; #[cfg(parallel_compiler)] use { @@ -36,7 +36,7 @@ pub type QueryMap = FxHashMap; /// A value uniquely identifying an active query job. #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] -pub struct QueryJobId(pub NonZeroU64); +pub struct QueryJobId(pub NonZero); impl QueryJobId { fn query(self, map: &QueryMap) -> QueryStackFrame { diff --git a/compiler/rustc_serialize/src/lib.rs b/compiler/rustc_serialize/src/lib.rs index 9c34aa9395764..1e27dca80bcf0 100644 --- a/compiler/rustc_serialize/src/lib.rs +++ b/compiler/rustc_serialize/src/lib.rs @@ -11,6 +11,7 @@ #![feature(associated_type_bounds)] #![feature(const_option)] #![feature(core_intrinsics)] +#![feature(generic_nonzero)] #![feature(inline_const)] #![feature(min_specialization)] #![feature(never_type)] diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 287e317b10f33..a38a4a916fb82 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -6,6 +6,7 @@ use std::cell::{Cell, RefCell}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; use std::hash::{BuildHasher, Hash}; use std::marker::PhantomData; +use std::num::NonZero; use std::path; use std::rc::Rc; use std::sync::Arc; @@ -216,15 +217,15 @@ impl Decodable for ! { } } -impl Encodable for ::std::num::NonZeroU32 { +impl Encodable for NonZero { fn encode(&self, s: &mut S) { s.emit_u32(self.get()); } } -impl Decodable for ::std::num::NonZeroU32 { +impl Decodable for NonZero { fn decode(d: &mut D) -> Self { - ::std::num::NonZeroU32::new(d.read_u32()).unwrap() + NonZero::::new(d.read_u32()).unwrap() } } diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 2ec1a726cef1f..48cac728819b9 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -3226,7 +3226,7 @@ pub(crate) mod dep_tracking { }; use std::collections::BTreeMap; use std::hash::{DefaultHasher, Hash}; - use std::num::NonZeroUsize; + use std::num::NonZero; use std::path::PathBuf; pub trait DepTrackingHash { @@ -3268,7 +3268,7 @@ pub(crate) mod dep_tracking { impl_dep_tracking_hash_via_hash!( bool, usize, - NonZeroUsize, + NonZero, u64, Hash64, String, diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index c36cec6f35327..192dbb05530f3 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -1,4 +1,4 @@ -use std::num::NonZeroU32; +use std::num::NonZero; use rustc_ast::token; use rustc_ast::util::literal::LitError; @@ -26,7 +26,7 @@ impl<'a> IntoDiagnostic<'a> for FeatureGateError { #[derive(Subdiagnostic)] #[note(session_feature_diagnostic_for_issue)] pub struct FeatureDiagnosticForIssue { - pub n: NonZeroU32, + pub n: NonZero, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_session/src/lib.rs b/compiler/rustc_session/src/lib.rs index 0d6328fbb071c..137c0fcdb1d18 100644 --- a/compiler/rustc_session/src/lib.rs +++ b/compiler/rustc_session/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(generic_nonzero)] #![feature(let_chains)] #![feature(lazy_cell)] #![feature(option_get_or_insert_default)] diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index d8d201d5f244d..88c284e06686c 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -21,7 +21,7 @@ use rustc_span::SourceFileHashAlgorithm; use std::collections::BTreeMap; use std::hash::{DefaultHasher, Hasher}; -use std::num::{IntErrorKind, NonZeroUsize}; +use std::num::{IntErrorKind, NonZero}; use std::path::PathBuf; use std::str; @@ -617,7 +617,7 @@ mod parse { pub(crate) fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool { match v.and_then(|s| s.parse().ok()) { Some(0) => { - *slot = std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get); + *slot = std::thread::available_parallelism().map_or(1, NonZero::::get); true } Some(i) => { @@ -991,7 +991,10 @@ mod parse { true } - pub(crate) fn parse_treat_err_as_bug(slot: &mut Option, v: Option<&str>) -> bool { + pub(crate) fn parse_treat_err_as_bug( + slot: &mut Option>, + v: Option<&str>, + ) -> bool { match v { Some(s) => match s.parse() { Ok(val) => { @@ -1004,7 +1007,7 @@ mod parse { } }, None => { - *slot = NonZeroUsize::new(1); + *slot = NonZero::::new(1); true } } @@ -1948,7 +1951,7 @@ written to standard error output)"), "translate remapped paths into local paths when possible (default: yes)"), trap_unreachable: Option = (None, parse_opt_bool, [TRACKED], "generate trap instructions for unreachable intrinsics (default: use target setting, usually yes)"), - treat_err_as_bug: Option = (None, parse_treat_err_as_bug, [TRACKED], + treat_err_as_bug: Option> = (None, parse_treat_err_as_bug, [TRACKED], "treat the `val`th error that occurs as bug (default if not specified: 0 - don't treat errors as bugs. \ default if specified without a value: 1 - treat the first error as bug)"), trim_diagnostic_paths: bool = (true, parse_bool, [UNTRACKED], diff --git a/compiler/stable_mir/src/abi.rs b/compiler/stable_mir/src/abi.rs index 53dac6abefb30..8e6926ec2876b 100644 --- a/compiler/stable_mir/src/abi.rs +++ b/compiler/stable_mir/src/abi.rs @@ -2,7 +2,7 @@ use crate::compiler_interface::with; use crate::mir::FieldIdx; use crate::ty::{Align, IndexedVal, Size, Ty, VariantIdx}; use crate::Opaque; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::ops::RangeInclusive; /// A function ABI definition. @@ -129,7 +129,7 @@ pub enum FieldsShape { Primitive, /// All fields start at no offset. The `usize` is the field count. - Union(NonZeroUsize), + Union(NonZero), /// Array/vector-like placement, with all fields of identical types. Array { stride: Size, count: u64 }, diff --git a/compiler/stable_mir/src/lib.rs b/compiler/stable_mir/src/lib.rs index 4f57f532a40c9..0a7ce3a01b92b 100644 --- a/compiler/stable_mir/src/lib.rs +++ b/compiler/stable_mir/src/lib.rs @@ -16,6 +16,7 @@ //! //! The goal is to eventually be published on //! [crates.io](https://crates.io). +#![feature(generic_nonzero)] #[macro_use] extern crate scoped_tls; diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index 00a101541c589..3a82fb0df8833 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -147,7 +147,7 @@ use core::alloc::Allocator; use core::fmt; use core::iter::{FusedIterator, InPlaceIterable, SourceIter, TrustedFused, TrustedLen}; use core::mem::{self, swap, ManuallyDrop}; -use core::num::NonZeroUsize; +use core::num::NonZero; use core::ops::{Deref, DerefMut}; use core::ptr; @@ -296,7 +296,7 @@ pub struct PeekMut< heap: &'a mut BinaryHeap, // If a set_len + sift_down are required, this is Some. If a &mut T has not // yet been exposed to peek_mut()'s caller, it's None. - original_len: Option, + original_len: Option>, } #[stable(feature = "collection_debug", since = "1.17.0")] @@ -350,7 +350,7 @@ impl DerefMut for PeekMut<'_, T, A> { // the standard library as "leak amplification". unsafe { // SAFETY: len > 1 so len != 0. - self.original_len = Some(NonZeroUsize::new_unchecked(len)); + self.original_len = Some(NonZero::::new_unchecked(len)); // SAFETY: len > 1 so all this does for now is leak elements, // which is safe. self.heap.data.set_len(1); @@ -1576,8 +1576,8 @@ unsafe impl SourceIter for IntoIter { #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] unsafe impl InPlaceIterable for IntoIter { - const EXPAND_BY: Option = NonZeroUsize::new(1); - const MERGE_BY: Option = NonZeroUsize::new(1); + const EXPAND_BY: Option> = NonZero::::new(1); + const MERGE_BY: Option> = NonZero::::new(1); } unsafe impl AsVecIntoIter for IntoIter { diff --git a/library/alloc/src/collections/vec_deque/into_iter.rs b/library/alloc/src/collections/vec_deque/into_iter.rs index d9e274df0f5f2..02ab3f79b0641 100644 --- a/library/alloc/src/collections/vec_deque/into_iter.rs +++ b/library/alloc/src/collections/vec_deque/into_iter.rs @@ -1,5 +1,5 @@ use core::iter::{FusedIterator, TrustedLen}; -use core::num::NonZeroUsize; +use core::num::NonZero; use core::{array, fmt, mem::MaybeUninit, ops::Try, ptr}; use crate::alloc::{Allocator, Global}; @@ -54,7 +54,7 @@ impl Iterator for IntoIter { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let len = self.inner.len; let rem = if len < n { self.inner.clear(); @@ -63,7 +63,7 @@ impl Iterator for IntoIter { self.inner.drain(..n); 0 }; - NonZeroUsize::new(rem).map_or(Ok(()), Err) + NonZero::::new(rem).map_or(Ok(()), Err) } #[inline] @@ -183,7 +183,7 @@ impl DoubleEndedIterator for IntoIter { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { let len = self.inner.len; let rem = if len < n { self.inner.clear(); @@ -192,7 +192,7 @@ impl DoubleEndedIterator for IntoIter { self.inner.truncate(len - n); 0 }; - NonZeroUsize::new(rem).map_or(Ok(()), Err) + NonZero::::new(rem).map_or(Ok(()), Err) } fn try_rfold(&mut self, mut init: B, mut f: F) -> R diff --git a/library/alloc/src/collections/vec_deque/iter.rs b/library/alloc/src/collections/vec_deque/iter.rs index 646a2a991e701..5a5e7f70854d8 100644 --- a/library/alloc/src/collections/vec_deque/iter.rs +++ b/library/alloc/src/collections/vec_deque/iter.rs @@ -1,5 +1,5 @@ use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce}; -use core::num::NonZeroUsize; +use core::num::NonZero; use core::ops::Try; use core::{fmt, mem, slice}; @@ -56,7 +56,7 @@ impl<'a, T> Iterator for Iter<'a, T> { } } - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let remaining = self.i1.advance_by(n); match remaining { Ok(()) => return Ok(()), @@ -128,7 +128,7 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> { } } - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { match self.i2.advance_back_by(n) { Ok(()) => return Ok(()), Err(n) => { diff --git a/library/alloc/src/collections/vec_deque/iter_mut.rs b/library/alloc/src/collections/vec_deque/iter_mut.rs index 7defbb1090ffd..5061931afb7b7 100644 --- a/library/alloc/src/collections/vec_deque/iter_mut.rs +++ b/library/alloc/src/collections/vec_deque/iter_mut.rs @@ -1,5 +1,5 @@ use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce}; -use core::num::NonZeroUsize; +use core::num::NonZero; use core::ops::Try; use core::{fmt, mem, slice}; @@ -48,7 +48,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { } } - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { match self.i1.advance_by(n) { Ok(()) => return Ok(()), Err(remaining) => { @@ -119,7 +119,7 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { } } - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { match self.i2.advance_back_by(n) { Ok(()) => return Ok(()), Err(remaining) => { diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index ab08b483367af..08942a20ec262 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -127,6 +127,7 @@ #![feature(extend_one)] #![feature(fmt_internals)] #![feature(fn_traits)] +#![feature(generic_nonzero)] #![feature(hasher_prefixfree_extras)] #![feature(hint_assert_unchecked)] #![feature(inline_const)] diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 654ce09afcd30..8a5ba1ac551f7 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -12,7 +12,7 @@ use core::iter::{ }; use core::marker::PhantomData; use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; -use core::num::NonZeroUsize; +use core::num::NonZero; #[cfg(not(no_global_oom_handling))] use core::ops::Deref; use core::ptr::{self, NonNull}; @@ -234,7 +234,7 @@ impl Iterator for IntoIter { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let step_size = self.len().min(n); let to_drop = ptr::slice_from_raw_parts_mut(self.ptr.as_ptr(), step_size); if T::IS_ZST { @@ -248,7 +248,7 @@ impl Iterator for IntoIter { unsafe { ptr::drop_in_place(to_drop); } - NonZeroUsize::new(n - step_size).map_or(Ok(()), Err) + NonZero::::new(n - step_size).map_or(Ok(()), Err) } #[inline] @@ -336,7 +336,7 @@ impl DoubleEndedIterator for IntoIter { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { let step_size = self.len().min(n); if T::IS_ZST { // SAFETY: same as for advance_by() @@ -350,7 +350,7 @@ impl DoubleEndedIterator for IntoIter { unsafe { ptr::drop_in_place(to_drop); } - NonZeroUsize::new(n - step_size).map_or(Ok(()), Err) + NonZero::::new(n - step_size).map_or(Ok(()), Err) } } @@ -457,8 +457,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter { #[unstable(issue = "none", feature = "inplace_iteration")] #[doc(hidden)] unsafe impl InPlaceIterable for IntoIter { - const EXPAND_BY: Option = NonZeroUsize::new(1); - const MERGE_BY: Option = NonZeroUsize::new(1); + const EXPAND_BY: Option> = NonZero::::new(1); + const MERGE_BY: Option> = NonZero::::new(1); } #[unstable(issue = "none", feature = "inplace_iteration")] diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 38a68df79cc73..e872ace883c03 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1,5 +1,5 @@ use core::alloc::{Allocator, Layout}; -use core::num::NonZeroUsize; +use core::num::NonZero; use core::ptr::NonNull; use core::{assert_eq, assert_ne}; use std::alloc::System; @@ -1089,9 +1089,9 @@ fn test_into_iter_advance_by() { assert_eq!(i.advance_back_by(1), Ok(())); assert_eq!(i.as_slice(), [2, 3, 4]); - assert_eq!(i.advance_back_by(usize::MAX), Err(NonZeroUsize::new(usize::MAX - 3).unwrap())); + assert_eq!(i.advance_back_by(usize::MAX), Err(NonZero::::new(usize::MAX - 3).unwrap())); - assert_eq!(i.advance_by(usize::MAX), Err(NonZeroUsize::new(usize::MAX).unwrap())); + assert_eq!(i.advance_by(usize::MAX), Err(NonZero::::new(usize::MAX).unwrap())); assert_eq!(i.advance_by(0), Ok(())); assert_eq!(i.advance_back_by(0), Ok(())); @@ -1192,7 +1192,7 @@ fn test_from_iter_specialization_with_iterator_adapters() { .map(|(a, b)| a + b) .map_while(Option::Some) .skip(1) - .map(|e| if e != usize::MAX { Ok(std::num::NonZeroUsize::new(e)) } else { Err(()) }); + .map(|e| if e != usize::MAX { Ok(NonZero::::new(e)) } else { Err(()) }); assert_in_place_trait(&iter); let sink = iter.collect::, _>>().unwrap(); let sinkptr = sink.as_ptr(); diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs index f6fb1f73e5cf9..36f0318715c40 100644 --- a/library/alloc/tests/vec_deque.rs +++ b/library/alloc/tests/vec_deque.rs @@ -445,9 +445,9 @@ fn test_into_iter() { assert_eq!(it.next_back(), Some(3)); let mut it = VecDeque::from(vec![1, 2, 3, 4, 5]).into_iter(); - assert_eq!(it.advance_by(10), Err(NonZeroUsize::new(5).unwrap())); + assert_eq!(it.advance_by(10), Err(NonZero::::new(5).unwrap())); let mut it = VecDeque::from(vec![1, 2, 3, 4, 5]).into_iter(); - assert_eq!(it.advance_back_by(10), Err(NonZeroUsize::new(5).unwrap())); + assert_eq!(it.advance_back_by(10), Err(NonZero::::new(5).unwrap())); } } diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs index 2b22488b8ffc5..e50ae1b0d70af 100644 --- a/library/core/src/array/iter.rs +++ b/library/core/src/array/iter.rs @@ -1,6 +1,6 @@ //! Defines the `IntoIter` owned iterator for arrays. -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::{ fmt, intrinsics::transmute_unchecked, @@ -280,7 +280,7 @@ impl Iterator for IntoIter { self.next_back() } - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { // This also moves the start, which marks them as conceptually "dropped", // so if anything goes bad then our drop impl won't double-free them. let range_to_drop = self.alive.take_prefix(n); @@ -292,7 +292,7 @@ impl Iterator for IntoIter { ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(slice)); } - NonZeroUsize::new(remaining).map_or(Ok(()), Err) + NonZero::::new(remaining).map_or(Ok(()), Err) } #[inline] @@ -335,7 +335,7 @@ impl DoubleEndedIterator for IntoIter { }) } - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { // This also moves the end, which marks them as conceptually "dropped", // so if anything goes bad then our drop impl won't double-free them. let range_to_drop = self.alive.take_suffix(n); @@ -347,7 +347,7 @@ impl DoubleEndedIterator for IntoIter { ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(slice)); } - NonZeroUsize::new(remaining).map_or(Ok(()), Err) + NonZero::::new(remaining).map_or(Ok(()), Err) } } diff --git a/library/core/src/ascii.rs b/library/core/src/ascii.rs index 02867789b79dd..c29e5565d514a 100644 --- a/library/core/src/ascii.rs +++ b/library/core/src/ascii.rs @@ -12,7 +12,7 @@ use crate::escape; use crate::fmt; use crate::iter::FusedIterator; -use crate::num::NonZeroUsize; +use crate::num::NonZero; mod ascii_char; #[unstable(feature = "ascii_char", issue = "110998")] @@ -133,7 +133,7 @@ impl Iterator for EscapeDefault { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.0.advance_by(n) } } @@ -146,7 +146,7 @@ impl DoubleEndedIterator for EscapeDefault { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.0.advance_back_by(n) } } diff --git a/library/core/src/char/mod.rs b/library/core/src/char/mod.rs index 5c42912874c66..12bca0b438cc3 100644 --- a/library/core/src/char/mod.rs +++ b/library/core/src/char/mod.rs @@ -43,7 +43,7 @@ use crate::error::Error; use crate::escape; use crate::fmt::{self, Write}; use crate::iter::FusedIterator; -use crate::num::NonZeroUsize; +use crate::num::NonZero; pub(crate) use self::methods::EscapeDebugExtArgs; @@ -185,7 +185,7 @@ impl Iterator for EscapeUnicode { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.0.advance_by(n) } } @@ -260,7 +260,7 @@ impl Iterator for EscapeDefault { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.0.advance_by(n) } } diff --git a/library/core/src/cmp/bytewise.rs b/library/core/src/cmp/bytewise.rs index 2548d9e24c9db..b19eef8e25587 100644 --- a/library/core/src/cmp/bytewise.rs +++ b/library/core/src/cmp/bytewise.rs @@ -33,7 +33,7 @@ is_bytewise_comparable!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, // so we can compare them directly. is_bytewise_comparable!(bool, char, super::Ordering); -// SAFETY: Similarly, the non-zero types have a niche, but no undef and no pointers, +// SAFETY: Similarly, the `NonZero` type has a niche, but no undef and no pointers, // and they compare like their underlying numeric type. is_bytewise_comparable!( NonZeroU8, @@ -50,7 +50,7 @@ is_bytewise_comparable!( NonZeroIsize, ); -// SAFETY: The NonZero types have the "null" optimization guaranteed, and thus +// SAFETY: The `NonZero` type has the "null" optimization guaranteed, and thus // are also safe to equality-compare bitwise inside an `Option`. // The way `PartialOrd` is defined for `Option` means that this wouldn't work // for `<` or `>` on the signed types, but since we only do `==` it's fine. diff --git a/library/core/src/escape.rs b/library/core/src/escape.rs index 60b5df752ca85..143e277283e2c 100644 --- a/library/core/src/escape.rs +++ b/library/core/src/escape.rs @@ -1,7 +1,7 @@ //! Helper code for character escaping. use crate::ascii; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Range; const HEX_DIGITS: [ascii::Char; 16] = *b"0123456789abcdef".as_ascii().unwrap(); @@ -106,11 +106,11 @@ impl EscapeIterInner { self.alive.next_back().map(|i| self.data[usize::from(i)].to_u8()) } - pub fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + pub fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.alive.advance_by(n) } - pub fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + pub fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.alive.advance_back_by(n) } } diff --git a/library/core/src/iter/adapters/array_chunks.rs b/library/core/src/iter/adapters/array_chunks.rs index 946d0051ccec3..2e437b41dcebc 100644 --- a/library/core/src/iter/adapters/array_chunks.rs +++ b/library/core/src/iter/adapters/array_chunks.rs @@ -3,7 +3,7 @@ use crate::iter::adapters::SourceIter; use crate::iter::{ ByRefSized, FusedIterator, InPlaceIterable, TrustedFused, TrustedRandomAccessNoCoerce, }; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, NeverShortCircuit, Try}; /// An iterator over `N` elements of the iterator at a time. @@ -253,9 +253,9 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for ArrayChunks { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = const { - match (I::MERGE_BY, NonZeroUsize::new(N)) { + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = const { + match (I::MERGE_BY, NonZero::::new(N)) { (Some(m), Some(n)) => m.checked_mul(n), _ => None, } diff --git a/library/core/src/iter/adapters/by_ref_sized.rs b/library/core/src/iter/adapters/by_ref_sized.rs index 4e0e19ddc7822..d084bede1eba6 100644 --- a/library/core/src/iter/adapters/by_ref_sized.rs +++ b/library/core/src/iter/adapters/by_ref_sized.rs @@ -1,4 +1,4 @@ -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{NeverShortCircuit, Try}; /// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics. @@ -27,7 +27,7 @@ impl Iterator for ByRefSized<'_, I> { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { I::advance_by(self.0, n) } @@ -63,7 +63,7 @@ impl DoubleEndedIterator for ByRefSized<'_, I> { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { I::advance_back_by(self.0, n) } diff --git a/library/core/src/iter/adapters/chain.rs b/library/core/src/iter/adapters/chain.rs index c748336cd7fa0..7edfee7bf6c5f 100644 --- a/library/core/src/iter/adapters/chain.rs +++ b/library/core/src/iter/adapters/chain.rs @@ -1,5 +1,5 @@ use crate::iter::{FusedIterator, TrustedLen}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; /// An iterator that links two iterators together, in a chain. @@ -96,7 +96,7 @@ where } #[inline] - fn advance_by(&mut self, mut n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, mut n: usize) -> Result<(), NonZero> { if let Some(ref mut a) = self.a { n = match a.advance_by(n) { Ok(()) => return Ok(()), @@ -110,7 +110,7 @@ where // we don't fuse the second iterator } - NonZeroUsize::new(n).map_or(Ok(()), Err) + NonZero::::new(n).map_or(Ok(()), Err) } #[inline] @@ -182,7 +182,7 @@ where } #[inline] - fn advance_back_by(&mut self, mut n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, mut n: usize) -> Result<(), NonZero> { if let Some(ref mut b) = self.b { n = match b.advance_back_by(n) { Ok(()) => return Ok(()), @@ -196,7 +196,7 @@ where // we don't fuse the second iterator } - NonZeroUsize::new(n).map_or(Ok(()), Err) + NonZero::::new(n).map_or(Ok(()), Err) } #[inline] diff --git a/library/core/src/iter/adapters/cloned.rs b/library/core/src/iter/adapters/cloned.rs index 3de91267cf5d9..1a106ef97942b 100644 --- a/library/core/src/iter/adapters/cloned.rs +++ b/library/core/src/iter/adapters/cloned.rs @@ -3,7 +3,7 @@ use crate::iter::adapters::{ }; use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen, UncheckedIterator}; use crate::ops::Try; -use core::num::NonZeroUsize; +use core::num::NonZero; /// An iterator that clones the elements of an underlying iterator. /// @@ -185,6 +185,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Cloned { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/copied.rs b/library/core/src/iter/adapters/copied.rs index 52a5add1132a4..6d82d1581f79d 100644 --- a/library/core/src/iter/adapters/copied.rs +++ b/library/core/src/iter/adapters/copied.rs @@ -4,7 +4,7 @@ use crate::iter::adapters::{ use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen}; use crate::mem::MaybeUninit; use crate::mem::SizedTypeProperties; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; use crate::{array, ptr}; @@ -90,7 +90,7 @@ where } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.it.advance_by(n) } @@ -131,7 +131,7 @@ where } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.it.advance_back_by(n) } } @@ -272,6 +272,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Copied { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/cycle.rs b/library/core/src/iter/adapters/cycle.rs index 51bd09b6effe1..7919c040dba9b 100644 --- a/library/core/src/iter/adapters/cycle.rs +++ b/library/core/src/iter/adapters/cycle.rs @@ -1,4 +1,4 @@ -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::{iter::FusedIterator, ops::Try}; /// An iterator that repeats endlessly. @@ -82,7 +82,7 @@ where #[inline] #[rustc_inherit_overflow_checks] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let mut n = match self.iter.advance_by(n) { Ok(()) => return Ok(()), Err(rem) => rem.get(), @@ -97,7 +97,7 @@ where }; } - NonZeroUsize::new(n).map_or(Ok(()), Err) + NonZero::::new(n).map_or(Ok(()), Err) } // No `fold` override, because `fold` doesn't make much sense for `Cycle`, diff --git a/library/core/src/iter/adapters/enumerate.rs b/library/core/src/iter/adapters/enumerate.rs index 92f465ccdb4e8..ef46040f0a703 100644 --- a/library/core/src/iter/adapters/enumerate.rs +++ b/library/core/src/iter/adapters/enumerate.rs @@ -2,7 +2,7 @@ use crate::iter::adapters::{ zip::try_get_unchecked, SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce, }; use crate::iter::{FusedIterator, InPlaceIterable, TrustedFused, TrustedLen}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; /// An iterator that yields the current count and the element during iteration. @@ -115,7 +115,7 @@ where #[inline] #[rustc_inherit_overflow_checks] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let remaining = self.iter.advance_by(n); let advanced = match remaining { Ok(()) => n, @@ -206,7 +206,7 @@ where } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { // we do not need to update the count since that only tallies the number of items // consumed from the front. consuming items from the back can never reduce that. self.iter.advance_back_by(n) @@ -265,8 +265,8 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Enumerate { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } #[stable(feature = "default_iters", since = "1.70.0")] diff --git a/library/core/src/iter/adapters/filter.rs b/library/core/src/iter/adapters/filter.rs index 882f3e3bc60f5..a7f1fde6975c0 100644 --- a/library/core/src/iter/adapters/filter.rs +++ b/library/core/src/iter/adapters/filter.rs @@ -1,6 +1,6 @@ use crate::fmt; use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedFused}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; use core::array; use core::mem::{ManuallyDrop, MaybeUninit}; @@ -209,6 +209,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Filter { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/filter_map.rs b/library/core/src/iter/adapters/filter_map.rs index 81ac0eaa67e95..64bd5b3e2b668 100644 --- a/library/core/src/iter/adapters/filter_map.rs +++ b/library/core/src/iter/adapters/filter_map.rs @@ -1,6 +1,6 @@ use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedFused}; use crate::mem::{ManuallyDrop, MaybeUninit}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; use crate::{array, fmt}; @@ -210,6 +210,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for FilterMap { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/flatten.rs b/library/core/src/iter/adapters/flatten.rs index 7d6f746845e31..42396157d863d 100644 --- a/library/core/src/iter/adapters/flatten.rs +++ b/library/core/src/iter/adapters/flatten.rs @@ -4,7 +4,7 @@ use crate::iter::{ TrustedLen, }; use crate::iter::{Once, OnceWith}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; use crate::result; use crate::{array, fmt, option}; @@ -90,7 +90,7 @@ where } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.inner.advance_by(n) } @@ -135,7 +135,7 @@ where } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.inner.advance_back_by(n) } } @@ -165,13 +165,13 @@ where I: InPlaceIterable, U: BoundedSize + IntoIterator, { - const EXPAND_BY: Option = const { + const EXPAND_BY: Option> = const { match (I::EXPAND_BY, U::UPPER_BOUND) { (Some(m), Some(n)) => m.checked_mul(n), _ => None, } }; - const MERGE_BY: Option = I::MERGE_BY; + const MERGE_BY: Option> = I::MERGE_BY; } #[unstable(issue = "none", feature = "inplace_iteration")] @@ -200,7 +200,7 @@ where #[rustc_specialization_trait] #[unstable(issue = "none", feature = "inplace_iteration")] unsafe trait BoundedSize { - const UPPER_BOUND: Option = NonZeroUsize::new(1); + const UPPER_BOUND: Option> = NonZero::::new(1); } #[unstable(issue = "none", feature = "inplace_iteration")] @@ -217,31 +217,31 @@ unsafe impl BoundedSize for Once {} unsafe impl BoundedSize for OnceWith {} #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl BoundedSize for [T; N] { - const UPPER_BOUND: Option = NonZeroUsize::new(N); + const UPPER_BOUND: Option> = NonZero::::new(N); } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl BoundedSize for array::IntoIter { - const UPPER_BOUND: Option = NonZeroUsize::new(N); + const UPPER_BOUND: Option> = NonZero::::new(N); } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl BoundedSize for Filter { - const UPPER_BOUND: Option = I::UPPER_BOUND; + const UPPER_BOUND: Option> = I::UPPER_BOUND; } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl BoundedSize for FilterMap { - const UPPER_BOUND: Option = I::UPPER_BOUND; + const UPPER_BOUND: Option> = I::UPPER_BOUND; } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl BoundedSize for Map { - const UPPER_BOUND: Option = I::UPPER_BOUND; + const UPPER_BOUND: Option> = I::UPPER_BOUND; } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl BoundedSize for Copied { - const UPPER_BOUND: Option = I::UPPER_BOUND; + const UPPER_BOUND: Option> = I::UPPER_BOUND; } #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl BoundedSize for Cloned { - const UPPER_BOUND: Option = I::UPPER_BOUND; + const UPPER_BOUND: Option> = I::UPPER_BOUND; } /// An iterator that flattens one level of nesting in an iterator of things @@ -322,7 +322,7 @@ where } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.inner.advance_by(n) } @@ -367,7 +367,7 @@ where } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.inner.advance_back_by(n) } } @@ -394,13 +394,13 @@ where I: InPlaceIterable + Iterator, ::Item: IntoIterator + BoundedSize, { - const EXPAND_BY: Option = const { + const EXPAND_BY: Option> = const { match (I::EXPAND_BY, I::Item::UPPER_BOUND) { (Some(m), Some(n)) => m.checked_mul(n), _ => None, } }; - const MERGE_BY: Option = I::MERGE_BY; + const MERGE_BY: Option> = I::MERGE_BY; } #[unstable(issue = "none", feature = "inplace_iteration")] @@ -669,7 +669,7 @@ where #[inline] #[rustc_inherit_overflow_checks] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { #[inline] #[rustc_inherit_overflow_checks] fn advance(n: usize, iter: &mut U) -> ControlFlow<(), usize> { @@ -680,7 +680,9 @@ where } match self.iter_try_fold(n, advance) { - ControlFlow::Continue(remaining) => NonZeroUsize::new(remaining).map_or(Ok(()), Err), + ControlFlow::Continue(remaining) => { + NonZero::::new(remaining).map_or(Ok(()), Err) + } _ => Ok(()), } } @@ -759,7 +761,7 @@ where #[inline] #[rustc_inherit_overflow_checks] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { #[inline] #[rustc_inherit_overflow_checks] fn advance(n: usize, iter: &mut U) -> ControlFlow<(), usize> { @@ -770,7 +772,9 @@ where } match self.iter_try_rfold(n, advance) { - ControlFlow::Continue(remaining) => NonZeroUsize::new(remaining).map_or(Ok(()), Err), + ControlFlow::Continue(remaining) => { + NonZero::::new(remaining).map_or(Ok(()), Err) + } _ => Ok(()), } } diff --git a/library/core/src/iter/adapters/inspect.rs b/library/core/src/iter/adapters/inspect.rs index fd2d830b693d1..1c4656a649a37 100644 --- a/library/core/src/iter/adapters/inspect.rs +++ b/library/core/src/iter/adapters/inspect.rs @@ -1,6 +1,6 @@ use crate::fmt; use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedFused}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; /// An iterator that calls a function with a reference to each element before @@ -168,6 +168,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Inspect { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/map.rs b/library/core/src/iter/adapters/map.rs index c882c9e7f3f51..6e163e20d8ec4 100644 --- a/library/core/src/iter/adapters/map.rs +++ b/library/core/src/iter/adapters/map.rs @@ -3,7 +3,7 @@ use crate::iter::adapters::{ zip::try_get_unchecked, SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce, }; use crate::iter::{FusedIterator, InPlaceIterable, TrustedFused, TrustedLen, UncheckedIterator}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; /// An iterator that maps the values of `iter` with `f`. @@ -237,6 +237,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Map { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/map_while.rs b/library/core/src/iter/adapters/map_while.rs index bcae73cbe09cf..9ad50048c25ea 100644 --- a/library/core/src/iter/adapters/map_while.rs +++ b/library/core/src/iter/adapters/map_while.rs @@ -1,6 +1,6 @@ use crate::fmt; use crate::iter::{adapters::SourceIter, InPlaceIterable}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; /// An iterator that only accepts elements while `predicate` returns `Some(_)`. @@ -84,6 +84,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for MapWhile { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/mod.rs b/library/core/src/iter/adapters/mod.rs index 4037e2e2839c0..cc514bd914f14 100644 --- a/library/core/src/iter/adapters/mod.rs +++ b/library/core/src/iter/adapters/mod.rs @@ -1,5 +1,5 @@ use crate::iter::InPlaceIterable; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try}; mod array_chunks; @@ -234,6 +234,6 @@ unsafe impl InPlaceIterable for GenericShunt<'_, I, R> where I: InPlaceIterable, { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/rev.rs b/library/core/src/iter/adapters/rev.rs index 4aaf7c61f50d6..06ab15d5e900d 100644 --- a/library/core/src/iter/adapters/rev.rs +++ b/library/core/src/iter/adapters/rev.rs @@ -1,5 +1,5 @@ use crate::iter::{FusedIterator, TrustedLen}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; /// A double-ended iterator with the direction inverted. @@ -39,7 +39,7 @@ where } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.iter.advance_back_by(n) } @@ -84,7 +84,7 @@ where } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.iter.advance_by(n) } diff --git a/library/core/src/iter/adapters/scan.rs b/library/core/src/iter/adapters/scan.rs index 635bad199ff95..d261a535b183a 100644 --- a/library/core/src/iter/adapters/scan.rs +++ b/library/core/src/iter/adapters/scan.rs @@ -1,6 +1,6 @@ use crate::fmt; use crate::iter::{adapters::SourceIter, InPlaceIterable}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; /// An iterator to maintain state while iterating another iterator. @@ -94,6 +94,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Scan { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/skip.rs b/library/core/src/iter/adapters/skip.rs index f5188dd458df9..20a3b616f81e5 100644 --- a/library/core/src/iter/adapters/skip.rs +++ b/library/core/src/iter/adapters/skip.rs @@ -5,7 +5,7 @@ use crate::iter::{ adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, }; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; /// An iterator that skips over `n` elements of `iter`. @@ -134,7 +134,7 @@ where #[inline] #[rustc_inherit_overflow_checks] - fn advance_by(&mut self, mut n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, mut n: usize) -> Result<(), NonZero> { let skip_inner = self.n; let skip_and_advance = skip_inner.saturating_add(n); @@ -154,7 +154,7 @@ where } } - NonZeroUsize::new(n).map_or(Ok(()), Err) + NonZero::::new(n).map_or(Ok(()), Err) } #[doc(hidden)] @@ -234,11 +234,11 @@ where impl_fold_via_try_fold! { rfold -> try_rfold } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { let min = crate::cmp::min(self.len(), n); let rem = self.iter.advance_back_by(min); assert!(rem.is_ok(), "ExactSizeIterator contract violation"); - NonZeroUsize::new(n - min).map_or(Ok(()), Err) + NonZero::::new(n - min).map_or(Ok(()), Err) } } @@ -264,8 +264,8 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Skip { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } #[doc(hidden)] diff --git a/library/core/src/iter/adapters/skip_while.rs b/library/core/src/iter/adapters/skip_while.rs index 3a661973e5fe0..8001e6e64713a 100644 --- a/library/core/src/iter/adapters/skip_while.rs +++ b/library/core/src/iter/adapters/skip_while.rs @@ -1,6 +1,6 @@ use crate::fmt; use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedFused}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::Try; /// An iterator that rejects elements while `predicate` returns `true`. @@ -124,6 +124,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for SkipWhile { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/take.rs b/library/core/src/iter/adapters/take.rs index 80e06066d28f9..e668e66253607 100644 --- a/library/core/src/iter/adapters/take.rs +++ b/library/core/src/iter/adapters/take.rs @@ -3,7 +3,7 @@ use crate::iter::{ adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedFused, TrustedLen, TrustedRandomAccess, }; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; /// An iterator that only iterates over the first `n` iterations of `iter`. @@ -117,7 +117,7 @@ where #[inline] #[rustc_inherit_overflow_checks] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let min = self.n.min(n); let rem = match self.iter.advance_by(min) { Ok(()) => 0, @@ -125,7 +125,7 @@ where }; let advanced = min - rem; self.n -= advanced; - NonZeroUsize::new(n - advanced).map_or(Ok(()), Err) + NonZero::::new(n - advanced).map_or(Ok(()), Err) } } @@ -145,8 +145,8 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Take { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } #[stable(feature = "double_ended_take_iterator", since = "1.38.0")] @@ -219,7 +219,7 @@ where #[inline] #[rustc_inherit_overflow_checks] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { // The amount by which the inner iterator needs to be shortened for it to be // at most as long as the take() amount. let trim_inner = self.iter.len().saturating_sub(self.n); @@ -235,7 +235,7 @@ where let advanced_by_inner = advance_by - remainder; let advanced_by = advanced_by_inner - trim_inner; self.n -= advanced_by; - NonZeroUsize::new(n - advanced_by).map_or(Ok(()), Err) + NonZero::::new(n - advanced_by).map_or(Ok(()), Err) } } diff --git a/library/core/src/iter/adapters/take_while.rs b/library/core/src/iter/adapters/take_while.rs index e55d55a6d2377..d3f09ab356ad8 100644 --- a/library/core/src/iter/adapters/take_while.rs +++ b/library/core/src/iter/adapters/take_while.rs @@ -1,6 +1,6 @@ use crate::fmt; use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedFused}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; /// An iterator that only accepts elements while `predicate` returns `true`. @@ -125,6 +125,6 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for TakeWhile { - const EXPAND_BY: Option = I::EXPAND_BY; - const MERGE_BY: Option = I::MERGE_BY; + const EXPAND_BY: Option> = I::EXPAND_BY; + const MERGE_BY: Option> = I::MERGE_BY; } diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs index b33400fab476f..2e885f06b5272 100644 --- a/library/core/src/iter/adapters/zip.rs +++ b/library/core/src/iter/adapters/zip.rs @@ -2,7 +2,7 @@ use crate::cmp; use crate::fmt::{self, Debug}; use crate::iter::{FusedIterator, TrustedFused}; use crate::iter::{InPlaceIterable, SourceIter, TrustedLen, UncheckedIterator}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; /// An iterator that iterates two other iterators simultaneously. /// @@ -489,8 +489,8 @@ where // Since SourceIter forwards the left hand side we do the same here #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Zip { - const EXPAND_BY: Option = A::EXPAND_BY; - const MERGE_BY: Option = A::MERGE_BY; + const EXPAND_BY: Option> = A::EXPAND_BY; + const MERGE_BY: Option> = A::MERGE_BY; } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/core/src/iter/range.rs b/library/core/src/iter/range.rs index 0e03d0c2d4e4f..7a4748dcc0d8c 100644 --- a/library/core/src/iter/range.rs +++ b/library/core/src/iter/range.rs @@ -2,7 +2,7 @@ use crate::ascii::Char as AsciiChar; use crate::convert::TryFrom; use crate::mem; use crate::net::{Ipv4Addr, Ipv6Addr}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{self, Try}; use super::{ @@ -629,12 +629,12 @@ trait RangeIteratorImpl { // Iterator fn spec_next(&mut self) -> Option; fn spec_nth(&mut self, n: usize) -> Option; - fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize>; + fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZero>; // DoubleEndedIterator fn spec_next_back(&mut self) -> Option; fn spec_nth_back(&mut self, n: usize) -> Option; - fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize>; + fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZero>; } impl RangeIteratorImpl for ops::Range { @@ -666,7 +666,7 @@ impl RangeIteratorImpl for ops::Range { } #[inline] - default fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + default fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZero> { let available = if self.start <= self.end { Step::steps_between(&self.start, &self.end).unwrap_or(usize::MAX) } else { @@ -678,7 +678,7 @@ impl RangeIteratorImpl for ops::Range { self.start = Step::forward_checked(self.start.clone(), taken).expect("`Step` invariants not upheld"); - NonZeroUsize::new(n - taken).map_or(Ok(()), Err) + NonZero::::new(n - taken).map_or(Ok(()), Err) } #[inline] @@ -707,7 +707,7 @@ impl RangeIteratorImpl for ops::Range { } #[inline] - default fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + default fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { let available = if self.start <= self.end { Step::steps_between(&self.start, &self.end).unwrap_or(usize::MAX) } else { @@ -719,7 +719,7 @@ impl RangeIteratorImpl for ops::Range { self.end = Step::backward_checked(self.end.clone(), taken).expect("`Step` invariants not upheld"); - NonZeroUsize::new(n - taken).map_or(Ok(()), Err) + NonZero::::new(n - taken).map_or(Ok(()), Err) } } @@ -751,7 +751,7 @@ impl RangeIteratorImpl for ops::Range { } #[inline] - fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZero> { let available = if self.start <= self.end { Step::steps_between(&self.start, &self.end).unwrap_or(usize::MAX) } else { @@ -766,7 +766,7 @@ impl RangeIteratorImpl for ops::Range { // Otherwise 0 is returned which always safe to use. self.start = unsafe { Step::forward_unchecked(self.start, taken) }; - NonZeroUsize::new(n - taken).map_or(Ok(()), Err) + NonZero::::new(n - taken).map_or(Ok(()), Err) } #[inline] @@ -795,7 +795,7 @@ impl RangeIteratorImpl for ops::Range { } #[inline] - fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { let available = if self.start <= self.end { Step::steps_between(&self.start, &self.end).unwrap_or(usize::MAX) } else { @@ -807,7 +807,7 @@ impl RangeIteratorImpl for ops::Range { // SAFETY: same as the spec_advance_by() implementation self.end = unsafe { Step::backward_unchecked(self.end, taken) }; - NonZeroUsize::new(n - taken).map_or(Ok(()), Err) + NonZero::::new(n - taken).map_or(Ok(()), Err) } } @@ -871,7 +871,7 @@ impl Iterator for ops::Range { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { self.spec_advance_by(n) } @@ -949,7 +949,7 @@ impl DoubleEndedIterator for ops::Range { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.spec_advance_back_by(n) } } diff --git a/library/core/src/iter/sources/repeat.rs b/library/core/src/iter/sources/repeat.rs index 67051f6e97bdd..0168b11c7394a 100644 --- a/library/core/src/iter/sources/repeat.rs +++ b/library/core/src/iter/sources/repeat.rs @@ -1,5 +1,5 @@ use crate::iter::{FusedIterator, TrustedLen}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; /// Creates a new iterator that endlessly repeats a single element. /// @@ -81,7 +81,7 @@ impl Iterator for Repeat { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { // Advancing an infinite iterator of a single element is a no-op. let _ = n; Ok(()) @@ -110,7 +110,7 @@ impl DoubleEndedIterator for Repeat { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { // Advancing an infinite iterator of a single element is a no-op. let _ = n; Ok(()) diff --git a/library/core/src/iter/sources/repeat_n.rs b/library/core/src/iter/sources/repeat_n.rs index db2f8b7ac283f..77bb8372a446a 100644 --- a/library/core/src/iter/sources/repeat_n.rs +++ b/library/core/src/iter/sources/repeat_n.rs @@ -1,6 +1,6 @@ use crate::iter::{FusedIterator, TrustedLen}; use crate::mem::ManuallyDrop; -use crate::num::NonZeroUsize; +use crate::num::NonZero; /// Creates a new iterator that repeats a single element a given number of times. /// @@ -136,7 +136,7 @@ impl Iterator for RepeatN { } #[inline] - fn advance_by(&mut self, skip: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, skip: usize) -> Result<(), NonZero> { let len = self.count; if skip >= len { @@ -145,7 +145,7 @@ impl Iterator for RepeatN { if skip > len { // SAFETY: we just checked that the difference is positive - Err(unsafe { NonZeroUsize::new_unchecked(skip - len) }) + Err(unsafe { NonZero::::new_unchecked(skip - len) }) } else { self.count = len - skip; Ok(()) @@ -178,7 +178,7 @@ impl DoubleEndedIterator for RepeatN { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { self.advance_by(n) } diff --git a/library/core/src/iter/traits/double_ended.rs b/library/core/src/iter/traits/double_ended.rs index 4c8af4eba7889..eb830256962e1 100644 --- a/library/core/src/iter/traits/double_ended.rs +++ b/library/core/src/iter/traits/double_ended.rs @@ -1,4 +1,4 @@ -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; /// An iterator able to yield elements from both ends. @@ -119,8 +119,8 @@ pub trait DoubleEndedIterator: Iterator { /// /// ``` /// #![feature(iter_advance_by)] - /// /// use std::num::NonZeroUsize; + /// /// let a = [3, 4, 5, 6]; /// let mut iter = a.iter(); /// @@ -134,11 +134,11 @@ pub trait DoubleEndedIterator: Iterator { /// [`Err(k)`]: Err #[inline] #[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { for i in 0..n { if self.next_back().is_none() { // SAFETY: `i` is always less than `n`. - return Err(unsafe { NonZeroUsize::new_unchecked(n - i) }); + return Err(unsafe { NonZero::::new_unchecked(n - i) }); } } Ok(()) @@ -373,7 +373,7 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I { fn next_back(&mut self) -> Option { (**self).next_back() } - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { (**self).advance_back_by(n) } fn nth_back(&mut self, n: usize) -> Option { diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 83f8fd25b50bd..ecb58a6619fca 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1,6 +1,6 @@ use crate::array; use crate::cmp::{self, Ordering}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try}; use super::super::try_process; @@ -328,8 +328,8 @@ pub trait Iterator { /// /// ``` /// #![feature(iter_advance_by)] - /// /// use std::num::NonZeroUsize; + /// /// let a = [1, 2, 3, 4]; /// let mut iter = a.iter(); /// @@ -341,11 +341,11 @@ pub trait Iterator { #[inline] #[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")] #[rustc_do_not_const_check] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { for i in 0..n { if self.next().is_none() { // SAFETY: `i` is always less than `n`. - return Err(unsafe { NonZeroUsize::new_unchecked(n - i) }); + return Err(unsafe { NonZero::::new_unchecked(n - i) }); } } Ok(()) @@ -4178,7 +4178,7 @@ impl Iterator for &mut I { fn size_hint(&self) -> (usize, Option) { (**self).size_hint() } - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { (**self).advance_by(n) } fn nth(&mut self, n: usize) -> Option { diff --git a/library/core/src/iter/traits/marker.rs b/library/core/src/iter/traits/marker.rs index e7c1f195aacc6..8bdbca120d7f9 100644 --- a/library/core/src/iter/traits/marker.rs +++ b/library/core/src/iter/traits/marker.rs @@ -1,5 +1,5 @@ use crate::iter::Step; -use crate::num::NonZeroUsize; +use crate::num::NonZero; /// Same as FusedIterator /// @@ -91,12 +91,12 @@ pub unsafe trait InPlaceIterable { /// E.g. [[u8; 4]; 4].iter().flatten().flatten() would have a `EXPAND_BY` of 16. /// This is an upper bound, i.e. the transformations will produce at most this many items per /// input. It's meant for layout calculations. - const EXPAND_BY: Option; + const EXPAND_BY: Option>; /// The product of many-to-one item reductions that happen throughout the iterator pipeline. /// E.g. [u8].iter().array_chunks::<4>().array_chunks::<4>() would have a `MERGE_BY` of 16. /// This is a lower bound, i.e. the transformations will consume at least this many items per /// output. - const MERGE_BY: Option; + const MERGE_BY: Option>; } /// A type that upholds all invariants of [`Step`]. diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 1124719fc8df1..9d961725aab39 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -302,10 +302,10 @@ macro_rules! nonzero_integer { /// #![feature(non_zero_count_ones)] /// # fn main() { test().unwrap(); } /// # fn test() -> Option<()> { - #[doc = concat!("# use std::num::{self, ", stringify!($Ty), "};")] - /// - /// let one = num::NonZeroU32::new(1)?; - /// let three = num::NonZeroU32::new(3)?; + /// # use std::num::*; + /// # + /// let one = NonZeroU32::new(1)?; + /// let three = NonZeroU32::new(3)?; #[doc = concat!("let a = ", stringify!($Ty), "::new(0b100_0000)?;")] #[doc = concat!("let b = ", stringify!($Ty), "::new(0b100_0011)?;")] /// @@ -326,7 +326,7 @@ macro_rules! nonzero_integer { // SAFETY: // `self` is non-zero, which means it has at least one bit set, which means // that the result of `count_ones` is non-zero. - unsafe { NonZeroU32::new_unchecked(self.get().count_ones()) } + unsafe { NonZero::::new_unchecked(self.get().count_ones()) } } nonzero_integer_signedness_dependent_methods! { diff --git a/library/core/src/ops/index_range.rs b/library/core/src/ops/index_range.rs index 743799c4b3edf..2ba0bd158f74b 100644 --- a/library/core/src/ops/index_range.rs +++ b/library/core/src/ops/index_range.rs @@ -1,6 +1,6 @@ use crate::intrinsics::{unchecked_add, unchecked_sub}; use crate::iter::{FusedIterator, TrustedLen}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; /// Like a `Range`, but with a safety invariant that `start <= end`. /// @@ -130,9 +130,9 @@ impl Iterator for IndexRange { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let taken = self.take_prefix(n); - NonZeroUsize::new(n - taken.len()).map_or(Ok(()), Err) + NonZero::::new(n - taken.len()).map_or(Ok(()), Err) } } @@ -148,9 +148,9 @@ impl DoubleEndedIterator for IndexRange { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { let taken = self.take_suffix(n); - NonZeroUsize::new(n - taken.len()).map_or(Ok(()), Err) + NonZero::::new(n - taken.len()).map_or(Ok(()), Err) } } diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs index ce176e6fc18f3..ad22ee5a0271b 100644 --- a/library/core/src/ptr/alignment.rs +++ b/library/core/src/ptr/alignment.rs @@ -1,5 +1,5 @@ use crate::convert::{TryFrom, TryInto}; -use crate::num::NonZeroUsize; +use crate::num::{NonZero, NonZeroUsize}; use crate::{cmp, fmt, hash, mem, num}; /// A type storing a `usize` which is a power of two, and thus @@ -100,7 +100,7 @@ impl Alignment { #[inline] pub const fn as_nonzero(self) -> NonZeroUsize { // SAFETY: All the discriminants are non-zero. - unsafe { NonZeroUsize::new_unchecked(self.as_usize()) } + unsafe { NonZero::::new_unchecked(self.as_usize()) } } /// Returns the base-2 logarithm of the alignment. diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 427a9f3f49456..abdf18d5a0696 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -6,7 +6,7 @@ use crate::intrinsics::assert_unsafe_precondition; use crate::marker::Unsize; use crate::mem::SizedTypeProperties; use crate::mem::{self, MaybeUninit}; -use crate::num::NonZeroUsize; +use crate::num::{NonZero, NonZeroUsize}; use crate::ops::{CoerceUnsized, DispatchFromDyn}; use crate::ptr; use crate::ptr::Unique; @@ -292,7 +292,7 @@ impl NonNull { pub fn addr(self) -> NonZeroUsize { // SAFETY: The pointer is guaranteed by the type to be non-null, // meaning that the address will be non-zero. - unsafe { NonZeroUsize::new_unchecked(self.pointer.addr()) } + unsafe { NonZero::::new_unchecked(self.pointer.addr()) } } /// Creates a new pointer with the given address. diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 2d4c7e78aea17..ce30d04075947 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -11,7 +11,7 @@ use crate::iter::{ }; use crate::marker::PhantomData; use crate::mem::{self, SizedTypeProperties}; -use crate::num::{NonZero, NonZeroUsize}; +use crate::num::NonZero; use crate::ptr::{self, invalid, invalid_mut, NonNull}; use super::{from_raw_parts, from_raw_parts_mut}; diff --git a/library/core/src/slice/iter/macros.rs b/library/core/src/slice/iter/macros.rs index fc6af45fb9077..53218391dcffd 100644 --- a/library/core/src/slice/iter/macros.rs +++ b/library/core/src/slice/iter/macros.rs @@ -196,11 +196,11 @@ macro_rules! iterator { } #[inline] - fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { let advance = cmp::min(len!(self), n); // SAFETY: By construction, `advance` does not exceed `self.len()`. unsafe { self.post_inc_start(advance) }; - NonZeroUsize::new(n - advance).map_or(Ok(()), Err) + NonZero::::new(n - advance).map_or(Ok(()), Err) } #[inline] @@ -421,11 +421,11 @@ macro_rules! iterator { } #[inline] - fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> { + fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero> { let advance = cmp::min(len!(self), n); // SAFETY: By construction, `advance` does not exceed `self.len()`. unsafe { self.pre_dec_end(advance) }; - NonZeroUsize::new(n - advance).map_or(Ok(()), Err) + NonZero::::new(n - advance).map_or(Ok(()), Err) } } diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 2d93ef6fbeb34..479a16c608e61 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -11,7 +11,7 @@ use crate::fmt; use crate::hint; use crate::intrinsics::exact_div; use crate::mem::{self, SizedTypeProperties}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{Bound, OneSidedRange, Range, RangeBounds}; use crate::panic::debug_assert_nounwind; use crate::ptr; @@ -1086,7 +1086,7 @@ impl [T] { #[inline] #[track_caller] pub fn windows(&self, size: usize) -> Windows<'_, T> { - let size = NonZeroUsize::new(size).expect("window size must be non-zero"); + let size = NonZero::::new(size).expect("window size must be non-zero"); Windows::new(self, size) } diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 4d6239e11a399..d2180fa83fb1e 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -8,7 +8,7 @@ use crate::iter::{TrustedRandomAccess, TrustedRandomAccessNoCoerce}; use crate::ops::Try; use crate::option; use crate::slice::{self, Split as SliceSplit}; -use core::num::NonZeroUsize; +use core::num::{NonZero, NonZeroUsize}; use super::from_utf8_unchecked; use super::pattern::Pattern; @@ -96,7 +96,7 @@ impl<'a> Iterator for Chars<'a> { unsafe { self.iter.advance_by(slurp).unwrap_unchecked() }; } - NonZeroUsize::new(remainder).map_or(Ok(()), Err) + NonZero::::new(remainder).map_or(Ok(()), Err) } #[inline] diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index ed52de3cbec16..579c140c198c4 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -1,4 +1,4 @@ -use core::num::NonZeroUsize; +use core::num::NonZero; use core::sync::atomic::{AtomicUsize, Ordering}; use core::{array, assert_eq}; @@ -548,7 +548,7 @@ fn array_intoiter_advance_by() { assert_eq!(counter.get(), 13); let r = it.advance_by(123456); - assert_eq!(r, Err(NonZeroUsize::new(123456 - 87).unwrap())); + assert_eq!(r, Err(NonZero::::new(123456 - 87).unwrap())); assert_eq!(it.len(), 0); assert_eq!(counter.get(), 100); @@ -558,7 +558,7 @@ fn array_intoiter_advance_by() { assert_eq!(counter.get(), 100); let r = it.advance_by(10); - assert_eq!(r, Err(NonZeroUsize::new(10).unwrap())); + assert_eq!(r, Err(NonZero::::new(10).unwrap())); assert_eq!(it.len(), 0); assert_eq!(counter.get(), 100); } @@ -601,7 +601,7 @@ fn array_intoiter_advance_back_by() { assert_eq!(counter.get(), 13); let r = it.advance_back_by(123456); - assert_eq!(r, Err(NonZeroUsize::new(123456 - 87).unwrap())); + assert_eq!(r, Err(NonZero::::new(123456 - 87).unwrap())); assert_eq!(it.len(), 0); assert_eq!(counter.get(), 100); @@ -611,7 +611,7 @@ fn array_intoiter_advance_back_by() { assert_eq!(counter.get(), 100); let r = it.advance_back_by(10); - assert_eq!(r, Err(NonZeroUsize::new(10).unwrap())); + assert_eq!(r, Err(NonZero::::new(10).unwrap())); assert_eq!(it.len(), 0); assert_eq!(counter.get(), 100); } diff --git a/library/core/tests/iter/adapters/chain.rs b/library/core/tests/iter/adapters/chain.rs index ad78a85a88dcb..9e098d5bee442 100644 --- a/library/core/tests/iter/adapters/chain.rs +++ b/library/core/tests/iter/adapters/chain.rs @@ -1,6 +1,6 @@ use super::*; use core::iter::*; -use core::num::NonZeroUsize; +use core::num::NonZero; #[test] fn test_iterator_chain() { @@ -34,7 +34,10 @@ fn test_iterator_chain_advance_by() { let mut iter = Unfuse::new(xs).chain(Unfuse::new(ys)); assert_eq!(iter.advance_by(i), Ok(())); assert_eq!(iter.next(), Some(&xs[i])); - assert_eq!(iter.advance_by(100), Err(NonZeroUsize::new(100 - (len - i - 1)).unwrap())); + assert_eq!( + iter.advance_by(100), + Err(NonZero::::new(100 - (len - i - 1)).unwrap()) + ); assert_eq!(iter.advance_by(0), Ok(())); } @@ -44,7 +47,7 @@ fn test_iterator_chain_advance_by() { assert_eq!(iter.next(), Some(&ys[i])); assert_eq!( iter.advance_by(100), - Err(NonZeroUsize::new(100 - (ys.len() - i - 1)).unwrap()) + Err(NonZero::::new(100 - (ys.len() - i - 1)).unwrap()) ); assert_eq!(iter.advance_by(0), Ok(())); } @@ -55,7 +58,7 @@ fn test_iterator_chain_advance_by() { assert_eq!(iter.advance_by(0), Ok(())); let mut iter = xs.iter().chain(ys); - assert_eq!(iter.advance_by(len + 1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!(iter.advance_by(len + 1), Err(NonZero::::new(1).unwrap())); assert_eq!(iter.advance_by(0), Ok(())); } @@ -76,7 +79,7 @@ fn test_iterator_chain_advance_back_by() { assert_eq!(iter.next_back(), Some(&ys[ys.len() - i - 1])); assert_eq!( iter.advance_back_by(100), - Err(NonZeroUsize::new(100 - (len - i - 1)).unwrap()) + Err(NonZero::::new(100 - (len - i - 1)).unwrap()) ); assert_eq!(iter.advance_back_by(0), Ok(())); } @@ -87,7 +90,7 @@ fn test_iterator_chain_advance_back_by() { assert_eq!(iter.next_back(), Some(&xs[xs.len() - i - 1])); assert_eq!( iter.advance_back_by(100), - Err(NonZeroUsize::new(100 - (xs.len() - i - 1)).unwrap()) + Err(NonZero::::new(100 - (xs.len() - i - 1)).unwrap()) ); assert_eq!(iter.advance_back_by(0), Ok(())); } @@ -98,7 +101,7 @@ fn test_iterator_chain_advance_back_by() { assert_eq!(iter.advance_back_by(0), Ok(())); let mut iter = xs.iter().chain(ys); - assert_eq!(iter.advance_back_by(len + 1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!(iter.advance_back_by(len + 1), Err(NonZero::::new(1).unwrap())); assert_eq!(iter.advance_back_by(0), Ok(())); } diff --git a/library/core/tests/iter/adapters/enumerate.rs b/library/core/tests/iter/adapters/enumerate.rs index ff57973a62a4b..5aa7532c10cb2 100644 --- a/library/core/tests/iter/adapters/enumerate.rs +++ b/library/core/tests/iter/adapters/enumerate.rs @@ -1,5 +1,5 @@ use core::iter::*; -use core::num::NonZeroUsize; +use core::num::NonZero; #[test] fn test_iterator_enumerate() { @@ -66,7 +66,7 @@ fn test_iterator_enumerate_advance_by() { assert_eq!(it.next(), Some((2, &2))); assert_eq!(it.advance_by(2), Ok(())); assert_eq!(it.next(), Some((5, &5))); - assert_eq!(it.advance_by(1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!(it.advance_by(1), Err(NonZero::::new(1).unwrap())); assert_eq!(it.next(), None); } diff --git a/library/core/tests/iter/adapters/flatten.rs b/library/core/tests/iter/adapters/flatten.rs index f429d90cd7ddd..fb6383b3289bb 100644 --- a/library/core/tests/iter/adapters/flatten.rs +++ b/library/core/tests/iter/adapters/flatten.rs @@ -1,7 +1,7 @@ use super::*; use core::assert_eq; use core::iter::*; -use core::num::NonZeroUsize; +use core::num::NonZero; #[test] fn test_iterator_flatten() { @@ -72,8 +72,8 @@ fn test_flatten_advance_by() { assert_eq!(it.advance_back_by(9), Ok(())); assert_eq!(it.next_back(), Some(25)); - assert_eq!(it.advance_by(usize::MAX), Err(NonZeroUsize::new(usize::MAX - 9).unwrap())); - assert_eq!(it.advance_back_by(usize::MAX), Err(NonZeroUsize::new(usize::MAX).unwrap())); + assert_eq!(it.advance_by(usize::MAX), Err(NonZero::::new(usize::MAX - 9).unwrap())); + assert_eq!(it.advance_back_by(usize::MAX), Err(NonZero::::new(usize::MAX).unwrap())); assert_eq!(it.advance_by(0), Ok(())); assert_eq!(it.advance_back_by(0), Ok(())); assert_eq!(it.size_hint(), (0, Some(0))); diff --git a/library/core/tests/iter/adapters/skip.rs b/library/core/tests/iter/adapters/skip.rs index e3e88a84fadf6..45726d158b608 100644 --- a/library/core/tests/iter/adapters/skip.rs +++ b/library/core/tests/iter/adapters/skip.rs @@ -1,5 +1,5 @@ use core::iter::*; -use core::num::NonZeroUsize; +use core::num::NonZero; use super::Unfuse; @@ -75,14 +75,14 @@ fn test_iterator_skip_nth() { #[test] fn test_skip_advance_by() { assert_eq!((0..0).skip(10).advance_by(0), Ok(())); - assert_eq!((0..0).skip(10).advance_by(1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!((0..0).skip(10).advance_by(1), Err(NonZero::::new(1).unwrap())); assert_eq!( (0u128..(usize::MAX as u128) + 1).skip(usize::MAX - 10).advance_by(usize::MAX - 5), - Err(NonZeroUsize::new(usize::MAX - 16).unwrap()) + Err(NonZero::::new(usize::MAX - 16).unwrap()) ); assert_eq!((0u128..u128::MAX).skip(usize::MAX - 10).advance_by(20), Ok(())); - assert_eq!((0..2).skip(1).advance_back_by(10), Err(NonZeroUsize::new(9).unwrap())); + assert_eq!((0..2).skip(1).advance_back_by(10), Err(NonZero::::new(9).unwrap())); assert_eq!((0..0).skip(1).advance_back_by(0), Ok(())); } diff --git a/library/core/tests/iter/adapters/take.rs b/library/core/tests/iter/adapters/take.rs index ff6e362b065c6..6aa1b92954691 100644 --- a/library/core/tests/iter/adapters/take.rs +++ b/library/core/tests/iter/adapters/take.rs @@ -1,5 +1,5 @@ use core::iter::*; -use core::num::NonZeroUsize; +use core::num::NonZero; #[test] fn test_iterator_take() { @@ -79,23 +79,23 @@ fn test_take_advance_by() { let mut take = (0..10).take(3); assert_eq!(take.advance_by(2), Ok(())); assert_eq!(take.next(), Some(2)); - assert_eq!(take.advance_by(1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!(take.advance_by(1), Err(NonZero::::new(1).unwrap())); assert_eq!((0..0).take(10).advance_by(0), Ok(())); - assert_eq!((0..0).take(10).advance_by(1), Err(NonZeroUsize::new(1).unwrap())); - assert_eq!((0..10).take(4).advance_by(5), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!((0..0).take(10).advance_by(1), Err(NonZero::::new(1).unwrap())); + assert_eq!((0..10).take(4).advance_by(5), Err(NonZero::::new(1).unwrap())); let mut take = (0..10).take(3); assert_eq!(take.advance_back_by(2), Ok(())); assert_eq!(take.next(), Some(0)); - assert_eq!(take.advance_back_by(1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!(take.advance_back_by(1), Err(NonZero::::new(1).unwrap())); - assert_eq!((0..2).take(1).advance_back_by(10), Err(NonZeroUsize::new(9).unwrap())); - assert_eq!((0..0).take(1).advance_back_by(1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!((0..2).take(1).advance_back_by(10), Err(NonZero::::new(9).unwrap())); + assert_eq!((0..0).take(1).advance_back_by(1), Err(NonZero::::new(1).unwrap())); assert_eq!((0..0).take(1).advance_back_by(0), Ok(())); assert_eq!( (0..usize::MAX).take(100).advance_back_by(usize::MAX), - Err(NonZeroUsize::new(usize::MAX - 100).unwrap()) + Err(NonZero::::new(usize::MAX - 100).unwrap()) ); } diff --git a/library/core/tests/iter/range.rs b/library/core/tests/iter/range.rs index a6b9f1cb7c889..f840218382dae 100644 --- a/library/core/tests/iter/range.rs +++ b/library/core/tests/iter/range.rs @@ -1,6 +1,6 @@ use super::*; use core::ascii::Char as AsciiChar; -use core::num::NonZeroUsize; +use core::num::NonZero; #[test] fn test_range() { @@ -314,7 +314,7 @@ fn test_range_advance_by() { assert_eq!((r.start, r.end), (1, usize::MAX - 1)); - assert_eq!(Err(NonZeroUsize::new(2).unwrap()), r.advance_by(usize::MAX)); + assert_eq!(Err(NonZero::::new(2).unwrap()), r.advance_by(usize::MAX)); assert_eq!(Ok(()), r.advance_by(0)); assert_eq!(Ok(()), r.advance_back_by(0)); diff --git a/library/core/tests/iter/traits/iterator.rs b/library/core/tests/iter/traits/iterator.rs index 4c2d843eaa0dc..507f15c608832 100644 --- a/library/core/tests/iter/traits/iterator.rs +++ b/library/core/tests/iter/traits/iterator.rs @@ -1,4 +1,4 @@ -use core::num::NonZeroUsize; +use core::num::NonZero; /// A wrapper struct that implements `Eq` and `Ord` based on the wrapped /// integer modulo 3. Used to test that `Iterator::max` and `Iterator::min` @@ -152,11 +152,14 @@ fn test_iterator_advance_by() { let mut iter = v.iter(); assert_eq!(iter.advance_by(i), Ok(())); assert_eq!(iter.next().unwrap(), &v[i]); - assert_eq!(iter.advance_by(100), Err(NonZeroUsize::new(100 - (v.len() - 1 - i)).unwrap())); + assert_eq!( + iter.advance_by(100), + Err(NonZero::::new(100 - (v.len() - 1 - i)).unwrap()) + ); } assert_eq!(v.iter().advance_by(v.len()), Ok(())); - assert_eq!(v.iter().advance_by(100), Err(NonZeroUsize::new(100 - v.len()).unwrap())); + assert_eq!(v.iter().advance_by(100), Err(NonZero::::new(100 - v.len()).unwrap())); } #[test] @@ -169,12 +172,12 @@ fn test_iterator_advance_back_by() { assert_eq!(iter.next_back().unwrap(), &v[v.len() - 1 - i]); assert_eq!( iter.advance_back_by(100), - Err(NonZeroUsize::new(100 - (v.len() - 1 - i)).unwrap()) + Err(NonZero::::new(100 - (v.len() - 1 - i)).unwrap()) ); } assert_eq!(v.iter().advance_back_by(v.len()), Ok(())); - assert_eq!(v.iter().advance_back_by(100), Err(NonZeroUsize::new(100 - v.len()).unwrap())); + assert_eq!(v.iter().advance_back_by(100), Err(NonZero::::new(100 - v.len()).unwrap())); } #[test] @@ -187,12 +190,15 @@ fn test_iterator_rev_advance_back_by() { assert_eq!(iter.next_back().unwrap(), &v[i]); assert_eq!( iter.advance_back_by(100), - Err(NonZeroUsize::new(100 - (v.len() - 1 - i)).unwrap()) + Err(NonZero::::new(100 - (v.len() - 1 - i)).unwrap()) ); } assert_eq!(v.iter().rev().advance_back_by(v.len()), Ok(())); - assert_eq!(v.iter().rev().advance_back_by(100), Err(NonZeroUsize::new(100 - v.len()).unwrap())); + assert_eq!( + v.iter().rev().advance_back_by(100), + Err(NonZero::::new(100 - v.len()).unwrap()) + ); } #[test] @@ -460,11 +466,14 @@ fn test_iterator_rev_advance_by() { let mut iter = v.iter().rev(); assert_eq!(iter.advance_by(i), Ok(())); assert_eq!(iter.next().unwrap(), &v[v.len() - 1 - i]); - assert_eq!(iter.advance_by(100), Err(NonZeroUsize::new(100 - (v.len() - 1 - i)).unwrap())); + assert_eq!( + iter.advance_by(100), + Err(NonZero::::new(100 - (v.len() - 1 - i)).unwrap()) + ); } assert_eq!(v.iter().rev().advance_by(v.len()), Ok(())); - assert_eq!(v.iter().rev().advance_by(100), Err(NonZeroUsize::new(100 - v.len()).unwrap())); + assert_eq!(v.iter().rev().advance_by(100), Err(NonZero::::new(100 - v.len()).unwrap())); } #[test] diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 9a3b477c2d159..b8967442f598f 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -38,6 +38,7 @@ #![feature(float_minimum_maximum)] #![feature(future_join)] #![feature(generic_assert_internals)] +#![feature(generic_nonzero)] #![feature(array_try_from_fn)] #![feature(hasher_prefixfree_extras)] #![feature(hashmap_internals)] diff --git a/library/core/tests/nonzero.rs b/library/core/tests/nonzero.rs index 8873d26880ced..cfbf760c728fc 100644 --- a/library/core/tests/nonzero.rs +++ b/library/core/tests/nonzero.rs @@ -1,30 +1,27 @@ -use core::num::{ - IntErrorKind, NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, - NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, -}; +use core::num::{IntErrorKind, NonZero}; use core::option::Option::None; use std::mem::size_of; #[test] fn test_create_nonzero_instance() { - let _a = unsafe { NonZeroU32::new_unchecked(21) }; + let _a = unsafe { NonZero::::new_unchecked(21) }; } #[test] fn test_size_nonzero_in_option() { - assert_eq!(size_of::(), size_of::>()); - assert_eq!(size_of::(), size_of::>()); + assert_eq!(size_of::>(), size_of::>>()); + assert_eq!(size_of::>(), size_of::>>()); } #[test] fn test_match_on_nonzero_option() { - let a = Some(unsafe { NonZeroU32::new_unchecked(42) }); + let a = Some(unsafe { NonZero::::new_unchecked(42) }); match a { Some(val) => assert_eq!(val.get(), 42), None => panic!("unexpected None while matching on Some(NonZeroU32(_))"), } - match unsafe { Some(NonZeroU32::new_unchecked(43)) } { + match unsafe { Some(NonZero::::new_unchecked(43)) } { Some(val) => assert_eq!(val.get(), 43), None => panic!("unexpected None while matching on Some(NonZeroU32(_))"), } @@ -89,13 +86,14 @@ fn test_match_option_string() { } mod atom { - use core::num::NonZeroU32; + use core::num::NonZero; #[derive(PartialEq, Eq)] pub struct Atom { - index: NonZeroU32, // private + index: NonZero, // private } - pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } }; + + pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZero::::new_unchecked(7) } }; } macro_rules! atom { @@ -115,62 +113,65 @@ fn test_match_nonzero_const_pattern() { #[test] fn test_from_nonzero() { - let nz = NonZeroU32::new(1).unwrap(); + let nz = NonZero::::new(1).unwrap(); let num: u32 = nz.into(); assert_eq!(num, 1u32); } #[test] fn test_from_signed_nonzero() { - let nz = NonZeroI32::new(1).unwrap(); + let nz = NonZero::::new(1).unwrap(); let num: i32 = nz.into(); assert_eq!(num, 1i32); } #[test] fn test_from_str() { - assert_eq!("123".parse::(), Ok(NonZeroU8::new(123).unwrap())); - assert_eq!("0".parse::().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero)); + assert_eq!("123".parse::>(), Ok(NonZero::::new(123).unwrap())); + assert_eq!( + "0".parse::>().err().map(|e| e.kind().clone()), + Some(IntErrorKind::Zero) + ); assert_eq!( - "-1".parse::().err().map(|e| e.kind().clone()), + "-1".parse::>().err().map(|e| e.kind().clone()), Some(IntErrorKind::InvalidDigit) ); assert_eq!( - "-129".parse::().err().map(|e| e.kind().clone()), + "-129".parse::>().err().map(|e| e.kind().clone()), Some(IntErrorKind::NegOverflow) ); assert_eq!( - "257".parse::().err().map(|e| e.kind().clone()), + "257".parse::>().err().map(|e| e.kind().clone()), Some(IntErrorKind::PosOverflow) ); } #[test] fn test_nonzero_bitor() { - let nz_alt = NonZeroU8::new(0b1010_1010).unwrap(); - let nz_low = NonZeroU8::new(0b0000_1111).unwrap(); + let nz_alt = NonZero::::new(0b1010_1010).unwrap(); + let nz_low = NonZero::::new(0b0000_1111).unwrap(); - let both_nz: NonZeroU8 = nz_alt | nz_low; + let both_nz: NonZero = nz_alt | nz_low; assert_eq!(both_nz.get(), 0b1010_1111); - let rhs_int: NonZeroU8 = nz_low | 0b1100_0000u8; + let rhs_int: NonZero = nz_low | 0b1100_0000u8; assert_eq!(rhs_int.get(), 0b1100_1111); - let rhs_zero: NonZeroU8 = nz_alt | 0u8; + let rhs_zero: NonZero = nz_alt | 0u8; assert_eq!(rhs_zero.get(), 0b1010_1010); - let lhs_int: NonZeroU8 = 0b0110_0110u8 | nz_alt; + let lhs_int: NonZero = 0b0110_0110u8 | nz_alt; assert_eq!(lhs_int.get(), 0b1110_1110); - let lhs_zero: NonZeroU8 = 0u8 | nz_low; + let lhs_zero: NonZero = 0u8 | nz_low; assert_eq!(lhs_zero.get(), 0b0000_1111); } #[test] fn test_nonzero_bitor_assign() { - let mut target = NonZeroU8::new(0b1010_1010).unwrap(); + let mut target = NonZero::::new(0b1010_1010).unwrap(); - target |= NonZeroU8::new(0b0000_1111).unwrap(); + target |= NonZero::::new(0b0000_1111).unwrap(); assert_eq!(target.get(), 0b1010_1111); target |= 0b0001_0000; @@ -182,20 +183,20 @@ fn test_nonzero_bitor_assign() { #[test] fn test_nonzero_from_int_on_success() { - assert_eq!(NonZeroU8::try_from(5), Ok(NonZeroU8::new(5).unwrap())); - assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5).unwrap())); + assert_eq!(NonZero::::try_from(5), Ok(NonZero::::new(5).unwrap())); + assert_eq!(NonZero::::try_from(5), Ok(NonZero::::new(5).unwrap())); - assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5).unwrap())); - assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5).unwrap())); + assert_eq!(NonZero::::try_from(-5), Ok(NonZero::::new(-5).unwrap())); + assert_eq!(NonZero::::try_from(-5), Ok(NonZero::::new(-5).unwrap())); } #[test] fn test_nonzero_from_int_on_err() { - assert!(NonZeroU8::try_from(0).is_err()); - assert!(NonZeroU32::try_from(0).is_err()); + assert!(NonZero::::try_from(0).is_err()); + assert!(NonZero::::try_from(0).is_err()); - assert!(NonZeroI8::try_from(0).is_err()); - assert!(NonZeroI32::try_from(0).is_err()); + assert!(NonZero::::try_from(0).is_err()); + assert!(NonZero::::try_from(0).is_err()); } #[test] @@ -203,126 +204,126 @@ fn nonzero_const() { // test that the methods of `NonZeroX>` are usable in a const context // Note: only tests NonZero8 - const NONZERO_U8: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) }; + const NONZERO_U8: NonZero = unsafe { NonZero::::new_unchecked(5) }; const GET: u8 = NONZERO_U8.get(); assert_eq!(GET, 5); - const ZERO: Option = NonZeroU8::new(0); + const ZERO: Option> = NonZero::::new(0); assert!(ZERO.is_none()); - const ONE: Option = NonZeroU8::new(1); + const ONE: Option> = NonZero::::new(1); assert!(ONE.is_some()); /* FIXME(#110395) const FROM_NONZERO_U8: u8 = u8::from(NONZERO_U8); assert_eq!(FROM_NONZERO_U8, 5); - const NONZERO_CONVERT: NonZeroU32 = NonZeroU32::from(NONZERO_U8); + const NONZERO_CONVERT: NonZero = NonZero::::from(NONZERO_U8); assert_eq!(NONZERO_CONVERT.get(), 5); */ } #[test] fn nonzero_leading_zeros() { - assert_eq!(NonZeroU8::new(1).unwrap().leading_zeros(), 7); - assert_eq!(NonZeroI8::new(1).unwrap().leading_zeros(), 7); - assert_eq!(NonZeroU16::new(1).unwrap().leading_zeros(), 15); - assert_eq!(NonZeroI16::new(1).unwrap().leading_zeros(), 15); - assert_eq!(NonZeroU32::new(1).unwrap().leading_zeros(), 31); - assert_eq!(NonZeroI32::new(1).unwrap().leading_zeros(), 31); - assert_eq!(NonZeroU64::new(1).unwrap().leading_zeros(), 63); - assert_eq!(NonZeroI64::new(1).unwrap().leading_zeros(), 63); - assert_eq!(NonZeroU128::new(1).unwrap().leading_zeros(), 127); - assert_eq!(NonZeroI128::new(1).unwrap().leading_zeros(), 127); - assert_eq!(NonZeroUsize::new(1).unwrap().leading_zeros(), usize::BITS - 1); - assert_eq!(NonZeroIsize::new(1).unwrap().leading_zeros(), usize::BITS - 1); - - assert_eq!(NonZeroU8::new(u8::MAX >> 2).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroI8::new((u8::MAX >> 2) as i8).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroU16::new(u16::MAX >> 2).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroI16::new((u16::MAX >> 2) as i16).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroU32::new(u32::MAX >> 2).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroI32::new((u32::MAX >> 2) as i32).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroU64::new(u64::MAX >> 2).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroI64::new((u64::MAX >> 2) as i64).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroU128::new(u128::MAX >> 2).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroI128::new((u128::MAX >> 2) as i128).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroUsize::new(usize::MAX >> 2).unwrap().leading_zeros(), 2); - assert_eq!(NonZeroIsize::new((usize::MAX >> 2) as isize).unwrap().leading_zeros(), 2); - - assert_eq!(NonZeroU8::new(u8::MAX).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroI8::new(-1i8).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroU16::new(u16::MAX).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroI16::new(-1i16).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroU32::new(u32::MAX).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroI32::new(-1i32).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroU64::new(u64::MAX).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroI64::new(-1i64).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroU128::new(u128::MAX).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroI128::new(-1i128).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroUsize::new(usize::MAX).unwrap().leading_zeros(), 0); - assert_eq!(NonZeroIsize::new(-1isize).unwrap().leading_zeros(), 0); - - const LEADING_ZEROS: u32 = NonZeroU16::new(1).unwrap().leading_zeros(); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 7); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 7); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 15); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 15); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 31); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 31); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 63); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 63); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 127); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), 127); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), usize::BITS - 1); + assert_eq!(NonZero::::new(1).unwrap().leading_zeros(), usize::BITS - 1); + + assert_eq!(NonZero::::new(u8::MAX >> 2).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new((u8::MAX >> 2) as i8).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new(u16::MAX >> 2).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new((u16::MAX >> 2) as i16).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new(u32::MAX >> 2).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new((u32::MAX >> 2) as i32).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new(u64::MAX >> 2).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new((u64::MAX >> 2) as i64).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new(u128::MAX >> 2).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new((u128::MAX >> 2) as i128).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new(usize::MAX >> 2).unwrap().leading_zeros(), 2); + assert_eq!(NonZero::::new((usize::MAX >> 2) as isize).unwrap().leading_zeros(), 2); + + assert_eq!(NonZero::::new(u8::MAX).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(-1i8).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(u16::MAX).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(-1i16).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(u32::MAX).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(-1i32).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(u64::MAX).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(-1i64).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(u128::MAX).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(-1i128).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(usize::MAX).unwrap().leading_zeros(), 0); + assert_eq!(NonZero::::new(-1isize).unwrap().leading_zeros(), 0); + + const LEADING_ZEROS: u32 = NonZero::::new(1).unwrap().leading_zeros(); assert_eq!(LEADING_ZEROS, 15); } #[test] fn nonzero_trailing_zeros() { - assert_eq!(NonZeroU8::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroI8::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroU16::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroI16::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroU32::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroI32::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroU64::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroI64::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroU128::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroI128::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroUsize::new(1).unwrap().trailing_zeros(), 0); - assert_eq!(NonZeroIsize::new(1).unwrap().trailing_zeros(), 0); - - assert_eq!(NonZeroU8::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroI8::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroU16::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroI16::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroU32::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroI32::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroU64::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroI64::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroU128::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroI128::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroUsize::new(1 << 2).unwrap().trailing_zeros(), 2); - assert_eq!(NonZeroIsize::new(1 << 2).unwrap().trailing_zeros(), 2); - - assert_eq!(NonZeroU8::new(1 << 7).unwrap().trailing_zeros(), 7); - assert_eq!(NonZeroI8::new(1 << 7).unwrap().trailing_zeros(), 7); - assert_eq!(NonZeroU16::new(1 << 15).unwrap().trailing_zeros(), 15); - assert_eq!(NonZeroI16::new(1 << 15).unwrap().trailing_zeros(), 15); - assert_eq!(NonZeroU32::new(1 << 31).unwrap().trailing_zeros(), 31); - assert_eq!(NonZeroI32::new(1 << 31).unwrap().trailing_zeros(), 31); - assert_eq!(NonZeroU64::new(1 << 63).unwrap().trailing_zeros(), 63); - assert_eq!(NonZeroI64::new(1 << 63).unwrap().trailing_zeros(), 63); - assert_eq!(NonZeroU128::new(1 << 127).unwrap().trailing_zeros(), 127); - assert_eq!(NonZeroI128::new(1 << 127).unwrap().trailing_zeros(), 127); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + assert_eq!(NonZero::::new(1).unwrap().trailing_zeros(), 0); + + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + assert_eq!(NonZero::::new(1 << 2).unwrap().trailing_zeros(), 2); + + assert_eq!(NonZero::::new(1 << 7).unwrap().trailing_zeros(), 7); + assert_eq!(NonZero::::new(1 << 7).unwrap().trailing_zeros(), 7); + assert_eq!(NonZero::::new(1 << 15).unwrap().trailing_zeros(), 15); + assert_eq!(NonZero::::new(1 << 15).unwrap().trailing_zeros(), 15); + assert_eq!(NonZero::::new(1 << 31).unwrap().trailing_zeros(), 31); + assert_eq!(NonZero::::new(1 << 31).unwrap().trailing_zeros(), 31); + assert_eq!(NonZero::::new(1 << 63).unwrap().trailing_zeros(), 63); + assert_eq!(NonZero::::new(1 << 63).unwrap().trailing_zeros(), 63); + assert_eq!(NonZero::::new(1 << 127).unwrap().trailing_zeros(), 127); + assert_eq!(NonZero::::new(1 << 127).unwrap().trailing_zeros(), 127); assert_eq!( - NonZeroUsize::new(1 << (usize::BITS - 1)).unwrap().trailing_zeros(), + NonZero::::new(1 << (usize::BITS - 1)).unwrap().trailing_zeros(), usize::BITS - 1 ); assert_eq!( - NonZeroIsize::new(1 << (usize::BITS - 1)).unwrap().trailing_zeros(), + NonZero::::new(1 << (usize::BITS - 1)).unwrap().trailing_zeros(), usize::BITS - 1 ); - const TRAILING_ZEROS: u32 = NonZeroU16::new(1 << 2).unwrap().trailing_zeros(); + const TRAILING_ZEROS: u32 = NonZero::::new(1 << 2).unwrap().trailing_zeros(); assert_eq!(TRAILING_ZEROS, 2); } #[test] fn test_nonzero_uint_div() { - let nz = NonZeroU32::new(1).unwrap(); + let nz = NonZero::::new(1).unwrap(); let x: u32 = 42u32 / nz; assert_eq!(x, 42u32); @@ -330,7 +331,7 @@ fn test_nonzero_uint_div() { #[test] fn test_nonzero_uint_rem() { - let nz = NonZeroU32::new(10).unwrap(); + let nz = NonZero::::new(10).unwrap(); let x: u32 = 42u32 % nz; assert_eq!(x, 2u32); @@ -338,18 +339,18 @@ fn test_nonzero_uint_rem() { #[test] fn test_signed_nonzero_neg() { - assert_eq!((-NonZeroI8::new(1).unwrap()).get(), -1); - assert_eq!((-NonZeroI8::new(-1).unwrap()).get(), 1); + assert_eq!((-NonZero::::new(1).unwrap()).get(), -1); + assert_eq!((-NonZero::::new(-1).unwrap()).get(), 1); - assert_eq!((-NonZeroI16::new(1).unwrap()).get(), -1); - assert_eq!((-NonZeroI16::new(-1).unwrap()).get(), 1); + assert_eq!((-NonZero::::new(1).unwrap()).get(), -1); + assert_eq!((-NonZero::::new(-1).unwrap()).get(), 1); - assert_eq!((-NonZeroI32::new(1).unwrap()).get(), -1); - assert_eq!((-NonZeroI32::new(-1).unwrap()).get(), 1); + assert_eq!((-NonZero::::new(1).unwrap()).get(), -1); + assert_eq!((-NonZero::::new(-1).unwrap()).get(), 1); - assert_eq!((-NonZeroI64::new(1).unwrap()).get(), -1); - assert_eq!((-NonZeroI64::new(-1).unwrap()).get(), 1); + assert_eq!((-NonZero::::new(1).unwrap()).get(), -1); + assert_eq!((-NonZero::::new(-1).unwrap()).get(), 1); - assert_eq!((-NonZeroI128::new(1).unwrap()).get(), -1); - assert_eq!((-NonZeroI128::new(-1).unwrap()).get(), 1); + assert_eq!((-NonZero::::new(1).unwrap()).get(), -1); + assert_eq!((-NonZero::::new(-1).unwrap()).get(), 1); } diff --git a/library/core/tests/ptr.rs b/library/core/tests/ptr.rs index b68f2a50b3211..8a0cf90d799c0 100644 --- a/library/core/tests/ptr.rs +++ b/library/core/tests/ptr.rs @@ -1,6 +1,6 @@ use core::cell::RefCell; use core::mem::{self, MaybeUninit}; -use core::num::NonZeroUsize; +use core::num::NonZero; use core::ptr; use core::ptr::*; use std::fmt::{Debug, Display}; @@ -1051,7 +1051,7 @@ fn nonnull_tagged_pointer_with_provenance() { pub fn pointer(self) -> NonNull { // SAFETY: The `addr` guaranteed to have bits set in the Self::ADDRESS_MASK, so the result will be non-null. self.0.map_addr(|addr| unsafe { - NonZeroUsize::new_unchecked(addr.get() & Self::ADDRESS_MASK) + NonZero::::new_unchecked(addr.get() & Self::ADDRESS_MASK) }) } @@ -1073,7 +1073,7 @@ fn nonnull_tagged_pointer_with_provenance() { // ADDRESS_MASK) will always be non-zero. This a property of the type and its // construction. self.0 = self.0.map_addr(|addr| unsafe { - NonZeroUsize::new_unchecked((addr.get() & Self::ADDRESS_MASK) | data) + NonZero::::new_unchecked((addr.get() & Self::ADDRESS_MASK) | data) }) } } diff --git a/library/core/tests/result.rs b/library/core/tests/result.rs index 50926da3ce799..758203408a86c 100644 --- a/library/core/tests/result.rs +++ b/library/core/tests/result.rs @@ -406,13 +406,14 @@ fn result_opt_conversions() { #[test] fn result_try_trait_v2_branch() { - use core::num::NonZeroU32; + use core::num::NonZero; use core::ops::{ControlFlow::*, Try}; + assert_eq!(Ok::(4).branch(), Continue(4)); assert_eq!(Err::(4).branch(), Break(Err(4))); - let one = NonZeroU32::new(1).unwrap(); - assert_eq!(Ok::<(), NonZeroU32>(()).branch(), Continue(())); - assert_eq!(Err::<(), NonZeroU32>(one).branch(), Break(Err(one))); - assert_eq!(Ok::(one).branch(), Continue(one)); - assert_eq!(Err::(()).branch(), Break(Err(()))); + let one = NonZero::::new(1).unwrap(); + assert_eq!(Ok::<(), NonZero>(()).branch(), Continue(())); + assert_eq!(Err::<(), NonZero>(one).branch(), Break(Err(one))); + assert_eq!(Ok::, ()>(one).branch(), Continue(one)); + assert_eq!(Err::, ()>(()).branch(), Break(Err(()))); } diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs index bcf7b5e59775a..bb2c17a479e22 100644 --- a/library/core/tests/slice.rs +++ b/library/core/tests/slice.rs @@ -1,7 +1,7 @@ use core::cell::Cell; use core::cmp::Ordering; use core::mem::MaybeUninit; -use core::num::NonZeroUsize; +use core::num::NonZero; use core::slice; #[test] @@ -147,7 +147,7 @@ fn test_iterator_advance_by() { } let mut iter = v.iter(); - assert_eq!(iter.advance_by(v.len() + 1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!(iter.advance_by(v.len() + 1), Err(NonZero::::new(1).unwrap())); assert_eq!(iter.as_slice(), &[]); let mut iter = v.iter(); @@ -169,7 +169,7 @@ fn test_iterator_advance_back_by() { } let mut iter = v.iter(); - assert_eq!(iter.advance_back_by(v.len() + 1), Err(NonZeroUsize::new(1).unwrap())); + assert_eq!(iter.advance_back_by(v.len() + 1), Err(NonZero::::new(1).unwrap())); assert_eq!(iter.as_slice(), &[]); let mut iter = v.iter(); diff --git a/library/proc_macro/src/bridge/handle.rs b/library/proc_macro/src/bridge/handle.rs index 00954107b7769..7caf80ee4aeda 100644 --- a/library/proc_macro/src/bridge/handle.rs +++ b/library/proc_macro/src/bridge/handle.rs @@ -2,13 +2,13 @@ use std::collections::BTreeMap; use std::hash::Hash; -use std::num::NonZeroU32; +use std::num::NonZero; use std::ops::{Index, IndexMut}; use std::sync::atomic::{AtomicUsize, Ordering}; use super::fxhash::FxHashMap; -pub(super) type Handle = NonZeroU32; +pub(super) type Handle = NonZero; /// A store that associates values of type `T` with numeric handles. A value can /// be looked up using its handle. @@ -20,7 +20,7 @@ pub(super) struct OwnedStore { impl OwnedStore { pub(super) fn new(counter: &'static AtomicUsize) -> Self { // Ensure the handle counter isn't 0, which would panic later, - // when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`. + // when `NonZero::new` (aka `Handle::new`) is called in `alloc`. assert_ne!(counter.load(Ordering::SeqCst), 0); OwnedStore { counter, data: BTreeMap::new() } diff --git a/library/proc_macro/src/bridge/symbol.rs b/library/proc_macro/src/bridge/symbol.rs index 930c111455df0..ae3c0fe018fb1 100644 --- a/library/proc_macro/src/bridge/symbol.rs +++ b/library/proc_macro/src/bridge/symbol.rs @@ -10,14 +10,14 @@ //! proc_macro, this module should probably be removed or simplified. use std::cell::RefCell; -use std::num::NonZeroU32; +use std::num::NonZero; use std::str; use super::*; /// Handle for a symbol string stored within the Interner. #[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub struct Symbol(NonZeroU32); +pub struct Symbol(NonZero); impl !Send for Symbol {} impl !Sync for Symbol {} @@ -137,7 +137,7 @@ thread_local! { names: fxhash::FxHashMap::default(), strings: Vec::new(), // Start with a base of 1 to make sure that `NonZeroU32` works. - sym_base: NonZeroU32::new(1).unwrap(), + sym_base: NonZero::::new(1).unwrap(), }); } @@ -152,7 +152,7 @@ struct Interner { // The offset to apply to symbol names stored in the interner. This is used // to ensure that symbol names are not re-used after the interner is // cleared. - sym_base: NonZeroU32, + sym_base: NonZero, } impl Interner { diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 87e89a464bc1a..dfc9f2667a199 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -26,6 +26,7 @@ #![feature(staged_api)] #![feature(allow_internal_unstable)] #![feature(decl_macro)] +#![feature(generic_nonzero)] #![feature(maybe_uninit_write_slice)] #![feature(negative_impls)] #![feature(new_uninit)] diff --git a/library/std/src/sys/pal/hermit/thread.rs b/library/std/src/sys/pal/hermit/thread.rs index 3384906a15e3b..789de7f41ff9a 100644 --- a/library/std/src/sys/pal/hermit/thread.rs +++ b/library/std/src/sys/pal/hermit/thread.rs @@ -5,7 +5,7 @@ use super::thread_local_dtor::run_dtors; use crate::ffi::CStr; use crate::io; use crate::mem; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ptr; use crate::time::Duration; @@ -97,8 +97,8 @@ impl Thread { } } -pub fn available_parallelism() -> io::Result { - unsafe { Ok(NonZeroUsize::new_unchecked(abi::get_processor_count())) } +pub fn available_parallelism() -> io::Result> { + unsafe { Ok(NonZero::::new_unchecked(abi::get_processor_count())) } } pub mod guard { diff --git a/library/std/src/sys/pal/sgx/abi/tls/mod.rs b/library/std/src/sys/pal/sgx/abi/tls/mod.rs index 09c4ab3d3e901..a2ba6c01f3d64 100644 --- a/library/std/src/sys/pal/sgx/abi/tls/mod.rs +++ b/library/std/src/sys/pal/sgx/abi/tls/mod.rs @@ -3,7 +3,7 @@ mod sync_bitset; use self::sync_bitset::*; use crate::cell::Cell; use crate::mem; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ptr; use crate::sync::atomic::{AtomicUsize, Ordering}; @@ -38,7 +38,7 @@ impl Key { } fn from_index(index: usize) -> Self { - Key(NonZeroUsize::new(index + 1).unwrap()) + Key(NonZero::::new(index + 1).unwrap()) } pub fn as_usize(self) -> usize { @@ -46,7 +46,7 @@ impl Key { } pub fn from_usize(index: usize) -> Self { - Key(NonZeroUsize::new(index).unwrap()) + Key(NonZero::::new(index).unwrap()) } } diff --git a/library/std/src/sys/pal/sgx/abi/usercalls/raw.rs b/library/std/src/sys/pal/sgx/abi/usercalls/raw.rs index 10c1456d4fd05..19a972a4284a9 100644 --- a/library/std/src/sys/pal/sgx/abi/usercalls/raw.rs +++ b/library/std/src/sys/pal/sgx/abi/usercalls/raw.rs @@ -194,7 +194,7 @@ macro_rules! enclave_usercalls_internal_define_usercalls { #[inline(always)] pub unsafe fn $f($n1: $t1, $n2: $t2, $n3: $t3, $n4: $t4) -> $r { ReturnValue::from_registers(stringify!($f), unsafe { do_usercall( - rtunwrap!(Some, NonZeroU64::new(Usercalls::$f as Register)), + rtunwrap!(Some, NonZero::::new(Usercalls::$f as Register)), RegisterArgument::into_register($n1), RegisterArgument::into_register($n2), RegisterArgument::into_register($n3), @@ -210,7 +210,7 @@ macro_rules! enclave_usercalls_internal_define_usercalls { #[inline(always)] pub unsafe fn $f($n1: $t1, $n2: $t2, $n3: $t3) -> $r { ReturnValue::from_registers(stringify!($f), unsafe { do_usercall( - rtunwrap!(Some, NonZeroU64::new(Usercalls::$f as Register)), + rtunwrap!(Some, NonZero::::new(Usercalls::$f as Register)), RegisterArgument::into_register($n1), RegisterArgument::into_register($n2), RegisterArgument::into_register($n3), @@ -226,7 +226,7 @@ macro_rules! enclave_usercalls_internal_define_usercalls { #[inline(always)] pub unsafe fn $f($n1: $t1, $n2: $t2) -> $r { ReturnValue::from_registers(stringify!($f), unsafe { do_usercall( - rtunwrap!(Some, NonZeroU64::new(Usercalls::$f as Register)), + rtunwrap!(Some, NonZero::::new(Usercalls::$f as Register)), RegisterArgument::into_register($n1), RegisterArgument::into_register($n2), 0,0, @@ -241,7 +241,7 @@ macro_rules! enclave_usercalls_internal_define_usercalls { #[inline(always)] pub unsafe fn $f($n1: $t1) -> $r { ReturnValue::from_registers(stringify!($f), unsafe { do_usercall( - rtunwrap!(Some, NonZeroU64::new(Usercalls::$f as Register)), + rtunwrap!(Some, NonZero::::new(Usercalls::$f as Register)), RegisterArgument::into_register($n1), 0,0,0, return_type_is_abort!($r) @@ -255,7 +255,7 @@ macro_rules! enclave_usercalls_internal_define_usercalls { #[inline(always)] pub unsafe fn $f() -> $r { ReturnValue::from_registers(stringify!($f), unsafe { do_usercall( - rtunwrap!(Some, NonZeroU64::new(Usercalls::$f as Register)), + rtunwrap!(Some, NonZero::::new(Usercalls::$f as Register)), 0,0,0,0, return_type_is_abort!($r) ) }) diff --git a/library/std/src/sys/pal/sgx/rwlock.rs b/library/std/src/sys/pal/sgx/rwlock.rs index d89de18ca5ff8..6584b0e17712f 100644 --- a/library/std/src/sys/pal/sgx/rwlock.rs +++ b/library/std/src/sys/pal/sgx/rwlock.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod tests; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::sys_common::lazy_box::{LazyBox, LazyInit}; use super::waitqueue::{ @@ -54,7 +54,7 @@ impl RwLock { } else { // No waiting writers, acquire the read lock *rguard.lock_var_mut() = - NonZeroUsize::new(rguard.lock_var().map_or(0, |n| n.get()) + 1); + NonZero::::new(rguard.lock_var().map_or(0, |n| n.get()) + 1); } } @@ -69,7 +69,7 @@ impl RwLock { } else { // No waiting writers, acquire the read lock *rguard.lock_var_mut() = - NonZeroUsize::new(rguard.lock_var().map_or(0, |n| n.get()) + 1); + NonZero::::new(rguard.lock_var().map_or(0, |n| n.get()) + 1); true } } @@ -111,7 +111,7 @@ impl RwLock { mut rguard: SpinMutexGuard<'_, WaitVariable>>, wguard: SpinMutexGuard<'_, WaitVariable>, ) { - *rguard.lock_var_mut() = NonZeroUsize::new(rguard.lock_var().unwrap().get() - 1); + *rguard.lock_var_mut() = NonZero::::new(rguard.lock_var().unwrap().get() - 1); if rguard.lock_var().is_some() { // There are other active readers } else { diff --git a/library/std/src/sys/pal/sgx/thread.rs b/library/std/src/sys/pal/sgx/thread.rs index 7ac9d1d64b420..c797fde7fbdca 100644 --- a/library/std/src/sys/pal/sgx/thread.rs +++ b/library/std/src/sys/pal/sgx/thread.rs @@ -2,7 +2,7 @@ use super::unsupported; use crate::ffi::CStr; use crate::io; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::time::Duration; use super::abi::usercalls; @@ -142,7 +142,7 @@ impl Thread { } } -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { unsupported() } diff --git a/library/std/src/sys/pal/sgx/waitqueue/mod.rs b/library/std/src/sys/pal/sgx/waitqueue/mod.rs index 25eca61d67b66..d9b2aff988bce 100644 --- a/library/std/src/sys/pal/sgx/waitqueue/mod.rs +++ b/library/std/src/sys/pal/sgx/waitqueue/mod.rs @@ -16,7 +16,7 @@ mod tests; mod spin_mutex; mod unsafe_list; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ops::{Deref, DerefMut}; use crate::panic::{self, AssertUnwindSafe}; use crate::time::Duration; @@ -252,7 +252,7 @@ impl WaitQueue { entry_guard.wake = true; } - if let Some(count) = NonZeroUsize::new(count) { + if let Some(count) = NonZero::::new(count) { Ok(WaitGuard { mutex_guard: Some(guard), notified_tcs: NotifiedTcs::All { count } }) } else { Err(guard) diff --git a/library/std/src/sys/pal/teeos/thread.rs b/library/std/src/sys/pal/teeos/thread.rs index 155f333f90616..77f9040ead540 100644 --- a/library/std/src/sys/pal/teeos/thread.rs +++ b/library/std/src/sys/pal/teeos/thread.rs @@ -4,7 +4,7 @@ use crate::cmp; use crate::ffi::CStr; use crate::io; use crate::mem; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ptr; use crate::sys::os; use crate::time::Duration; @@ -140,7 +140,7 @@ impl Drop for Thread { // Note: Both `sched_getaffinity` and `sysconf` are available but not functional on // teeos, so this function always returns an Error! -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { Err(io::Error::new( io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform", diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs index 7e4a01a5ecd33..dd3c370667a1e 100644 --- a/library/std/src/sys/pal/unix/thread.rs +++ b/library/std/src/sys/pal/unix/thread.rs @@ -2,7 +2,7 @@ use crate::cmp; use crate::ffi::CStr; use crate::io; use crate::mem; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ptr; use crate::sys::{os, stack_overflow}; use crate::time::Duration; @@ -306,7 +306,7 @@ fn truncate_cstr(cstr: &CStr) -> [libc::c_char; MAX_W result } -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { cfg_if::cfg_if! { if #[cfg(any( target_os = "android", @@ -338,7 +338,7 @@ pub fn available_parallelism() -> io::Result { // some old MIPS kernels were buggy and zero-initialized the mask if // none was explicitly set. // In that case we use the sysconf fallback. - if let Some(count) = NonZeroUsize::new(count) { + if let Some(count) = NonZero::::new(count) { return Ok(count) } } @@ -351,7 +351,7 @@ pub fn available_parallelism() -> io::Result { let count = cpus as usize; // Cover the unusual situation where we were able to get the quota but not the affinity mask let count = count.min(quota); - Ok(unsafe { NonZeroUsize::new_unchecked(count) }) + Ok(unsafe { NonZero::::new_unchecked(count) }) } } } else if #[cfg(any( @@ -375,7 +375,7 @@ pub fn available_parallelism() -> io::Result { ) == 0 { let count = libc::CPU_COUNT(&set) as usize; if count > 0 { - return Ok(NonZeroUsize::new_unchecked(count)); + return Ok(NonZero::::new_unchecked(count)); } } } @@ -397,7 +397,7 @@ pub fn available_parallelism() -> io::Result { } } libc::_cpuset_destroy(set); - if let Some(count) = NonZeroUsize::new(count) { + if let Some(count) = NonZero::::new(count) { return Ok(count); } } @@ -433,7 +433,7 @@ pub fn available_parallelism() -> io::Result { } } - Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) }) + Ok(unsafe { NonZero::::new_unchecked(cpus as usize) }) } else if #[cfg(target_os = "nto")] { unsafe { use libc::_syspage_ptr; @@ -441,7 +441,7 @@ pub fn available_parallelism() -> io::Result { Err(io::const_io_error!(io::ErrorKind::NotFound, "No syspage available")) } else { let cpus = (*_syspage_ptr).num_cpu; - NonZeroUsize::new(cpus as usize) + NonZero::::new(cpus as usize) .ok_or(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")) } } @@ -456,7 +456,7 @@ pub fn available_parallelism() -> io::Result { return Err(io::const_io_error!(io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform")); } - Ok(NonZeroUsize::new_unchecked(sinfo.cpu_count as usize)) + Ok(NonZero::::new_unchecked(sinfo.cpu_count as usize)) } } else { // FIXME: implement on vxWorks, Redox, l4re diff --git a/library/std/src/sys/pal/unsupported/thread.rs b/library/std/src/sys/pal/unsupported/thread.rs index a8db251de2017..cd1ae7f7d11cd 100644 --- a/library/std/src/sys/pal/unsupported/thread.rs +++ b/library/std/src/sys/pal/unsupported/thread.rs @@ -1,7 +1,7 @@ use super::unsupported; use crate::ffi::CStr; use crate::io; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::time::Duration; pub struct Thread(!); @@ -31,7 +31,7 @@ impl Thread { } } -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { unsupported() } diff --git a/library/std/src/sys/pal/wasi/thread.rs b/library/std/src/sys/pal/wasi/thread.rs index a0eefa8811a39..77d8b4378e7d5 100644 --- a/library/std/src/sys/pal/wasi/thread.rs +++ b/library/std/src/sys/pal/wasi/thread.rs @@ -1,7 +1,7 @@ use crate::ffi::CStr; use crate::io; use crate::mem; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::sys::unsupported; use crate::time::Duration; @@ -186,7 +186,7 @@ impl Thread { } } -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { unsupported() } diff --git a/library/std/src/sys/pal/wasm/atomics/thread.rs b/library/std/src/sys/pal/wasm/atomics/thread.rs index 714b704922794..49f936f14498c 100644 --- a/library/std/src/sys/pal/wasm/atomics/thread.rs +++ b/library/std/src/sys/pal/wasm/atomics/thread.rs @@ -1,6 +1,6 @@ use crate::ffi::CStr; use crate::io; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::sys::unsupported; use crate::time::Duration; @@ -40,7 +40,7 @@ impl Thread { pub fn join(self) {} } -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { unsupported() } diff --git a/library/std/src/sys/pal/windows/args.rs b/library/std/src/sys/pal/windows/args.rs index fbbdbc2126595..c3a4d4706996d 100644 --- a/library/std/src/sys/pal/windows/args.rs +++ b/library/std/src/sys/pal/windows/args.rs @@ -10,7 +10,7 @@ use super::os::current_exe; use crate::ffi::OsString; use crate::fmt; use crate::io; -use crate::num::NonZeroU16; +use crate::num::NonZero; use crate::os::windows::prelude::*; use crate::path::{Path, PathBuf}; use crate::sys::path::get_long_path; @@ -21,12 +21,12 @@ use crate::vec; use crate::iter; -/// This is the const equivalent to `NonZeroU16::new(n).unwrap()` +/// This is the const equivalent to `NonZero::::new(n).unwrap()` /// /// FIXME: This can be removed once `Option::unwrap` is stably const. /// See the `const_option` feature (#67441). -const fn non_zero_u16(n: u16) -> NonZeroU16 { - match NonZeroU16::new(n) { +const fn non_zero_u16(n: u16) -> NonZero { + match NonZero::::new(n) { Some(n) => n, None => panic!("called `unwrap` on a `None` value"), } @@ -69,10 +69,10 @@ fn parse_lp_cmd_line<'a, F: Fn() -> OsString>( lp_cmd_line: Option>, exe_name: F, ) -> Vec { - const BACKSLASH: NonZeroU16 = non_zero_u16(b'\\' as u16); - const QUOTE: NonZeroU16 = non_zero_u16(b'"' as u16); - const TAB: NonZeroU16 = non_zero_u16(b'\t' as u16); - const SPACE: NonZeroU16 = non_zero_u16(b' ' as u16); + const BACKSLASH: NonZero = non_zero_u16(b'\\' as u16); + const QUOTE: NonZero = non_zero_u16(b'"' as u16); + const TAB: NonZero = non_zero_u16(b'\t' as u16); + const SPACE: NonZero = non_zero_u16(b' ' as u16); let mut ret_val = Vec::new(); // If the cmd line pointer is null or it points to an empty string then diff --git a/library/std/src/sys/pal/windows/thread.rs b/library/std/src/sys/pal/windows/thread.rs index 1fe744935193c..4f189944fb2b8 100644 --- a/library/std/src/sys/pal/windows/thread.rs +++ b/library/std/src/sys/pal/windows/thread.rs @@ -1,6 +1,6 @@ use crate::ffi::CStr; use crate::io; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::os::windows::io::AsRawHandle; use crate::os::windows::io::HandleOrNull; use crate::ptr; @@ -110,7 +110,7 @@ impl Thread { } } -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { let res = unsafe { let mut sysinfo: c::SYSTEM_INFO = crate::mem::zeroed(); c::GetSystemInfo(&mut sysinfo); @@ -121,7 +121,7 @@ pub fn available_parallelism() -> io::Result { io::ErrorKind::NotFound, "The number of hardware threads is not known for the target platform", )), - cpus => Ok(unsafe { NonZeroUsize::new_unchecked(cpus) }), + cpus => Ok(unsafe { NonZero::::new_unchecked(cpus) }), } } diff --git a/library/std/src/sys/pal/xous/thread.rs b/library/std/src/sys/pal/xous/thread.rs index 0f452e07a5c58..2cc1585650184 100644 --- a/library/std/src/sys/pal/xous/thread.rs +++ b/library/std/src/sys/pal/xous/thread.rs @@ -1,6 +1,6 @@ use crate::ffi::CStr; use crate::io; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::os::xous::ffi::{ blocking_scalar, create_thread, do_yield, join_thread, map_memory, update_memory_flags, MemoryFlags, Syscall, ThreadId, @@ -132,9 +132,9 @@ impl Thread { } } -pub fn available_parallelism() -> io::Result { +pub fn available_parallelism() -> io::Result> { // We're unicore right now. - Ok(unsafe { NonZeroUsize::new_unchecked(1) }) + Ok(unsafe { NonZero::::new_unchecked(1) }) } pub mod guard { diff --git a/library/std/src/sys_common/wstr.rs b/library/std/src/sys_common/wstr.rs index b230fd1a829f7..601ef3dd1505c 100644 --- a/library/std/src/sys_common/wstr.rs +++ b/library/std/src/sys_common/wstr.rs @@ -2,7 +2,7 @@ #![allow(dead_code)] use crate::marker::PhantomData; -use crate::num::NonZeroU16; +use crate::num::NonZero; use crate::ptr::NonNull; /// A safe iterator over a LPWSTR @@ -23,15 +23,15 @@ impl WStrUnits<'_> { Some(Self { lpwstr: NonNull::new(lpwstr as _)?, lifetime: PhantomData }) } - pub fn peek(&self) -> Option { + pub fn peek(&self) -> Option> { // SAFETY: It's always safe to read the current item because we don't // ever move out of the array's bounds. - unsafe { NonZeroU16::new(*self.lpwstr.as_ptr()) } + unsafe { NonZero::::new(*self.lpwstr.as_ptr()) } } /// Advance the iterator while `predicate` returns true. /// Returns the number of items it advanced by. - pub fn advance_while bool>(&mut self, mut predicate: P) -> usize { + pub fn advance_while) -> bool>(&mut self, mut predicate: P) -> usize { let mut counter = 0; while let Some(w) = self.peek() { if !predicate(w) { @@ -46,8 +46,9 @@ impl WStrUnits<'_> { impl Iterator for WStrUnits<'_> { // This can never return zero as that marks the end of the string. - type Item = NonZeroU16; - fn next(&mut self) -> Option { + type Item = NonZero; + + fn next(&mut self) -> Option { // SAFETY: If NULL is reached we immediately return. // Therefore it's safe to advance the pointer after that. unsafe { diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 8498937809e71..d2d42b4f9cfe9 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -165,8 +165,7 @@ use crate::fmt; use crate::io; use crate::marker::PhantomData; use crate::mem::{self, forget}; -use crate::num::NonZeroU64; -use crate::num::NonZeroUsize; +use crate::num::{NonZero, NonZeroU64, NonZeroUsize}; use crate::panic; use crate::panicking; use crate::pin::Pin; @@ -1166,7 +1165,7 @@ pub fn park_timeout(dur: Duration) { /// [`id`]: Thread::id #[stable(feature = "thread_id", since = "1.19.0")] #[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)] -pub struct ThreadId(NonZeroU64); +pub struct ThreadId(NonZero); impl ThreadId { // Generate a new unique thread ID. @@ -1189,7 +1188,7 @@ impl ThreadId { }; match COUNTER.compare_exchange_weak(last, id, Relaxed, Relaxed) { - Ok(_) => return ThreadId(NonZeroU64::new(id).unwrap()), + Ok(_) => return ThreadId(NonZero::::new(id).unwrap()), Err(id) => last = id, } } @@ -1208,7 +1207,7 @@ impl ThreadId { *counter = id; drop(counter); - ThreadId(NonZeroU64::new(id).unwrap()) + ThreadId(NonZero::::new(id).unwrap()) } } } diff --git a/src/tools/clippy/clippy_lints/src/transmute/mod.rs b/src/tools/clippy/clippy_lints/src/transmute/mod.rs index 06de7a1103166..e25ab48513a88 100644 --- a/src/tools/clippy/clippy_lints/src/transmute/mod.rs +++ b/src/tools/clippy/clippy_lints/src/transmute/mod.rs @@ -271,7 +271,7 @@ declare_clippy_lint! { /// Use instead: /// ```no_run /// # use core::num::NonZeroU32; - /// let _non_zero = unsafe { NonZeroU32::new_unchecked(123) }; + /// let _non_zero = unsafe { NonZero::::new_unchecked(123) }; /// ``` #[clippy::version = "1.69.0"] pub TRANSMUTE_INT_TO_NON_ZERO, diff --git a/src/tools/clippy/tests/ui/arithmetic_side_effects.rs b/src/tools/clippy/tests/ui/arithmetic_side_effects.rs index b454c29aef4db..8f028c202946b 100644 --- a/src/tools/clippy/tests/ui/arithmetic_side_effects.rs +++ b/src/tools/clippy/tests/ui/arithmetic_side_effects.rs @@ -502,7 +502,7 @@ pub fn issue_11392() { unsigned % nonzero_unsigned } - let (unsigned, nonzero_unsigned) = (0, NonZeroUsize::new(1).unwrap()); + let (unsigned, nonzero_unsigned) = (0, NonZero::::new(1).unwrap()); example_div(unsigned, nonzero_unsigned); example_rem(unsigned, nonzero_unsigned); } diff --git a/src/tools/clippy/tests/ui/eager_transmute.rs b/src/tools/clippy/tests/ui/eager_transmute.rs index a82bd578f76cc..a0aed2c414284 100644 --- a/src/tools/clippy/tests/ui/eager_transmute.rs +++ b/src/tools/clippy/tests/ui/eager_transmute.rs @@ -90,16 +90,16 @@ fn niche_tests(v1: u8, v2: NonZeroU8, v3: NonZeroNonMaxU8) { let _: Option = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) }); // NonZeroU8 -> u8, don't lint, target type has no niche and therefore a higher validity range - let _: Option = (v2 > NonZeroU8::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); + let _: Option = (v2 > NonZero::::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); // NonZeroU8 -> NonMaxU8, do lint, different niche - let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); + let _: Option = (v2 < NonZero::::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); // NonZeroNonMaxU8 -> NonMaxU8, don't lint, target type has more validity let _: Option = (v3 < 255).then_some(unsafe { std::mem::transmute(v2) }); // NonZeroU8 -> NonZeroNonMaxU8, do lint, target type has less validity - let _: Option = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); + let _: Option = (v2 < NonZero::::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) }); } fn main() {} diff --git a/src/tools/clippy/tests/ui/transmute_int_to_non_zero.rs b/src/tools/clippy/tests/ui/transmute_int_to_non_zero.rs index 803c4945c755a..279bb50d3f700 100644 --- a/src/tools/clippy/tests/ui/transmute_int_to_non_zero.rs +++ b/src/tools/clippy/tests/ui/transmute_int_to_non_zero.rs @@ -38,15 +38,15 @@ fn main() { let _: NonZeroI128 = unsafe { std::mem::transmute(int_i128) }; //~^ ERROR: transmute from a `i128` to a `NonZeroI128` - let _: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(int_u8) }; - let _: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(int_u16) }; - let _: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(int_u32) }; - let _: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(int_u64) }; - let _: NonZeroU128 = unsafe { NonZeroU128::new_unchecked(int_u128) }; + let _: NonZeroU8 = unsafe { NonZero::::new_unchecked(int_u8) }; + let _: NonZeroU16 = unsafe { NonZero::::new_unchecked(int_u16) }; + let _: NonZeroU32 = unsafe { NonZero::::new_unchecked(int_u32) }; + let _: NonZeroU64 = unsafe { NonZero::::new_unchecked(int_u64) }; + let _: NonZeroU128 = unsafe { NonZero::::new_unchecked(int_u128) }; - let _: NonZeroI8 = unsafe { NonZeroI8::new_unchecked(int_i8) }; - let _: NonZeroI16 = unsafe { NonZeroI16::new_unchecked(int_i16) }; - let _: NonZeroI32 = unsafe { NonZeroI32::new_unchecked(int_i32) }; - let _: NonZeroI64 = unsafe { NonZeroI64::new_unchecked(int_i64) }; - let _: NonZeroI128 = unsafe { NonZeroI128::new_unchecked(int_i128) }; + let _: NonZeroI8 = unsafe { NonZero::::new_unchecked(int_i8) }; + let _: NonZeroI16 = unsafe { NonZero::::new_unchecked(int_i16) }; + let _: NonZeroI32 = unsafe { NonZero::::new_unchecked(int_i32) }; + let _: NonZeroI64 = unsafe { NonZero::::new_unchecked(int_i64) }; + let _: NonZeroI128 = unsafe { NonZero::::new_unchecked(int_i128) }; } diff --git a/src/tools/miri/src/borrow_tracker/mod.rs b/src/tools/miri/src/borrow_tracker/mod.rs index 74ff6ed4e0abe..d3650ba7b7243 100644 --- a/src/tools/miri/src/borrow_tracker/mod.rs +++ b/src/tools/miri/src/borrow_tracker/mod.rs @@ -21,7 +21,7 @@ pub struct BorTag(NonZeroU64); impl BorTag { pub fn new(i: u64) -> Option { - NonZeroU64::new(i).map(BorTag) + NonZero::::new(i).map(BorTag) } pub fn get(&self) -> u64 { @@ -184,7 +184,7 @@ impl GlobalStateInner { borrow_tracker_method, next_ptr_tag: BorTag::one(), base_ptr_tags: FxHashMap::default(), - next_call_id: NonZeroU64::new(1).unwrap(), + next_call_id: NonZero::::new(1).unwrap(), protected_tags: FxHashMap::default(), tracked_pointer_tags, tracked_call_ids, @@ -206,7 +206,7 @@ impl GlobalStateInner { if self.tracked_call_ids.contains(&call_id) { machine.emit_diagnostic(NonHaltingDiagnostic::CreatedCallId(call_id)); } - self.next_call_id = NonZeroU64::new(call_id.get() + 1).unwrap(); + self.next_call_id = NonZero::::new(call_id.get() + 1).unwrap(); FrameState { call_id, protected_tags: SmallVec::new() } } diff --git a/src/tools/miri/src/concurrency/sync.rs b/src/tools/miri/src/concurrency/sync.rs index b288b69e0cef9..b8fccb8a92699 100644 --- a/src/tools/miri/src/concurrency/sync.rs +++ b/src/tools/miri/src/concurrency/sync.rs @@ -31,7 +31,7 @@ macro_rules! declare_id { impl SyncId for $name { // Panics if `id == 0`. fn from_u32(id: u32) -> Self { - Self(NonZeroU32::new(id).unwrap()) + Self(NonZero::::new(id).unwrap()) } fn to_u32(&self) -> u32 { self.0.get() @@ -44,7 +44,7 @@ macro_rules! declare_id { // therefore, need to shift by one when converting from an index // into a vector. let shifted_idx = u32::try_from(idx).unwrap().checked_add(1).unwrap(); - $name(NonZeroU32::new(shifted_idx).unwrap()) + $name(NonZero::::new(shifted_idx).unwrap()) } fn index(self) -> usize { // See the comment in `Self::new`. diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs index a002f2aad057c..983b1e9b9d73f 100644 --- a/src/tools/miri/src/shims/foreign_items.rs +++ b/src/tools/miri/src/shims/foreign_items.rs @@ -477,7 +477,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [id, show_unnamed] = this.check_shim(abi, Abi::Rust, link_name, args)?; let id = this.read_scalar(id)?.to_u64()?; let show_unnamed = this.read_scalar(show_unnamed)?.to_bool()?; - if let Some(id) = std::num::NonZeroU64::new(id) { + if let Some(id) = std::num::NonZero::::new(id) { this.print_borrow_state(AllocId(id), show_unnamed)?; } } diff --git a/src/tools/miri/tests/compiletest.rs b/src/tools/miri/tests/compiletest.rs index 8bed321e6551c..bef7a7e2487a4 100644 --- a/src/tools/miri/tests/compiletest.rs +++ b/src/tools/miri/tests/compiletest.rs @@ -94,7 +94,7 @@ fn test_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) -> edition: Some("2021".into()), // keep in sync with `./miri run` threads: std::env::var("MIRI_TEST_THREADS") .ok() - .map(|threads| NonZeroUsize::new(threads.parse().unwrap()).unwrap()), + .map(|threads| NonZero::::new(threads.parse().unwrap()).unwrap()), ..Config::rustc(path) }; diff --git a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs index 2cf4e04477778..158ce2d60be5a 100644 --- a/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs +++ b/src/tools/miri/tests/fail/function_pointers/abi_mismatch_repr_C.rs @@ -11,6 +11,6 @@ fn callee(_s: S2) {} fn main() { let fnptr: fn(S2) = callee; let fnptr: fn(S1) = unsafe { std::mem::transmute(fnptr) }; - fnptr(S1(NonZeroI32::new(1).unwrap())); + fnptr(S1(NonZero::::new(1).unwrap())); //~^ ERROR: calling a function with argument of type S2 passing data of type S1 } diff --git a/src/tools/miri/tests/pass/available-parallelism-miri-num-cpus.rs b/src/tools/miri/tests/pass/available-parallelism-miri-num-cpus.rs index 137fa51024975..e27a59cc32c72 100644 --- a/src/tools/miri/tests/pass/available-parallelism-miri-num-cpus.rs +++ b/src/tools/miri/tests/pass/available-parallelism-miri-num-cpus.rs @@ -4,5 +4,5 @@ use std::num::NonZeroUsize; use std::thread::available_parallelism; fn main() { - assert_eq!(available_parallelism().unwrap(), NonZeroUsize::new(1024).unwrap()); + assert_eq!(available_parallelism().unwrap(), NonZero::::new(1024).unwrap()); } diff --git a/src/tools/miri/tests/pass/function_calls/abi_compat.rs b/src/tools/miri/tests/pass/function_calls/abi_compat.rs index 14fd2d333d4fb..f7a74f16541fd 100644 --- a/src/tools/miri/tests/pass/function_calls/abi_compat.rs +++ b/src/tools/miri/tests/pass/function_calls/abi_compat.rs @@ -70,7 +70,7 @@ fn main() { test_abi_compat(0usize, 0u64); test_abi_compat(0isize, 0i64); } - test_abi_compat(42u32, num::NonZeroU32::new(1).unwrap()); + test_abi_compat(42u32, num::NonZero::::new(1).unwrap()); // - `char` and `u32`. test_abi_compat(42u32, 'x'); // - Reference/pointer types with the same pointee. @@ -86,9 +86,9 @@ fn main() { // - Guaranteed null-pointer-optimizations (RFC 3391). test_abi_compat(&0u32 as *const u32, Some(&0u32)); test_abi_compat(main as fn(), Some(main as fn())); - test_abi_compat(0u32, Some(num::NonZeroU32::new(1).unwrap())); + test_abi_compat(0u32, Some(num::NonZero::::new(1).unwrap())); test_abi_compat(&0u32 as *const u32, Some(Wrapper(&0u32))); - test_abi_compat(0u32, Some(Wrapper(num::NonZeroU32::new(1).unwrap()))); + test_abi_compat(0u32, Some(Wrapper(num::NonZero::::new(1).unwrap()))); // These must work for *any* type, since we guarantee that `repr(transparent)` is ABI-compatible // with the wrapped field. diff --git a/tests/debuginfo/msvc-pretty-enums.rs b/tests/debuginfo/msvc-pretty-enums.rs index 400e8801ca217..90006217f6aae 100644 --- a/tests/debuginfo/msvc-pretty-enums.rs +++ b/tests/debuginfo/msvc-pretty-enums.rs @@ -210,7 +210,7 @@ fn main() { let j = CStyleEnum::High; let k = Some("IAMA optional string!".to_string()); let l = Result::::Ok(42); - let niche128_some = Some(NonZeroI128::new(123456).unwrap()); + let niche128_some = Some(NonZero::::new(123456).unwrap()); let niche128_none: Option = None; let wrapping_niche128_untagged = @@ -224,7 +224,7 @@ fn main() { let niche_w_fields_1_some = NicheLayoutWithFields1::A(&77, 7); let niche_w_fields_1_none = NicheLayoutWithFields1::B(99); - let niche_w_fields_2_some = NicheLayoutWithFields2::A(NonZeroU32::new(800).unwrap(), 900); + let niche_w_fields_2_some = NicheLayoutWithFields2::A(NonZero::::new(800).unwrap(), 900); let niche_w_fields_2_none = NicheLayoutWithFields2::B(1000); let niche_w_fields_3_some = NicheLayoutWithFields3::A(137, true); diff --git a/tests/debuginfo/numeric-types.rs b/tests/debuginfo/numeric-types.rs index a1b5ae792a16b..17531c8fe4545 100644 --- a/tests/debuginfo/numeric-types.rs +++ b/tests/debuginfo/numeric-types.rs @@ -243,19 +243,19 @@ use std::num::*; use std::sync::atomic::*; fn main() { - let nz_i8 = NonZeroI8::new(11).unwrap(); - let nz_i16 = NonZeroI16::new(22).unwrap(); - let nz_i32 = NonZeroI32::new(33).unwrap(); - let nz_i64 = NonZeroI64::new(44).unwrap(); - let nz_i128 = NonZeroI128::new(55).unwrap(); - let nz_isize = NonZeroIsize::new(66).unwrap(); - - let nz_u8 = NonZeroU8::new(77).unwrap(); - let nz_u16 = NonZeroU16::new(88).unwrap(); - let nz_u32 = NonZeroU32::new(99).unwrap(); - let nz_u64 = NonZeroU64::new(100).unwrap(); - let nz_u128 = NonZeroU128::new(111).unwrap(); - let nz_usize = NonZeroUsize::new(122).unwrap(); + let nz_i8 = NonZero::::new(11).unwrap(); + let nz_i16 = NonZero::::new(22).unwrap(); + let nz_i32 = NonZero::::new(33).unwrap(); + let nz_i64 = NonZero::::new(44).unwrap(); + let nz_i128 = NonZero::::new(55).unwrap(); + let nz_isize = NonZero::::new(66).unwrap(); + + let nz_u8 = NonZero::::new(77).unwrap(); + let nz_u16 = NonZero::::new(88).unwrap(); + let nz_u32 = NonZero::::new(99).unwrap(); + let nz_u64 = NonZero::::new(100).unwrap(); + let nz_u128 = NonZero::::new(111).unwrap(); + let nz_usize = NonZero::::new(122).unwrap(); let w_i8 = Wrapping(10i8); let w_i16 = Wrapping(20i16); diff --git a/tests/ui/consts/ice-48279.rs b/tests/ui/consts/ice-48279.rs index d1d90df240ca5..47995a9871449 100644 --- a/tests/ui/consts/ice-48279.rs +++ b/tests/ui/consts/ice-48279.rs @@ -15,7 +15,7 @@ impl NonZeroU32 { } } -//pub const FOO_ATOM: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(7) }; +//pub const FOO_ATOM: NonZeroU32 = unsafe { NonZero::::new_unchecked(7) }; pub const FOO_ATOM: NonZeroU32 = unsafe { NonZeroU32 { value: 7 } }; fn main() { diff --git a/tests/ui/consts/tuple-struct-constructors.rs b/tests/ui/consts/tuple-struct-constructors.rs index 1655f0eb85038..e57f8dd3cbd5a 100644 --- a/tests/ui/consts/tuple-struct-constructors.rs +++ b/tests/ui/consts/tuple-struct-constructors.rs @@ -5,6 +5,6 @@ use std::num::NonZeroU64; fn main() { - const FOO: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(2) }; + const FOO: NonZeroU64 = unsafe { NonZero::::new_unchecked(2) }; if let FOO = FOO {} } diff --git a/tests/ui/mismatched_types/non_zero_assigned_something.rs b/tests/ui/mismatched_types/non_zero_assigned_something.rs index d2adbe01c1828..c65f4da376d43 100644 --- a/tests/ui/mismatched_types/non_zero_assigned_something.rs +++ b/tests/ui/mismatched_types/non_zero_assigned_something.rs @@ -1,9 +1,9 @@ fn main() { let _: std::num::NonZeroU64 = 1; //~^ ERROR mismatched types - //~| HELP consider calling `NonZeroU64::new` + //~| HELP consider calling `NonZero::::new` let _: Option = 1; //~^ ERROR mismatched types - //~| HELP consider calling `NonZeroU64::new` + //~| HELP consider calling `NonZero::::new` } diff --git a/tests/ui/numbers-arithmetic/overflowing-neg-nonzero.rs b/tests/ui/numbers-arithmetic/overflowing-neg-nonzero.rs index dabb0d50cbb5a..a8e74aa3400f9 100644 --- a/tests/ui/numbers-arithmetic/overflowing-neg-nonzero.rs +++ b/tests/ui/numbers-arithmetic/overflowing-neg-nonzero.rs @@ -8,5 +8,5 @@ use std::num::NonZeroI8; fn main() { - let _x = -NonZeroI8::new(i8::MIN).unwrap(); + let _x = -NonZero::::new(i8::MIN).unwrap(); } diff --git a/tests/ui/print_type_sizes/niche-filling.rs b/tests/ui/print_type_sizes/niche-filling.rs index 5ee5085ddc8e1..a2089ab371a33 100644 --- a/tests/ui/print_type_sizes/niche-filling.rs +++ b/tests/ui/print_type_sizes/niche-filling.rs @@ -56,7 +56,7 @@ pub struct NestedNonZero { impl Default for NestedNonZero { fn default() -> Self { - NestedNonZero { pre: 0, val: unsafe { NonZeroU32::new_unchecked(1) }, post: 0 } + NestedNonZero { pre: 0, val: unsafe { NonZero::::new_unchecked(1) }, post: 0 } } } diff --git a/tests/ui/structs-enums/type-sizes.rs b/tests/ui/structs-enums/type-sizes.rs index 490d6a2f9189e..9a3562b47e98c 100644 --- a/tests/ui/structs-enums/type-sizes.rs +++ b/tests/ui/structs-enums/type-sizes.rs @@ -317,7 +317,7 @@ pub fn main() { // Ideal layouts: (bool, u8, NonZeroU16) or (NonZeroU16, u8, bool) // Currently the layout algorithm will choose the latter because it doesn't attempt // to aggregate multiple smaller fields to move a niche before a higher-alignment one. - let b = BoolInTheMiddle( NonZeroU16::new(1).unwrap(), true, 0); + let b = BoolInTheMiddle( NonZero::::new(1).unwrap(), true, 0); assert!(ptr::from_ref(&b.1).addr() > ptr::from_ref(&b.2).addr()); assert_eq!(size_of::>(), size_of::()); diff --git a/tests/ui/suggestions/core-std-import-order-issue-83564.rs b/tests/ui/suggestions/core-std-import-order-issue-83564.rs index b7fe5af7bf8a1..f2f5fe20b4a1a 100644 --- a/tests/ui/suggestions/core-std-import-order-issue-83564.rs +++ b/tests/ui/suggestions/core-std-import-order-issue-83564.rs @@ -5,6 +5,6 @@ fn main() { //~^ HELP consider importing one of these items - let _x = NonZeroU32::new(5).unwrap(); + let _x = NonZero::::new(5).unwrap(); //~^ ERROR failed to resolve: use of undeclared type `NonZeroU32` }