Skip to content

Commit

Permalink
Revert "chore: address MSRV TODOs for 1.81 (#781)"
Browse files Browse the repository at this point in the history
This reverts commit d37a5aa.
  • Loading branch information
DaniPopes committed Oct 28, 2024
1 parent bd028e1 commit e881612
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
20 changes: 6 additions & 14 deletions crates/sol-types/src/abi/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ 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 @@ -59,18 +53,16 @@ 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])`
unsafe { mem::transmute::<Vec<Word>, Vec<[u8; 32]>>(self.finish()) }.into_flattened()
crate::impl_core::into_flattened::<u8, 32>(unsafe {
mem::transmute::<Vec<Word>, Vec<[u8; 32]>>(self.buf)
})
}

/// Determine the current suffix offset.
Expand Down
40 changes: 40 additions & 0 deletions crates/sol-types/src/impl_core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
//! 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 @@ -79,3 +88,34 @@ 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 e881612

Please sign in to comment.