From 1bf6c77303a3e73a0870e95a4ee10ce9abaa7c62 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Mon, 27 Jan 2020 10:07:45 +0300 Subject: [PATCH 1/4] Expand const fn coverage --- fixed-hash/src/hash.rs | 15 +++++---------- uint/src/uint.rs | 10 +++------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index ea2210076..66a817d36 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -106,20 +106,17 @@ macro_rules! construct_fixed_hash { impl $name { /// Returns a new fixed hash where all bits are set to the given byte. - #[inline] - pub fn repeat_byte(byte: u8) -> $name { + pub const fn repeat_byte(byte: u8) -> $name { $name([byte; $n_bytes]) } /// Returns a new zero-initialized fixed hash. - #[inline] - pub fn zero() -> $name { + pub const fn zero() -> $name { $name::repeat_byte(0u8) } /// Returns the size of this hash in bytes. - #[inline] - pub fn len_bytes() -> usize { + pub const fn len_bytes() -> usize { $n_bytes } @@ -136,8 +133,7 @@ macro_rules! construct_fixed_hash { } /// Extracts a reference to the byte array containing the entire fixed hash. - #[inline] - pub fn as_fixed_bytes(&self) -> &[u8; $n_bytes] { + pub const fn as_fixed_bytes(&self) -> &[u8; $n_bytes] { &self.0 } @@ -148,8 +144,7 @@ macro_rules! construct_fixed_hash { } /// Returns the inner bytes array. - #[inline] - pub fn to_fixed_bytes(self) -> [u8; $n_bytes] { + pub const fn to_fixed_bytes(self) -> [u8; $n_bytes] { self.0 } diff --git a/uint/src/uint.rs b/uint/src/uint.rs index b15726b72..4e45d3a39 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -472,15 +472,13 @@ macro_rules! construct_uint { } /// Conversion to u32 - #[inline] - pub fn low_u32(&self) -> u32 { + pub const fn low_u32(&self) -> u32 { let &$name(ref arr) = self; arr[0] as u32 } /// Low word (u64) - #[inline] - pub fn low_u64(&self) -> u64 { + pub const fn low_u64(&self) -> u64 { let &$name(ref arr) = self; arr[0] } @@ -600,8 +598,7 @@ macro_rules! construct_uint { /// # Panics /// /// Panics if `index` exceeds the byte width of the number. - #[inline] - pub fn byte(&self, index: usize) -> u8 { + pub const fn byte(&self, index: usize) -> u8 { let &$name(ref arr) = self; (arr[index / 8] >> (((index % 8)) * 8)) as u8 } @@ -641,7 +638,6 @@ macro_rules! construct_uint { } /// Zero (additive identity) of this type. - #[inline] pub const fn zero() -> Self { Self([0; $n_words]) } From 02e4330a3906745e1ee627bb3e2da49b379357d6 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Mon, 27 Jan 2020 11:15:23 +0300 Subject: [PATCH 2/4] also const fn rlp --- rlp/src/rlpin.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rlp/src/rlpin.rs b/rlp/src/rlpin.rs index f1c488626..6cdfaa808 100644 --- a/rlp/src/rlpin.rs +++ b/rlp/src/rlpin.rs @@ -25,7 +25,7 @@ struct OffsetCache { } impl OffsetCache { - fn new(index: usize, offset: usize) -> OffsetCache { + const fn new(index: usize, offset: usize) -> OffsetCache { OffsetCache { index, offset } } } @@ -68,7 +68,7 @@ fn calculate_payload_info(header_bytes: &[u8], len_of_len: usize) -> Result PayloadInfo { + const fn new(header_len: usize, value_len: usize) -> PayloadInfo { PayloadInfo { header_len, value_len } } @@ -128,7 +128,7 @@ impl<'a> fmt::Display for Rlp<'a> { } impl<'a> Rlp<'a> { - pub fn new(bytes: &'a [u8]) -> Rlp<'a> { + pub const fn new(bytes: &'a [u8]) -> Rlp<'a> { Rlp { bytes, offset_cache: Cell::new(None), count_cache: Cell::new(None) } } @@ -374,7 +374,7 @@ pub struct BasicDecoder<'a> { } impl<'a> BasicDecoder<'a> { - pub fn new(rlp: &'a [u8]) -> BasicDecoder<'a> { + pub const fn new(rlp: &'a [u8]) -> BasicDecoder<'a> { BasicDecoder { rlp } } From a0130cda45ec888b0712b34e773a5ff07c2b5ec2 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Mon, 27 Jan 2020 11:36:34 +0300 Subject: [PATCH 3/4] make more uint methods const fn --- uint/src/uint.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 4e45d3a39..a87df5187 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -370,8 +370,7 @@ macro_rules! construct_uint { impl $name { /// Low 2 words (u128) - #[inline] - pub fn low_u128(&self) -> u128 { + pub const fn low_u128(&self) -> u128 { let &$name(ref arr) = self; ((arr[1] as u128) << 64) + arr[0] as u128 } @@ -557,8 +556,7 @@ macro_rules! construct_uint { /// # Panics /// /// Panics if `index` exceeds the bit width of the number. - #[inline] - pub fn bit(&self, index: usize) -> bool { + pub const fn bit(&self, index: usize) -> bool { let &$name(ref arr) = self; arr[index / 64] & (1 << (index % 64)) != 0 } @@ -1062,18 +1060,18 @@ macro_rules! construct_uint { } #[inline(always)] - fn mul_u64(a: u64, b: u64, carry: u64) -> (u64, u64) { - let (hi, lo) = Self::split_u128(u128::from(a) * u128::from(b) + u128::from(carry)); + const fn mul_u64(a: u64, b: u64, carry: u64) -> (u64, u64) { + let (hi, lo) = Self::split_u128(a as u128 * b as u128 + carry as u128); (lo, hi) } #[inline(always)] - fn split(a: u64) -> (u64, u64) { + const fn split(a: u64) -> (u64, u64) { (a >> 32, a & 0xFFFF_FFFF) } #[inline(always)] - fn split_u128(a: u128) -> (u64, u64) { + const fn split_u128(a: u128) -> (u64, u64) { ((a >> 64) as _, (a & 0xFFFFFFFFFFFFFFFF) as _) } From b72d3ac6a9a52ac91d02056304d9391ea510d717 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Mon, 27 Jan 2020 11:43:10 +0300 Subject: [PATCH 4/4] restore inline attribute --- fixed-hash/src/hash.rs | 5 +++++ uint/src/uint.rs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index 66a817d36..09ce43d8c 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -106,16 +106,19 @@ macro_rules! construct_fixed_hash { impl $name { /// Returns a new fixed hash where all bits are set to the given byte. + #[inline] pub const fn repeat_byte(byte: u8) -> $name { $name([byte; $n_bytes]) } /// Returns a new zero-initialized fixed hash. + #[inline] pub const fn zero() -> $name { $name::repeat_byte(0u8) } /// Returns the size of this hash in bytes. + #[inline] pub const fn len_bytes() -> usize { $n_bytes } @@ -133,6 +136,7 @@ macro_rules! construct_fixed_hash { } /// Extracts a reference to the byte array containing the entire fixed hash. + #[inline] pub const fn as_fixed_bytes(&self) -> &[u8; $n_bytes] { &self.0 } @@ -144,6 +148,7 @@ macro_rules! construct_fixed_hash { } /// Returns the inner bytes array. + #[inline] pub const fn to_fixed_bytes(self) -> [u8; $n_bytes] { self.0 } diff --git a/uint/src/uint.rs b/uint/src/uint.rs index a87df5187..7dd1bca42 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -370,6 +370,7 @@ macro_rules! construct_uint { impl $name { /// Low 2 words (u128) + #[inline] pub const fn low_u128(&self) -> u128 { let &$name(ref arr) = self; ((arr[1] as u128) << 64) + arr[0] as u128 @@ -471,12 +472,14 @@ macro_rules! construct_uint { } /// Conversion to u32 + #[inline] pub const fn low_u32(&self) -> u32 { let &$name(ref arr) = self; arr[0] as u32 } /// Low word (u64) + #[inline] pub const fn low_u64(&self) -> u64 { let &$name(ref arr) = self; arr[0] @@ -556,6 +559,7 @@ macro_rules! construct_uint { /// # Panics /// /// Panics if `index` exceeds the bit width of the number. + #[inline] pub const fn bit(&self, index: usize) -> bool { let &$name(ref arr) = self; arr[index / 64] & (1 << (index % 64)) != 0 @@ -596,6 +600,7 @@ macro_rules! construct_uint { /// # Panics /// /// Panics if `index` exceeds the byte width of the number. + #[inline] pub const fn byte(&self, index: usize) -> u8 { let &$name(ref arr) = self; (arr[index / 8] >> (((index % 8)) * 8)) as u8 @@ -636,6 +641,7 @@ macro_rules! construct_uint { } /// Zero (additive identity) of this type. + #[inline] pub const fn zero() -> Self { Self([0; $n_words]) }