Skip to content

Commit

Permalink
Remove From [u8; n] impl for uint types (#859)
Browse files Browse the repository at this point in the history
* update uint and deps

* Fix uppercase

* Apply suggestions from code review

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update changelogs

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Alexander Theißen <alex.theissen@me.com>
Co-authored-by: ordian <write@reusable.software>
  • Loading branch information
3 people authored Sep 10, 2024
1 parent 701148e commit 0db43ee
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 80 deletions.
2 changes: 2 additions & 0 deletions ethereum-types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog].
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
### Breaking
- Updated `uint` to 0.10. [#859](https://github.com/paritytech/parity-common/pull/859)

## [0.14.1] - 2022-11-29
- Added `if_ethbloom` conditional macro. [#682](https://github.com/paritytech/parity-common/pull/682)
Expand Down
4 changes: 2 additions & 2 deletions ethereum-types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ macro_rules! impl_uint_conversions {

fn from_uint(value: &$uint) -> Self {
let mut ret = $hash::zero();
value.to_big_endian(ret.as_bytes_mut());
value.write_as_big_endian(ret.as_bytes_mut());
ret
}

fn into_uint(&self) -> $uint {
$uint::from(self.as_ref() as &[u8])
$uint::from_big_endian(self.as_ref() as &[u8])
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions ethereum-types/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ mod tests {
#[test]
fn fixed_arrays_roundtrip() {
let raw: U256 = "7094875209347850239487502394881".into();
let array: [u8; 32] = raw.into();
let new_raw = array.into();
let array: [u8; 32] = raw.to_big_endian();
let new_raw = U256::from_big_endian(&array);

assert_eq!(raw, new_raw);
}
Expand Down
2 changes: 2 additions & 0 deletions primitive-types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog].
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
### Breaking
- Updated `uint` to 0.10. [#859](https://github.com/paritytech/parity-common/pull/859)

## [0.12.2] - 2023-10-10
- Added `schemars` support via `json-schema` feature. [#785](https://github.com/paritytech/parity-common/pull/785)
Expand Down
3 changes: 1 addition & 2 deletions primitive-types/impls/codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ macro_rules! impl_uint_codec {
($name: ident, $len: expr) => {
impl $crate::codec::Encode for $name {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut bytes = [0u8; $len * 8];
self.to_little_endian(&mut bytes);
let bytes = self.to_little_endian();
bytes.using_encoded(f)
}
}
Expand Down
5 changes: 2 additions & 3 deletions primitive-types/impls/rlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ macro_rules! impl_uint_rlp {
impl $crate::rlp::Encodable for $name {
fn rlp_append(&self, s: &mut $crate::rlp::RlpStream) {
let leading_empty_bytes = $size * 8 - (self.bits() + 7) / 8;
let mut buffer = [0u8; $size * 8];
self.to_big_endian(&mut buffer);
let buffer = self.to_big_endian();
s.encoder().encode_value(&buffer[leading_empty_bytes..]);
}
}
Expand All @@ -35,7 +34,7 @@ macro_rules! impl_uint_rlp {
if !bytes.is_empty() && bytes[0] == 0 {
Err($crate::rlp::DecoderError::RlpInvalidIndirection)
} else if bytes.len() <= $size * 8 {
Ok($name::from(bytes))
Ok($name::from_big_endian(bytes))
} else {
Err($crate::rlp::DecoderError::RlpIsTooBig)
}
Expand Down
4 changes: 4 additions & 0 deletions primitive-types/impls/serde/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
### Breaking
- Updated `uint` to 0.10. [#859](https://github.com/paritytech/parity-common/pull/859)

## [0.4.0] - 2022-09-02
- Support deserializing H256 et al from bytes or sequences of bytes, too. [#668](https://github.com/paritytech/parity-common/pull/668)
- Support deserializing H256 et al from newtype structs containing anything compatible, too. [#672](https://github.com/paritytech/parity-common/pull/672)
Expand Down
5 changes: 2 additions & 3 deletions primitive-types/impls/serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ macro_rules! impl_uint_serde {
S: $crate::serde::Serializer,
{
let mut slice = [0u8; 2 + 2 * $len * 8];
let mut bytes = [0u8; $len * 8];
self.to_big_endian(&mut bytes);
let bytes = self.to_big_endian();
$crate::serialize::serialize_uint(&mut slice, &bytes, serializer)
}
}
Expand All @@ -48,7 +47,7 @@ macro_rules! impl_uint_serde {
deserializer,
$crate::serialize::ExpectedLen::Between(0, &mut bytes),
)?;
Ok(bytes[0..wrote].into())
Ok(Self::from_big_endian(&bytes[0..wrote]))
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions rlp/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn encode_u256() {
ETestPair::from((U256::from(0x0100_0000_u64), hex!("8401000000"))),
ETestPair::from((U256::from(0xffff_ffff_u64), hex!("84ffffffff"))),
ETestPair::from((
hex!(" 8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").into(),
U256::from_big_endian(&hex!(" 8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0")),
hex!("a08090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0"),
)),
];
Expand Down Expand Up @@ -482,7 +482,7 @@ fn decode_untrusted_u256() {
DTestPair::from((U256::from(0x0100_0000_u64), hex!("8401000000"))),
DTestPair::from((U256::from(0xffff_ffff_u64), hex!("84ffffffff"))),
DTestPair::from((
hex!(" 8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").into(),
U256::from_big_endian(&hex!(" 8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0")),
hex!("a08090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0"),
)),
];
Expand Down
2 changes: 2 additions & 0 deletions uint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog].
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
### Breaking
- Removed From<[u8; n]> conversions, renamed `to_big_endian` / `to_little_endian` to write_as_*, and made them return byte arrays. [#859](https://github.com/paritytech/parity-common/pull/859)

## [0.9.5] - 2022-11-29
- Implemented bitwise assign traits. [#690](https://github.com/paritytech/parity-common/pull/690)
Expand Down
7 changes: 3 additions & 4 deletions uint/benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ criterion_group!(
criterion_main!(bigint);

fn to_biguint(x: U256) -> BigUint {
let mut bytes = [0u8; 32];
x.to_little_endian(&mut bytes);
let bytes = x.to_little_endian();
BigUint::from_bytes_le(&bytes)
}

Expand Down Expand Up @@ -662,8 +661,8 @@ fn from_fixed_array(c: &mut Criterion) {
[255, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 0, 0, 0, 0, 0, 213, 0, 0, 0, 0, 0, 0];
c.bench_function("from_fixed_array", move |b| {
b.iter(|| {
let _: U512 = black_box(black_box(ary512).into());
let _: U256 = black_box(black_box(ary256).into());
let _: U512 = black_box(U512::from_big_endian(black_box(&ary512)));
let _: U256 = black_box(U256::from_big_endian(black_box(&ary256)));
})
});
}
Expand Down
55 changes: 21 additions & 34 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,19 +743,34 @@ macro_rules! construct_uint {
(arr[index / 8] >> (((index % 8)) * 8)) as u8
}

/// Convert to big-endian bytes.
#[inline]
pub fn to_big_endian(&self) -> [u8; $n_words * 8] {
let mut bytes = [0u8; $n_words * 8];
self.write_as_big_endian(&mut bytes);
bytes
}

/// Write to the slice in big-endian format.
#[inline]
pub fn to_big_endian(&self, bytes: &mut [u8]) {
pub fn write_as_big_endian(&self, bytes: &mut [u8]) {
use $crate::byteorder::{ByteOrder, BigEndian};
debug_assert!($n_words * 8 == bytes.len());
for i in 0..$n_words {
BigEndian::write_u64(&mut bytes[8 * i..], self.0[$n_words - i - 1]);
}
}

/// Write to the slice in little-endian format.
/// Convert to little-endian bytes.
#[inline]
pub fn to_little_endian(&self, bytes: &mut [u8]) {
pub fn to_little_endian(&self) -> [u8; $n_words * 8] {
let mut bytes = [0u8; $n_words * 8];
self.write_as_little_endian(&mut bytes);
bytes
}

#[inline]
pub fn write_as_little_endian(&self, bytes: &mut [u8]) {
use $crate::byteorder::{ByteOrder, LittleEndian};
debug_assert!($n_words * 8 == bytes.len());
for i in 0..$n_words {
Expand Down Expand Up @@ -1307,26 +1322,6 @@ macro_rules! construct_uint {
}
}

impl $crate::core_::convert::From<$name> for [u8; $n_words * 8] {
fn from(number: $name) -> Self {
let mut arr = [0u8; $n_words * 8];
number.to_big_endian(&mut arr);
arr
}
}

impl $crate::core_::convert::From<[u8; $n_words * 8]> for $name {
fn from(bytes: [u8; $n_words * 8]) -> Self {
Self::from(&bytes)
}
}

impl<'a> $crate::core_::convert::From<&'a [u8; $n_words * 8]> for $name {
fn from(bytes: &[u8; $n_words * 8]) -> Self {
Self::from(&bytes[..])
}
}

impl $crate::core_::default::Default for $name {
fn default() -> Self {
$name::zero()
Expand Down Expand Up @@ -1360,13 +1355,6 @@ macro_rules! construct_uint {
$crate::impl_map_from!($name, i32, i64);
$crate::impl_map_from!($name, isize, i64);

// Converts from big endian representation.
impl<'a> $crate::core_::convert::From<&'a [u8]> for $name {
fn from(bytes: &[u8]) -> $name {
Self::from_big_endian(bytes)
}
}

$crate::impl_try_from_for_primitive!($name, u8);
$crate::impl_try_from_for_primitive!($name, u16);
$crate::impl_try_from_for_primitive!($name, u32);
Expand Down Expand Up @@ -1736,8 +1724,7 @@ macro_rules! construct_uint {
$crate::hex::decode_to_slice(encoded, out).map_err(Self::Err::from)?;
}

let bytes_ref: &[u8] = &bytes;
Ok(From::from(bytes_ref))
Ok(Self::from_big_endian(&bytes))
}
}

Expand Down Expand Up @@ -1787,7 +1774,7 @@ macro_rules! impl_quickcheck_arbitrary_for_uint {
}
});

res.as_ref().into()
Self::from_big_endian(res.as_ref())
}
}
};
Expand All @@ -1809,7 +1796,7 @@ macro_rules! impl_arbitrary_for_uint {
fn arbitrary(u: &mut $crate::arbitrary::Unstructured<'_>) -> $crate::arbitrary::Result<Self> {
let mut res = [0u8; $n_bytes];
u.fill_buffer(&mut res)?;
Ok(Self::from(res))
Ok(Self::from_big_endian(&res))
}
}
};
Expand Down
Loading

0 comments on commit 0db43ee

Please sign in to comment.