Skip to content

Commit

Permalink
code cleanup (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on authored Jul 7, 2023
1 parent 5b4aee4 commit 2ed83ff
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 64 deletions.
6 changes: 3 additions & 3 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn encode_decode_vec<T: TryFrom<u8> + Codec>(c: &mut Criterion) where T::Error:
});
}

core::mem::drop(g);
drop(g);
let mut g = c.benchmark_group("vec_decode");
for vec_size in [1, 2, 5, 32, 1024, 2048, 16384] {
g.bench_with_input(&format!("{}/{}", type_name::<T>(), vec_size), &vec_size, |b, &vec_size| {
Expand All @@ -146,7 +146,7 @@ fn encode_decode_vec<T: TryFrom<u8> + Codec>(c: &mut Criterion) where T::Error:
});
}

core::mem::drop(g);
drop(g);
let mut g = c.benchmark_group("vec_decode_no_limit");
for vec_size in [16384, 131072] {
g.bench_with_input(&format!("vec_decode_no_limit_{}/{}", type_name::<T>(), vec_size), &vec_size, |b, &vec_size| {
Expand Down Expand Up @@ -191,7 +191,7 @@ fn encode_decode_complex_type(c: &mut Criterion) {
});
}

core::mem::drop(g);
drop(g);
let mut g = c.benchmark_group("vec_decode_complex_type");
for vec_size in [1, 2, 5, 32, 1024, 2048, 16384] {
let complex_types = complex_types.clone();
Expand Down
2 changes: 1 addition & 1 deletion derive/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::utils;

type FieldsList = Punctuated<Field, Comma>;

// Encode a signle field by using using_encoded, must not have skip attribute
// Encode a single field by using using_encoded, must not have skip attribute
fn encode_single_field(
field: &Field,
field_name: TokenStream,
Expand Down
4 changes: 2 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ fn wrap_with_dummy_const(
/// The variable is encoded with one byte for the variant and then the variant struct encoding.
/// The variant number is:
/// * if variant has attribute: `#[codec(index = "$n")]` then n
/// * else if variant has discrimant (like 3 in `enum T { A = 3 }`) then the discrimant.
/// * else if variant has discriminant (like 3 in `enum T { A = 3 }`) then the discriminant.
/// * else its position in the variant set, excluding skipped variants, but including variant with
/// discrimant or attribute. Warning this position does collision with discrimant or attribute
/// discriminant or attribute. Warning this position does collision with discriminant or attribute
/// index.
///
/// variant attributes:
Expand Down
22 changes: 11 additions & 11 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,15 +890,15 @@ impl<T: Encode, const N: usize> Encode for [T; N] {
}

const fn calculate_array_bytesize<T, const N: usize>() -> usize {
struct AssertNotOverflow<T, const N: usize>(core::marker::PhantomData<T>);
struct AssertNotOverflow<T, const N: usize>(PhantomData<T>);

impl<T, const N: usize> AssertNotOverflow<T, N> {
const OK: () = assert!(core::mem::size_of::<T>().checked_mul(N).is_some(), "array size overflow");
const OK: () = assert!(mem::size_of::<T>().checked_mul(N).is_some(), "array size overflow");
}

#[allow(clippy::let_unit_value)]
let () = AssertNotOverflow::<T, N>::OK;
core::mem::size_of::<T>() * N
mem::size_of::<T>() * N
}

impl<T: Decode, const N: usize> Decode for [T; N] {
Expand Down Expand Up @@ -978,7 +978,7 @@ impl<T: Decode, const N: usize> Decode for [T; N] {

impl<'a, T, const N: usize> Drop for State<'a, T, N> {
fn drop(&mut self) {
if !core::mem::needs_drop::<T>() {
if !mem::needs_drop::<T>() {
// If the types don't actually need to be dropped then don't even
// try to run the loop below.
//
Expand Down Expand Up @@ -1011,7 +1011,7 @@ impl<T: Decode, const N: usize> Decode for [T; N] {
}

// We've successfully read everything, so disarm the `Drop` impl.
core::mem::forget(state);
mem::forget(state);

// SAFETY: We've initialized the whole slice so calling this is safe.
unsafe {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ impl Decode for String {

/// Writes the compact encoding of `len` do `dest`.
pub(crate) fn compact_encode_len_to<W: Output + ?Sized>(dest: &mut W, len: usize) -> Result<(), Error> {
if len > u32::max_value() as usize {
if len > u32::MAX as usize {
return Err("Attempted to serialize a collection with too many elements.".into());
}

Expand All @@ -1094,7 +1094,7 @@ pub(crate) fn compact_encode_len_to<W: Output + ?Sized>(dest: &mut W, len: usize

impl<T: Encode> Encode for [T] {
fn size_hint(&self) -> usize {
mem::size_of::<u32>() + core::mem::size_of_val(self)
mem::size_of::<u32>() + mem::size_of_val(self)
}

fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
Expand Down Expand Up @@ -1853,8 +1853,8 @@ mod tests {

#[test]
fn shared_references_implement_encode() {
std::sync::Arc::new(10u32).encode();
std::rc::Rc::new(10u32).encode();
Arc::new(10u32).encode();
Rc::new(10u32).encode();
}

#[test]
Expand Down Expand Up @@ -1973,7 +1973,7 @@ mod tests {

#[test]
fn u64_max() {
let num_secs = u64::max_value();
let num_secs = u64::MAX;
let num_nanos = 0;
let duration = Duration::new(num_secs, num_nanos);
let expected = (num_secs, num_nanos).encode();
Expand All @@ -1984,7 +1984,7 @@ mod tests {

#[test]
fn decoding_does_not_overflow() {
let num_secs = u64::max_value();
let num_secs = u64::MAX;
let num_nanos = A_BILLION;

// `num_nanos`' carry should make `num_secs` overflow if we were to call `Duration::new()`.
Expand Down
80 changes: 40 additions & 40 deletions src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl Decode for Compact<u32> {
},
2 => {
let x = u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
if x > 0b0011_1111_1111_1111 && x <= u32::max_value() >> 2 {
if x > 0b0011_1111_1111_1111 && x <= u32::MAX >> 2 {
x
} else {
return Err(U32_OUT_OF_RANGE.into());
Expand All @@ -497,7 +497,7 @@ impl Decode for Compact<u32> {
if prefix >> 2 == 0 {
// just 4 bytes. ok.
let x = u32::decode(input)?;
if x > u32::max_value() >> 2 {
if x > u32::MAX >> 2 {
x
} else {
return Err(U32_OUT_OF_RANGE.into());
Expand Down Expand Up @@ -527,7 +527,7 @@ impl Decode for Compact<u64> {
},
2 => {
let x = u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
if x > 0b0011_1111_1111_1111 && x <= u32::max_value() >> 2 {
if x > 0b0011_1111_1111_1111 && x <= u32::MAX >> 2 {
u64::from(x)
} else {
return Err(U64_OUT_OF_RANGE.into());
Expand All @@ -536,15 +536,15 @@ impl Decode for Compact<u64> {
3 => match (prefix >> 2) + 4 {
4 => {
let x = u32::decode(input)?;
if x > u32::max_value() >> 2 {
if x > u32::MAX >> 2 {
u64::from(x)
} else {
return Err(U64_OUT_OF_RANGE.into());
}
},
8 => {
let x = u64::decode(input)?;
if x > u64::max_value() >> 8 {
if x > u64::MAX >> 8 {
x
} else {
return Err(U64_OUT_OF_RANGE.into());
Expand All @@ -556,7 +556,7 @@ impl Decode for Compact<u64> {
for i in 0..bytes_needed {
res |= u64::from(input.read_byte()?) << (i * 8);
}
if res > u64::max_value() >> ((8 - bytes_needed + 1) * 8) {
if res > u64::MAX >> ((8 - bytes_needed + 1) * 8) {
res
} else {
return Err(U64_OUT_OF_RANGE.into());
Expand All @@ -583,7 +583,7 @@ impl Decode for Compact<u128> {
},
2 => {
let x = u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
if x > 0b0011_1111_1111_1111 && x <= u32::max_value() >> 2 {
if x > 0b0011_1111_1111_1111 && x <= u32::MAX >> 2 {
u128::from(x)
} else {
return Err(U128_OUT_OF_RANGE.into());
Expand All @@ -592,23 +592,23 @@ impl Decode for Compact<u128> {
3 => match (prefix >> 2) + 4 {
4 => {
let x = u32::decode(input)?;
if x > u32::max_value() >> 2 {
if x > u32::MAX >> 2 {
u128::from(x)
} else {
return Err(U128_OUT_OF_RANGE.into());
}
},
8 => {
let x = u64::decode(input)?;
if x > u64::max_value() >> 8 {
if x > u64::MAX >> 8 {
u128::from(x)
} else {
return Err(U128_OUT_OF_RANGE.into());
}
},
16 => {
let x = u128::decode(input)?;
if x > u128::max_value() >> 8 {
if x > u128::MAX >> 8 {
x
} else {
return Err(U128_OUT_OF_RANGE.into());
Expand All @@ -620,7 +620,7 @@ impl Decode for Compact<u128> {
for i in 0..bytes_needed {
res |= u128::from(input.read_byte()?) << (i * 8);
}
if res > u128::max_value() >> ((16 - bytes_needed + 1) * 8) {
if res > u128::MAX >> ((16 - bytes_needed + 1) * 8) {
res
} else {
return Err(U128_OUT_OF_RANGE.into());
Expand All @@ -644,7 +644,7 @@ mod tests {
(1073741824, 5), ((1 << 32) - 1, 5),
(1 << 32, 6), (1 << 40, 7), (1 << 48, 8), ((1 << 56) - 1, 8), (1 << 56, 9), ((1 << 64) - 1, 9),
(1 << 64, 10), (1 << 72, 11), (1 << 80, 12), (1 << 88, 13), (1 << 96, 14), (1 << 104, 15),
(1 << 112, 16), ((1 << 120) - 1, 16), (1 << 120, 17), (u128::max_value(), 17)
(1 << 112, 16), ((1 << 120) - 1, 16), (1 << 120, 17), (u128::MAX, 17)
];
for &(n, l) in &tests {
let encoded = Compact(n as u128).encode();
Expand All @@ -660,7 +660,7 @@ mod tests {
(0u64, 1usize), (63, 1), (64, 2), (16383, 2),
(16384, 4), (1073741823, 4),
(1073741824, 5), ((1 << 32) - 1, 5),
(1 << 32, 6), (1 << 40, 7), (1 << 48, 8), ((1 << 56) - 1, 8), (1 << 56, 9), (u64::max_value(), 9)
(1 << 32, 6), (1 << 40, 7), (1 << 48, 8), ((1 << 56) - 1, 8), (1 << 56, 9), (u64::MAX, 9)
];
for &(n, l) in &tests {
let encoded = Compact(n as u64).encode();
Expand All @@ -672,7 +672,7 @@ mod tests {

#[test]
fn compact_32_encoding_works() {
let tests = [(0u32, 1usize), (63, 1), (64, 2), (16383, 2), (16384, 4), (1073741823, 4), (1073741824, 5), (u32::max_value(), 5)];
let tests = [(0u32, 1usize), (63, 1), (64, 2), (16383, 2), (16384, 4), (1073741823, 4), (1073741824, 5), (u32::MAX, 5)];
for &(n, l) in &tests {
let encoded = Compact(n as u32).encode();
assert_eq!(encoded.len(), l);
Expand Down Expand Up @@ -725,7 +725,7 @@ mod tests {
(1 << 48, "0f 00 00 00 00 00 00 01"),
((1 << 56) - 1, "0f ff ff ff ff ff ff ff"),
(1 << 56, "13 00 00 00 00 00 00 00 01"),
(u64::max_value(), "13 ff ff ff ff ff ff ff ff")
(u64::MAX, "13 ff ff ff ff ff ff ff ff")
];
for &(n, s) in &tests {
// Verify u64 encoding
Expand All @@ -734,19 +734,19 @@ mod tests {
assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n);

// Verify encodings for lower-size uints are compatible with u64 encoding
if n <= u32::max_value() as u64 {
if n <= u32::MAX as u64 {
assert_eq!(<Compact<u32>>::decode(&mut &encoded[..]).unwrap().0, n as u32);
let encoded = Compact(n as u32).encode();
assert_eq!(hexify(&encoded), s);
assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
}
if n <= u16::max_value() as u64 {
if n <= u16::MAX as u64 {
assert_eq!(<Compact<u16>>::decode(&mut &encoded[..]).unwrap().0, n as u16);
let encoded = Compact(n as u16).encode();
assert_eq!(hexify(&encoded), s);
assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
}
if n <= u8::max_value() as u64 {
if n <= u8::MAX as u64 {
assert_eq!(<Compact<u8>>::decode(&mut &encoded[..]).unwrap().0, n as u8);
let encoded = Compact(n as u8).encode();
assert_eq!(hexify(&encoded), s);
Expand Down Expand Up @@ -800,17 +800,17 @@ mod tests {

#[test]
fn compact_using_encoded_arrayvec_size() {
Compact(std::u8::MAX).using_encoded(|_| {});
Compact(std::u16::MAX).using_encoded(|_| {});
Compact(std::u32::MAX).using_encoded(|_| {});
Compact(std::u64::MAX).using_encoded(|_| {});
Compact(std::u128::MAX).using_encoded(|_| {});
Compact(u8::MAX).using_encoded(|_| {});
Compact(u16::MAX).using_encoded(|_| {});
Compact(u32::MAX).using_encoded(|_| {});
Compact(u64::MAX).using_encoded(|_| {});
Compact(u128::MAX).using_encoded(|_| {});

CompactRef(&std::u8::MAX).using_encoded(|_| {});
CompactRef(&std::u16::MAX).using_encoded(|_| {});
CompactRef(&std::u32::MAX).using_encoded(|_| {});
CompactRef(&std::u64::MAX).using_encoded(|_| {});
CompactRef(&std::u128::MAX).using_encoded(|_| {});
CompactRef(&u8::MAX).using_encoded(|_| {});
CompactRef(&u16::MAX).using_encoded(|_| {});
CompactRef(&u32::MAX).using_encoded(|_| {});
CompactRef(&u64::MAX).using_encoded(|_| {});
CompactRef(&u128::MAX).using_encoded(|_| {});
}

#[test]
Expand All @@ -833,7 +833,7 @@ mod tests {
)*
};
( $m:expr, $ty:ty, $typ1:ty, $ty2:ty, $ty2_err:expr) => {
let enc = ((<$ty>::max_value() >> 2) as $typ1 << 2) | $m;
let enc = ((<$ty>::MAX >> 2) as $typ1 << 2) | $m;
assert_eq!(Compact::<$ty2>::decode(&mut &enc.to_le_bytes()[..]),
Err($ty2_err.into()));
};
Expand All @@ -859,7 +859,7 @@ mod tests {
let mut dest = Vec::new();
dest.push(0b11 + (($s - 4) << 2) as u8);
for _ in 0..($s - 1) {
dest.push(u8::max_value());
dest.push(u8::MAX);
}
dest.push(0);
assert_eq!(Compact::<$ty2>::decode(&mut &dest[..]),
Expand All @@ -870,12 +870,12 @@ mod tests {
#[test]
fn compact_u64_test() {
for a in [
u64::max_value(),
u64::max_value() - 1,
u64::max_value() << 8,
(u64::max_value() << 8) - 1,
u64::max_value() << 16,
(u64::max_value() << 16) - 1,
u64::MAX,
u64::MAX - 1,
u64::MAX << 8,
(u64::MAX << 8) - 1,
u64::MAX << 16,
(u64::MAX << 16) - 1,
].iter() {
let e = Compact::<u64>::encode(&Compact(*a));
let d = Compact::<u64>::decode(&mut &e[..]).unwrap().0;
Expand All @@ -886,10 +886,10 @@ mod tests {
#[test]
fn compact_u128_test() {
for a in [
u64::max_value() as u128,
(u64::max_value() - 10) as u128,
u128::max_value(),
u128::max_value() - 10,
u64::MAX as u128,
(u64::MAX - 10) as u128,
u128::MAX,
u128::MAX - 10,
].iter() {
let e = Compact::<u128>::encode(&Compact(*a));
let d = Compact::<u128>::decode(&mut &e[..]).unwrap().0;
Expand Down
2 changes: 1 addition & 1 deletion src/decode_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{Decode, Error};
/// The error message returned when `decode_all` fails.
pub(crate) const DECODE_ALL_ERR_MSG: &str = "Input buffer has still data left after decoding!";

/// Extension trait to [`Decode`] that ensures that the given input data is consumed completly while
/// Extension trait to [`Decode`] that ensures that the given input data is consumed completely while
/// decoding.
pub trait DecodeAll: Sized {
/// Decode `Self` and consume all of the given input data.
Expand Down
2 changes: 1 addition & 1 deletion src/keyedvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Serialiser and prepender.
//! Serializer and prepender.

use core::iter::Extend;

Expand Down
Loading

0 comments on commit 2ed83ff

Please sign in to comment.