Skip to content

Commit

Permalink
Use size_of from the prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
nvzqz committed Dec 5, 2024
1 parent d214f88 commit f5ff95b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ impl<T: fmt::Debug> fmt::Debug for AllocOpMap<T> {
impl ThreadAllocTallyMap {
#[inline]
pub const fn new() -> Self {
unsafe { std::mem::transmute([0u8; std::mem::size_of::<Self>()]) }
unsafe { std::mem::transmute([0u8; size_of::<Self>()]) }
}

/// Returns `true` if all tallies are 0.
Expand Down Expand Up @@ -602,7 +602,7 @@ mod tests {

// Helper to create `ThreadAllocTallyMap` since each operation only
// changes `buf` by 1 `i32`.
let item_tally = ThreadAllocTally { count: 1, size: std::mem::size_of::<i32>() as _ };
let item_tally = ThreadAllocTally { count: 1, size: size_of::<i32>() as _ };
let make_tally_map = |op: AllocOp| {
ThreadAllocTallyMap::from_fn(|other_op| {
if other_op == op {
Expand Down
2 changes: 1 addition & 1 deletion src/bench/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ where
// This can be done multiple times without invoking a `Drop` destructor
// because it implements `Copy`.
let bench_impl: B = unsafe {
assert_eq!(mem::size_of::<B>(), 0, "benchmark closure expected to be zero-sized");
assert_eq!(size_of::<B>(), 0, "benchmark closure expected to be zero-sized");
mem::zeroed()
};

Expand Down
4 changes: 2 additions & 2 deletions src/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ impl<'a> BenchContext<'a> {
let sample_start: UntaggedTimestamp;
let sample_end: UntaggedTimestamp;

if mem::size_of::<I>() == 0 && (mem::size_of::<O>() == 0 || !mem::needs_drop::<O>()) {
if size_of::<I>() == 0 && (size_of::<O>() == 0 || !mem::needs_drop::<O>()) {
// Use a range instead of `defer_store` to make the benchmarking
// loop cheaper.

Expand Down Expand Up @@ -928,7 +928,7 @@ impl<'a> BenchContext<'a> {
// Drop outputs and inputs.
for _ in 0..sample_size {
// Output only needs drop if ZST.
if mem::size_of::<O>() == 0 {
if size_of::<O>() == 0 {
// SAFETY: Output is a ZST, so we can construct one out
// of thin air.
unsafe { _ = mem::zeroed::<O>() }
Expand Down
15 changes: 7 additions & 8 deletions src/counter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! }
//! ```
use std::{any::Any, mem};
use std::any::Any;

mod any_counter;
mod collection;
Expand Down Expand Up @@ -129,30 +129,29 @@ impl BytesCount {
Self { count: count.into_max_uint() }
}

/// Counts the size of a type with [`std::mem::size_of`].
/// Counts the size of a type with [`size_of`].
#[inline]
#[doc(alias = "size_of")]
pub const fn of<T>() -> Self {
Self { count: mem::size_of::<T>() as MaxCountUInt }
Self { count: size_of::<T>() as MaxCountUInt }
}

/// Counts the size of multiple instances of a type with
/// [`std::mem::size_of`].
/// Counts the size of multiple instances of a type with [`size_of`].
#[inline]
#[doc(alias = "size_of")]
pub const fn of_many<T>(n: usize) -> Self {
match (mem::size_of::<T>() as MaxCountUInt).checked_mul(n as MaxCountUInt) {
match (size_of::<T>() as MaxCountUInt).checked_mul(n as MaxCountUInt) {
Some(count) => Self { count },
None => panic!("overflow"),
}
}

/// Counts the size of a value with [`std::mem::size_of_val`].
/// Counts the size of a value with [`size_of_val`].
#[inline]
#[doc(alias = "size_of_val")]
pub fn of_val<T: ?Sized>(val: &T) -> Self {
// TODO: Make const, https://github.com/rust-lang/rust/issues/46571
Self { count: mem::size_of_val(val) as MaxCountUInt }
Self { count: size_of_val(val) as MaxCountUInt }
}

/// Counts the bytes of [`Iterator::Item`s](Iterator::Item).
Expand Down
2 changes: 1 addition & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) fn defer<F: FnOnce()>(f: F) -> impl Drop {
#[inline]
pub(crate) fn slice_ptr_index<T>(slice: &[T], ptr: *const T) -> usize {
// Safe pointer `offset_from`.
(ptr as usize - slice.as_ptr() as usize) / std::mem::size_of::<T>()
(ptr as usize - slice.as_ptr() as usize) / size_of::<T>()
}

/// Returns the values in the middle of `slice`.
Expand Down
2 changes: 1 addition & 1 deletion src/util/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<T> PThreadKey<T> {
where
D: FnOnce(NonNull<T>) + Copy,
{
assert_eq!(std::mem::size_of::<D>(), 0);
assert_eq!(size_of::<D>(), 0);

unsafe extern "C" fn dtor<T, D>(ptr: *mut libc::c_void)
where
Expand Down

0 comments on commit f5ff95b

Please sign in to comment.