Skip to content

Commit

Permalink
use core::mem::{size_of,align_of} instead of qualifying.
Browse files Browse the repository at this point in the history
Address a new complaint from the newest Clippy.

Remove the relevant outdated/misleading advice in STYLE.md.
  • Loading branch information
briansmith committed Sep 27, 2024
1 parent 24086cf commit 00de91f
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 36 deletions.
9 changes: 0 additions & 9 deletions STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ style guidelines for that code are in the second section of this document.
*ring* usually follows the [Rust Guidelines](https://aturon.github.io/), but
there are some differences and *ring* adds additional guidelines.

## Imports (`use`) and Qualification

In general, import modules, not non-module items, e.g. `use core`, not
`use core::mem::size_of_val`. This means that the uses of such functions must
be qualified: `core::mem::size_of_val(x)`, not `size_of_val(x)`. Exceptions may
be made for things that are very annoying to qualify; for example, we usually
`use super::input::*` or `use super::input::Input` because writing things like
`input::Input` is highly annoying.

## Submodules and file naming.

In general, avoid nesting modules and avoid exporting any non-module items from
Expand Down
2 changes: 1 addition & 1 deletion src/aead/chacha/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn chacha_core(output: &mut [u8; BLOCK_LEN], input: &State) {
}

output
.chunks_exact_mut(core::mem::size_of::<u32>())
.chunks_exact_mut(size_of::<u32>())
.zip(x.iter())
.for_each(|(output, &x)| output.copy_from_slice(&x.to_le_bytes()));
}
Expand Down
3 changes: 2 additions & 1 deletion src/arithmetic/constant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::limb::Limb;
use core::mem::size_of;

const fn parse_digit(d: u8) -> u8 {
match d.to_ascii_lowercase() {
Expand All @@ -12,7 +13,7 @@ const fn parse_digit(d: u8) -> u8 {
pub const fn limbs_from_hex<const LIMBS: usize>(hex: &str) -> [Limb; LIMBS] {
let hex = hex.as_bytes();
let mut limbs = [0; LIMBS];
let limb_nibbles = core::mem::size_of::<Limb>() * 2;
let limb_nibbles = size_of::<Limb>() * 2;
let mut i = 0;

while i < hex.len() {
Expand Down
9 changes: 3 additions & 6 deletions src/bssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ impl From<Result> for core::result::Result<(), error::Unspecified> {
mod tests {
mod result {
use crate::{bssl, c};
use core::mem;
use core::mem::{align_of, size_of};

#[test]
fn size_and_alignment() {
type Underlying = c::int;
assert_eq!(mem::size_of::<bssl::Result>(), mem::size_of::<Underlying>());
assert_eq!(
mem::align_of::<bssl::Result>(),
mem::align_of::<Underlying>()
);
assert_eq!(size_of::<bssl::Result>(), size_of::<Underlying>());
assert_eq!(align_of::<bssl::Result>(), align_of::<Underlying>());
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

pub(crate) use self::features::Features;
use core::mem::size_of;

macro_rules! impl_get_feature {
{ $feature:path => $T:ident } => {
Expand Down Expand Up @@ -96,7 +97,7 @@ mod features {
}
}

const _: () = assert!(core::mem::size_of::<Features>() == 0);
const _: () = assert!(size_of::<Features>() == 0);

cfg_if::cfg_if! {
if #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] {
Expand Down
7 changes: 4 additions & 3 deletions src/cpu/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

mod abi_assumptions {
use corE::mem::size_of;

// TODO: Support ARM64_32; see
// https://github.com/briansmith/ring/issues/1832#issuecomment-1892928147. This also requires
// replacing all `cfg(target_pointer_width)` logic for non-pointer/reference things
Expand All @@ -21,9 +23,8 @@ mod abi_assumptions {
const _ASSUMED_POINTER_SIZE: usize = 8;
#[cfg(target_arch = "arm")]
const _ASSUMED_POINTER_SIZE: usize = 4;
const _ASSUMED_USIZE_SIZE: () = assert!(core::mem::size_of::<usize>() == _ASSUMED_POINTER_SIZE);
const _ASSUMED_REF_SIZE: () =
assert!(core::mem::size_of::<&'static u8>() == _ASSUMED_POINTER_SIZE);
const _ASSUMED_USIZE_SIZE: () = assert!(size_of::<usize>() == _ASSUMED_POINTER_SIZE);
const _ASSUMED_REF_SIZE: () = assert!(size_of::<&'static u8>() == _ASSUMED_POINTER_SIZE);

// To support big-endian, we'd need to make several changes as described in
// https://github.com/briansmith/ring/issues/1832.
Expand Down
7 changes: 4 additions & 3 deletions src/cpu/intel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use cfg_if::cfg_if;

mod abi_assumptions {
use core::mem::size_of;

// TOOD: Support targets that do not have SSE and SSE2 enabled, such as
// x86_64-unknown-linux-none. See
// https://github.com/briansmith/ring/issues/1793#issuecomment-1793243725,
Expand All @@ -27,9 +29,8 @@ mod abi_assumptions {
const _ASSUMED_POINTER_SIZE: usize = 8;
#[cfg(target_arch = "x86")]
const _ASSUMED_POINTER_SIZE: usize = 4;
const _ASSUMED_USIZE_SIZE: () = assert!(core::mem::size_of::<usize>() == _ASSUMED_POINTER_SIZE);
const _ASSUMED_REF_SIZE: () =
assert!(core::mem::size_of::<&'static u8>() == _ASSUMED_POINTER_SIZE);
const _ASSUMED_USIZE_SIZE: () = assert!(size_of::<usize>() == _ASSUMED_POINTER_SIZE);
const _ASSUMED_REF_SIZE: () = assert!(size_of::<&'static u8>() == _ASSUMED_POINTER_SIZE);

const _ASSUMED_ENDIANNESS: () = assert!(cfg!(target_endian = "little"));
}
Expand Down
5 changes: 3 additions & 2 deletions src/digest/dynstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use super::{format_output, sha1, sha2, Output};
use crate::{cpu, polyfill::slice};
use core::mem::size_of;

// Invariant: When constructed with `new32` (resp. `new64`), `As32` (resp.
// `As64`) is the active variant.
Expand Down Expand Up @@ -92,7 +93,7 @@ pub(super) fn sha256_format_output(state: DynState) -> Output {
unreachable!();
}
};
format_output::<_, _, { core::mem::size_of::<u32>() }>(state, u32::to_be_bytes)
format_output::<_, _, { size_of::<u32>() }>(state, u32::to_be_bytes)
}

pub(super) fn sha512_format_output(state: DynState) -> Output {
Expand All @@ -102,5 +103,5 @@ pub(super) fn sha512_format_output(state: DynState) -> Output {
unreachable!();
}
};
format_output::<_, _, { core::mem::size_of::<u64>() }>(state, u64::to_be_bytes)
format_output::<_, _, { size_of::<u64>() }>(state, u64::to_be_bytes)
}
4 changes: 2 additions & 2 deletions src/polyfill/array_flat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mod tests {
}
impl ExactSizeIterator for DownwardCounter {}

const MAX: usize = usize::MAX / core::mem::size_of::<usize>();
const MAX: usize = usize::MAX / size_of::<usize>();

static TEST_CASES: &[(usize, bool)] = &[(MAX, true), (MAX + 1, false)];
TEST_CASES.iter().copied().for_each(|(input_len, is_some)| {
Expand All @@ -119,7 +119,7 @@ mod tests {
let mapped = ArrayFlatMap::new(inner, usize::to_be_bytes);
assert_eq!(mapped.is_some(), is_some);
if let Some(mapped) = mapped {
assert_eq!(mapped.len(), input_len * core::mem::size_of::<usize>());
assert_eq!(mapped.len(), input_len * size_of::<usize>());
}
});
}
Expand Down
6 changes: 2 additions & 4 deletions src/polyfill/cstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ pub struct Ref(&'static [u8]);
impl Ref {
#[inline(always)]
pub fn as_ptr(&self) -> *const c_char {
const _SAME_ALIGNMENT: () =
assert!(core::mem::align_of::<u8>() == core::mem::align_of::<c_char>());
const _SAME_SIZE: () =
assert!(core::mem::size_of::<u8>() == core::mem::size_of::<c_char>());
const _SAME_ALIGNMENT: () = assert!(align_of::<u8>() == align_of::<c_char>());
const _SAME_SIZE: () = assert!(size_of::<u8>() == size_of::<c_char>());

// It is safe to cast a `*const u8` to a `const c_char` as they are the
// same size and alignment.
Expand Down
4 changes: 2 additions & 2 deletions src/polyfill/notsend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use crate::test;
use core::marker::PhantomData;
use core::{marker::PhantomData, mem::size_of};

/// A ZST that can be added to any type to make the type `!Send`.
#[derive(Clone, Copy)]
Expand All @@ -25,4 +25,4 @@ impl NotSend {

const _: () = test::compile_time_assert_clone::<NotSend>();
const _: () = test::compile_time_assert_copy::<NotSend>();
const _: () = assert!(core::mem::size_of::<NotSend>() == 0);
const _: () = assert!(size_of::<NotSend>() == 0);
6 changes: 4 additions & 2 deletions src/polyfill/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use core::mem::size_of;

// TODO(MSRV feature(slice_as_chunks)): Use `slice::as_chunks` instead.
// This is copied from the libcore implementation of `slice::as_chunks`.
#[inline(always)]
Expand Down Expand Up @@ -56,7 +58,7 @@ pub fn as_chunks_mut<T, const N: usize>(slice: &mut [T]) -> (&mut [[T; N]], &mut
// TODO(MSRV feature(slice_flatten)): Use `slice::flatten` instead.
// This is derived from the libcore implementation, using only stable APIs.
pub fn flatten<T, const N: usize>(slice: &[[T; N]]) -> &[T] {
let len = if core::mem::size_of::<T>() == 0 {
let len = if size_of::<T>() == 0 {
slice.len().checked_mul(N).expect("slice len overflow")
} else {
// SAFETY: `slice.len() * N` cannot overflow because `slice` is
Expand All @@ -70,7 +72,7 @@ pub fn flatten<T, const N: usize>(slice: &[[T; N]]) -> &[T] {
// TODO(MSRV feature(slice_flatten)): Use `slice::flatten_mut` instead.
// This is derived from the libcore implementation, using only stable APIs.
pub fn flatten_mut<T, const N: usize>(slice: &mut [[T; N]]) -> &mut [T] {
let len = if core::mem::size_of::<T>() == 0 {
let len = if size_of::<T>() == 0 {
slice.len().checked_mul(N).expect("slice len overflow")
} else {
// SAFETY: `slice.len() * N` cannot overflow because `slice` is
Expand Down

0 comments on commit 00de91f

Please sign in to comment.