Rollup merge of #76492 - fusion-engineering-forks:int-bits, r=dtolnay
Add associated constant `BITS` to all integer types Recently I've regularly come across this snippet (in a few different crates, including `core` and `std`): ```rust std::mem::size_of<usize>() * 8 ``` I think it's time for a `usize::BITS`.
This commit is contained in:
commit
fef3324043
18 changed files with 72 additions and 64 deletions
|
@ -146,7 +146,7 @@
|
|||
|
||||
use core::fmt;
|
||||
use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen};
|
||||
use core::mem::{self, size_of, swap, ManuallyDrop};
|
||||
use core::mem::{self, swap, ManuallyDrop};
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::ptr;
|
||||
|
||||
|
@ -617,7 +617,7 @@ impl<T: Ord> BinaryHeap<T> {
|
|||
|
||||
#[inline(always)]
|
||||
fn log2_fast(x: usize) -> usize {
|
||||
8 * size_of::<usize>() - (x.leading_zeros() as usize) - 1
|
||||
(usize::BITS - x.leading_zeros() - 1) as usize
|
||||
}
|
||||
|
||||
// `rebuild` takes O(len1 + len2) operations
|
||||
|
|
|
@ -101,6 +101,7 @@
|
|||
#![feature(fn_traits)]
|
||||
#![feature(fundamental)]
|
||||
#![feature(inplace_iteration)]
|
||||
#![feature(int_bits_const)]
|
||||
#![feature(lang_items)]
|
||||
#![feature(layout_for_ptr)]
|
||||
#![feature(libc)]
|
||||
|
|
|
@ -528,7 +528,7 @@ unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
|
|||
|
||||
#[inline]
|
||||
fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> {
|
||||
if mem::size_of::<usize>() < 8 && alloc_size > isize::MAX as usize {
|
||||
if usize::BITS < 64 && alloc_size > isize::MAX as usize {
|
||||
Err(CapacityOverflow)
|
||||
} else {
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue