Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(primitives): improve from_slice functions #546

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ impl<const N: usize> FixedBytes<N> {
///
/// Panics if the underlying call to
/// [`getrandom_uninit`](getrandom::getrandom_uninit) fails.
#[inline]
#[cfg(feature = "getrandom")]
#[inline]
#[track_caller]
pub fn randomize(&mut self) {
self.try_randomize().unwrap()
Expand Down Expand Up @@ -450,17 +450,22 @@ impl<const N: usize> FixedBytes<N> {

/// Create a new [`FixedBytes`] from the given slice `src`.
///
/// For a fallible version, use the `TryFrom<&[u8]>` implementation.
///
/// # Note
///
/// The given bytes are interpreted in big endian order.
///
/// # Panics
///
/// If the length of `src` and the number of bytes in `Self` do not match.
#[track_caller]
#[inline]
pub fn from_slice(value: &[u8]) -> Self {
Self::try_from(value).unwrap()
#[track_caller]
pub fn from_slice(src: &[u8]) -> Self {
match Self::try_from(src) {
Ok(x) => x,
Err(_) => panic!("cannot convert a slice of length {} to FixedBytes<{N}>", src.len()),
}
}

/// Create a new [`FixedBytes`] from the given slice `src`, left-padding it
Expand All @@ -473,8 +478,8 @@ impl<const N: usize> FixedBytes<N> {
/// # Panics
///
/// Panics if `src.len() > N`.
#[track_caller]
#[inline]
#[track_caller]
pub fn left_padding_from(value: &[u8]) -> Self {
let len = value.len();
assert!(len <= N, "slice is too large. Expected <={N} bytes, got {len}");
Expand All @@ -493,8 +498,8 @@ impl<const N: usize> FixedBytes<N> {
/// # Panics
///
/// Panics if `src.len() > N`.
#[track_caller]
#[inline]
#[track_caller]
pub fn right_padding_from(value: &[u8]) -> Self {
let len = value.len();
assert!(len <= N, "slice is too large. Expected <={N} bytes, got {len}");
Expand Down
12 changes: 9 additions & 3 deletions crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ macro_rules! wrap_fixed_bytes {

/// Create a new byte array from the given slice `src`.
///
/// For a fallible version, use the `TryFrom<&[u8]>` implementation.
///
/// # Note
///
/// The given bytes are interpreted in big endian order.
Expand All @@ -222,8 +224,12 @@ macro_rules! wrap_fixed_bytes {
///
/// If the length of `src` and the number of bytes in `Self` do not match.
#[inline]
#[track_caller]
pub fn from_slice(src: &[u8]) -> Self {
Self($crate::FixedBytes::from_slice(src))
match Self::try_from(src) {
Ok(x) => x,
Err(_) => panic!("cannot convert a slice of length {} to {}", src.len(), stringify!($name)),
}
}

/// Create a new byte array from the given slice `src`, left-padding it
Expand All @@ -236,8 +242,8 @@ macro_rules! wrap_fixed_bytes {
/// # Panics
///
/// Panics if `src.len() > N`.
#[track_caller]
#[inline]
#[track_caller]
pub fn left_padding_from(value: &[u8]) -> Self {
Self($crate::FixedBytes::left_padding_from(value))
}
Expand All @@ -252,8 +258,8 @@ macro_rules! wrap_fixed_bytes {
/// # Panics
///
/// Panics if `src.len() > N`.
#[track_caller]
#[inline]
#[track_caller]
pub fn right_padding_from(value: &[u8]) -> Self {
Self($crate::FixedBytes::right_padding_from(value))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/signed/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ impl<const BITS: usize, const LIMBS: usize> Signed<BITS, LIMBS> {
///
/// Panics if the value is to large for the bit-size of the Uint.
#[inline(always)]
#[must_use]
#[track_caller]
#[must_use]
pub const fn from_limbs(limbs: [u64; LIMBS]) -> Self {
Self(Uint::from_limbs(limbs))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-macro/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ fn tokens_for_sol(name: &Ident, sol: &str) -> Result<TokenStream> {
Ok(tokens)
}

#[track_caller]
#[inline]
#[track_caller]
fn id(s: impl AsRef<str>) -> Ident {
// Ident::new panics on Rust keywords and `r#` prefixes
syn::parse_str(s.as_ref()).unwrap()
Expand Down
Loading