chore(reth-provider): use Vec::with_capacity (#10912)

This commit is contained in:
nk_ysg
2024-09-17 17:37:51 +08:00
committed by GitHub
parent 0fd8132e90
commit acdb2050a7
5 changed files with 22 additions and 23 deletions

View File

@ -487,7 +487,7 @@ mod tests {
fn compact_bytes() {
let arr = [1, 2, 3, 4, 5];
let list = Bytes::copy_from_slice(&arr);
let mut buf = vec![];
let mut buf = Vec::with_capacity(list.len() + 1);
assert_eq!(list.to_compact(&mut buf), list.len());
// Add some noise data.
@ -512,7 +512,7 @@ mod tests {
#[test]
fn compact_b256() {
let mut buf = vec![];
let mut buf = Vec::with_capacity(32 + 1);
assert_eq!(B256::ZERO.to_compact(&mut buf), 32);
assert_eq!(buf, vec![0; 32]);
@ -545,7 +545,7 @@ mod tests {
#[test]
fn compact_option() {
let opt = Some(B256::ZERO);
let mut buf = vec![];
let mut buf = Vec::with_capacity(1 + 32);
assert_eq!(None::<B256>.to_compact(&mut buf), 0);
assert_eq!(opt.to_compact(&mut buf), 1);
@ -556,7 +556,7 @@ mod tests {
// If `None`, it returns the slice at the same cursor position.
assert_eq!(Option::<B256>::from_compact(&buf, 0), (None, buf.as_slice()));
let mut buf = vec![];
let mut buf = Vec::with_capacity(32);
assert_eq!(opt.specialized_to_compact(&mut buf), 1);
assert_eq!(buf.len(), 32);
assert_eq!(Option::<B256>::specialized_from_compact(&buf, 1), (opt, vec![].as_slice()));
@ -605,7 +605,7 @@ mod tests {
assert_eq!(buf, vec![2u8]);
assert_eq!(u64::from_compact(&buf, 1), (2u64, vec![].as_slice()));
let mut buf = vec![];
let mut buf = Vec::with_capacity(8);
assert_eq!(0xffffffffffffffffu64.to_compact(&mut buf), 8);
assert_eq!(&buf, &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
@ -683,18 +683,16 @@ mod tests {
#[test]
fn compact_test_struct() {
let test = TestStruct::default();
let mut buf = vec![];
assert_eq!(
test.to_compact(&mut buf),
2 + // TestStructFlags
const EXPECTED_SIZE: usize = 2 + // TestStructFlags
1 +
1 +
// 0 + 0 + 0 +
32 +
1 + 2 +
1 +
1 + 20 * 2
);
1 + 20 * 2;
let mut buf = Vec::with_capacity(EXPECTED_SIZE);
assert_eq!(test.to_compact(&mut buf), EXPECTED_SIZE);
assert_eq!(
TestStruct::from_compact(&buf, buf.len()),