Skip to content

Commit

Permalink
chore: address MSRV TODOs for 1.81 (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Oct 24, 2024
1 parent 7b84276 commit d37a5aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 46 deletions.
20 changes: 14 additions & 6 deletions crates/sol-types/src/abi/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ impl Encoder {
}
}

/// Return a reference to the encoded words.
#[inline]
pub fn words(&self) -> &[Word] {
&self.buf
}

/// Finish the encoding process, returning the encoded words.
///
/// Use `into_bytes` instead to flatten the words into bytes.
Expand All @@ -53,16 +59,18 @@ impl Encoder {
self.buf
}

/// Return a reference to the encoded bytes.
#[inline]
pub fn bytes(&self) -> &[u8] {
// SAFETY: `#[repr(transparent)] FixedBytes<N>([u8; N])`
unsafe { &*(self.words() as *const [Word] as *const [[u8; 32]]) }.as_flattened()
}

/// Finish the encoding process, returning the encoded bytes.
#[inline]
pub fn into_bytes(self) -> Vec<u8> {
// TODO: remove once `Vec::into_flattened` is stabilized.
// unsafe { mem::transmute::<Vec<_>, Vec<[u8; 32]>>(self.buf) }.into_flattened()

// SAFETY: `#[repr(transparent)] FixedBytes<N>([u8; N])`
crate::impl_core::into_flattened::<u8, 32>(unsafe {
mem::transmute::<Vec<Word>, Vec<[u8; 32]>>(self.buf)
})
unsafe { mem::transmute::<Vec<Word>, Vec<[u8; 32]>>(self.finish()) }.into_flattened()
}

/// Determine the current suffix offset.
Expand Down
40 changes: 0 additions & 40 deletions crates/sol-types/src/impl_core.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
//! Modified implementations of unstable libcore functions.

use alloc::vec::Vec;
use core::mem::{self, MaybeUninit};

trait Ext {
const IS_ZST: bool;
}

impl<T> Ext for T {
const IS_ZST: bool = mem::size_of::<Self>() == 0;
}

/// [`core::array::try_from_fn`]
#[inline]
pub(crate) fn try_from_fn<F, T, E, const N: usize>(mut cb: F) -> Result<[T; N], E>
Expand Down Expand Up @@ -88,34 +79,3 @@ pub(crate) unsafe fn array_assume_init<T, const N: usize>(array: [MaybeUninit<T>
unsafe fn transpose<T, const N: usize>(array: [MaybeUninit<T>; N]) -> MaybeUninit<[T; N]> {
mem::transmute_copy::<[MaybeUninit<T>; N], MaybeUninit<[T; N]>>(&mem::ManuallyDrop::new(&array))
}

// TODO(MSRV-1.80): remove
/// [`Vec::into_flattened`].
#[inline]
pub(crate) fn into_flattened<T, const N: usize>(vec: Vec<[T; N]>) -> Vec<T> {
let (ptr, len, cap) = into_raw_parts(vec);
let (new_len, new_cap) = if T::IS_ZST {
(len.checked_mul(N).expect("vec len overflow"), usize::MAX)
} else {
// SAFETY:
// - `cap * N` cannot overflow because the allocation is already in
// the address space.
// - Each `[T; N]` has `N` valid elements, so there are `len * N`
// valid elements in the allocation.
unsafe { (len.checked_mul(N).unwrap_unchecked(), cap.checked_mul(N).unwrap_unchecked()) }
};
// SAFETY:
// - `ptr` was allocated by `self`
// - `ptr` is well-aligned because `[T; N]` has the same alignment as `T`.
// - `new_cap` refers to the same sized allocation as `cap` because
// `new_cap * size_of::<T>()` == `cap * size_of::<[T; N]>()`
// - `len` <= `cap`, so `len * N` <= `cap * N`.
unsafe { Vec::from_raw_parts(ptr.cast(), new_len, new_cap) }
}

/// [`Vec::into_raw_parts`]
#[inline(always)]
fn into_raw_parts<T>(vec: Vec<T>) -> (*mut T, usize, usize) {
let mut me = mem::ManuallyDrop::new(vec);
(me.as_mut_ptr(), me.len(), me.capacity())
}

0 comments on commit d37a5aa

Please sign in to comment.