-
Notifications
You must be signed in to change notification settings - Fork 696
Feat: Add clippy stackslib, pass indexing_slicing #6188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Co-authored-by: Francesco <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
} | ||
|
||
for _i in 0..burn_sample.len() { | ||
for _sample in burn_sample.iter() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
for _sample in burn_sample.iter() { | |
for sample in burn_sample.iter() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should stay _sample
because its only ever used in the test_debug!()
macro, which is elided in non-test builds.
stackslib/src/chainstate/burn/mod.rs
Outdated
@@ -157,19 +157,12 @@ impl SortitionHash { | |||
} | |||
|
|||
/// Convert a SortitionHash into a (little-endian) uint256 | |||
#[allow(clippy::indexing_slicing)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about chunks_exact
, which cleans this up and avoids skipping clippy. Thanks, LLM.
Also, TIL about stacks_common::util::uint
. Was this just before there were better uint helpers in Rust std?
pub fn to_uint256(&self) -> Uint256 {
let tmp = self
.0
.chunks_exact(8)
.map(|chunk| u64::from_le_bytes(chunk.try_into().unwrap()))
.collect::<Vec<u64>>();
Uint256(tmp.try_into().unwrap())
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes -- though maybe even as_chunks
is the best thing here:
pub fn to_uint256(&self) -> Uint256 {
let (u64_chunks, []) = self.0.as_chunks::<8>() else {
panic!("SortitionHash was not evenly divisible by 8")
};
let tmp: Vec<u64> = u64_chunks.iter().map(|chunk| u64::from_le_bytes(*chunk)).collect();
Uint256(tmp.try_into().expect("SortitionHash should have 4 chunks of 8 bytes"))
}
This is brand new in stable rust (as in, today's release). It creates arrays from constant-sized chunks. It's like the chunks iterator, but it returns arrays instead of slices and it doesn't need to be an iterator, because the arrays are all guaranteed to be inline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm after the clippy warning is fixed!
This gets stackslib to pass the first lint listed in #6156
This does a couple of things in the refactor beyond just replacing
foo[..]
withfoo.get(..)?
in order to make the usage a little more idiomatic in rust:Some of the previous avoidance of iterators (in MARF in particular) had comments indicating that iterators were slower than the alternatives (for loops, and even manual for loops). I'm not sure if that used to be the case in older versions of rustc, but as of this PR, there was no measurable difference when measured using the MARF benchmarks (with optimizations turned on: I think in unoptimized builds, there is a performance difference).