Skip to content

Commit

Permalink
refactor(queries): fix clippy lints
Browse files Browse the repository at this point in the history
Signed-off-by: ⭐️NINIKA⭐️ <[email protected]>
  • Loading branch information
DCNick3 committed Dec 5, 2024
1 parent 6272c72 commit b91226d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 41 deletions.
5 changes: 1 addition & 4 deletions crates/iroha/tests/queries/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ fn find_accounts_with_asset() {
QueryExecutionFail::Find(FindError::MetadataKey(returned_key)),
))) = alice_no_key_err
else {
panic!(
"Got unexpected query error on missing metadata key {:?}",
alice_no_key_err
);
panic!("Got unexpected query error on missing metadata key {alice_no_key_err:?}",);
};
assert_eq!(returned_key, another_key);

Expand Down
2 changes: 1 addition & 1 deletion crates/iroha_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ mod json {
// for efficiency reasons iroha encodes query results in a columnar format,
// so we need to transpose the batch to get the format that is more natural for humans
let mut batches = vec![Vec::new(); accumulated_batch.len()];
for batch in accumulated_batch.into_iter() {
for batch in accumulated_batch {
// downcast to json and extract the actual array
// dynamic typing is just easier to use here than introducing a bunch of new types only for iroha_cli
let batch = serde_json::to_value(batch)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/iroha_core/src/sumeragi/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl Sumeragi {
})
.collect::<Vec<_>>()
.join("\n");
panic!("Genesis contains invalid transactions:\n{}", errors);
panic!("Genesis contains invalid transactions:\n{errors}");
}

// NOTE: By this time genesis block is executed and list of trusted peers is updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<T: HasProjection<PredicateMarker>> CompoundPredicate<T> {
// aliases for logical operations
/// Negate the predicate.
#[must_use]
#[expect(clippy::should_implement_trait)] // we do implement the `Not` trait, this is just a shorthand to avoid requiring importing it
pub fn not(self) -> Self {
!self
}
Expand Down
10 changes: 9 additions & 1 deletion crates/iroha_data_model/src/query/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,20 @@ pub trait HasPredicateAtom {
/// Trait implemented on all evaluable selectors for type `T`.
pub trait EvaluateSelector<T: 'static> {
/// Select the field from each of the elements in the input and type-erase the result. Cloning version.
#[expect(single_use_lifetimes)] // FP, this the suggested change is not allowed on stable
///
/// # Errors
///
/// Returns an error if the projection fails.
#[expect(single_use_lifetimes)] // FP, the suggested change is not allowed on stable
fn project_clone<'a>(
&self,
batch: impl Iterator<Item = &'a T>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail>;
/// Select the field from each of the elements in the input and type-erase the result.
///
/// # Errors
///
/// Returns an error if the projection fails.
fn project(
&self,
batch: impl Iterator<Item = T>,
Expand Down
48 changes: 24 additions & 24 deletions crates/iroha_data_model/src/query/dsl/type_descriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ macro_rules! type_descriptions {
}

impl EvaluateSelector<$ty> for $projection_name<SelectorMarker> {
#[expect(single_use_lifetimes)] // FP, this the suggested change is not allowed on stable
#[expect(single_use_lifetimes)] // FP, the suggested change is not allowed on stable
fn project_clone<'a>(&self, batch: impl Iterator<Item = &'a $ty>) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
$projection_name::Atom(_) => Ok(batch.cloned().collect::<Vec<_>>().into()),
Expand Down Expand Up @@ -364,7 +364,7 @@ mod fallible_selector {

trait Collector<TOut> {
fn collect(
&self,
self,
iter: impl Iterator<Item = TOut>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail>;
}
Expand All @@ -377,7 +377,7 @@ mod fallible_selector {
T::Projection: EvaluateSelector<T>,
{
fn collect(
&self,
self,
iter: impl Iterator<Item = &'a T>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
self.0.project_clone(iter)
Expand All @@ -392,7 +392,7 @@ mod fallible_selector {
T::Projection: EvaluateSelector<T>,
{
fn collect(
&self,
self,
iter: impl Iterator<Item = T>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
self.0.project(iter)
Expand Down Expand Up @@ -476,14 +476,14 @@ impl EvaluatePredicate<BlockHeader> for BlockHeaderProjection<PredicateMarker> {
}

impl EvaluateSelector<BlockHeader> for BlockHeaderProjection<SelectorMarker> {
#[expect(single_use_lifetimes)] // FP, this the suggested change is not allowed on stable
#[expect(single_use_lifetimes)] // FP, the suggested change is not allowed on stable
fn project_clone<'a>(
&self,
batch: impl Iterator<Item = &'a BlockHeader>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
BlockHeaderProjection::Atom(_) => Ok(batch.cloned().collect::<Vec<_>>().into()),
BlockHeaderProjection::Hash(hash) => hash.project(batch.map(|item| item.hash())),
BlockHeaderProjection::Atom(()) => Ok(batch.copied().collect::<Vec<_>>().into()),
BlockHeaderProjection::Hash(hash) => hash.project(batch.map(BlockHeader::hash)),
}
}

Expand All @@ -492,7 +492,7 @@ impl EvaluateSelector<BlockHeader> for BlockHeaderProjection<SelectorMarker> {
batch: impl Iterator<Item = BlockHeader>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
BlockHeaderProjection::Atom(_) => Ok(batch.collect::<Vec<_>>().into()),
BlockHeaderProjection::Atom(()) => Ok(batch.collect::<Vec<_>>().into()),
BlockHeaderProjection::Hash(hash) => hash.project(batch.map(|item| item.hash())),
}
}
Expand All @@ -508,16 +508,14 @@ impl EvaluatePredicate<SignedBlock> for SignedBlockProjection<PredicateMarker> {
}

impl EvaluateSelector<SignedBlock> for SignedBlockProjection<SelectorMarker> {
#[expect(single_use_lifetimes)] // FP, this the suggested change is not allowed on stable
#[expect(single_use_lifetimes)] // FP, the suggested change is not allowed on stable
fn project_clone<'a>(
&self,
batch: impl Iterator<Item = &'a SignedBlock>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
SignedBlockProjection::Atom(_) => Ok(batch.cloned().collect::<Vec<_>>().into()),
SignedBlockProjection::Header(header) => {
header.project(batch.map(|item| item.header()))
}
SignedBlockProjection::Atom(()) => Ok(batch.cloned().collect::<Vec<_>>().into()),
SignedBlockProjection::Header(header) => header.project(batch.map(SignedBlock::header)),
}
}

Expand All @@ -526,7 +524,7 @@ impl EvaluateSelector<SignedBlock> for SignedBlockProjection<SelectorMarker> {
batch: impl Iterator<Item = SignedBlock>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
SignedBlockProjection::Atom(_) => Ok(batch.collect::<Vec<_>>().into()),
SignedBlockProjection::Atom(()) => Ok(batch.collect::<Vec<_>>().into()),
SignedBlockProjection::Header(header) => {
header.project(batch.map(|item| item.header()))
}
Expand All @@ -540,23 +538,25 @@ impl EvaluatePredicate<SignedTransaction> for SignedTransactionProjection<Predic
SignedTransactionProjection::Atom(atom) => atom.applies(input),
SignedTransactionProjection::Hash(hash) => hash.applies(&input.hash()),
SignedTransactionProjection::Authority(authority) => {
authority.applies(&input.authority())
authority.applies(input.authority())
}
}
}
}

impl EvaluateSelector<SignedTransaction> for SignedTransactionProjection<SelectorMarker> {
#[expect(single_use_lifetimes)] // FP, this the suggested change is not allowed on stable
#[expect(single_use_lifetimes)] // FP, the suggested change is not allowed on stable
fn project_clone<'a>(
&self,
batch: impl Iterator<Item = &'a SignedTransaction>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
SignedTransactionProjection::Atom(_) => Ok(batch.cloned().collect::<Vec<_>>().into()),
SignedTransactionProjection::Hash(hash) => hash.project(batch.map(|item| item.hash())),
SignedTransactionProjection::Atom(()) => Ok(batch.cloned().collect::<Vec<_>>().into()),
SignedTransactionProjection::Hash(hash) => {
hash.project(batch.map(SignedTransaction::hash))
}
SignedTransactionProjection::Authority(authority) => {
authority.project_clone(batch.map(|item| item.authority()))
authority.project_clone(batch.map(SignedTransaction::authority))
}
}
}
Expand All @@ -566,7 +566,7 @@ impl EvaluateSelector<SignedTransaction> for SignedTransactionProjection<Selecto
batch: impl Iterator<Item = SignedTransaction>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
SignedTransactionProjection::Atom(_) => Ok(batch.collect::<Vec<_>>().into()),
SignedTransactionProjection::Atom(()) => Ok(batch.collect::<Vec<_>>().into()),
SignedTransactionProjection::Hash(hash) => hash.project(batch.map(|item| item.hash())),
SignedTransactionProjection::Authority(authority) => {
authority.project(batch.map(|item| item.authority().clone()))
Expand Down Expand Up @@ -598,7 +598,7 @@ impl EvaluateSelector<AssetValue> for AssetValueProjection<SelectorMarker> {
batch: impl Iterator<Item = &'a AssetValue>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
AssetValueProjection::Atom(_) => Ok(batch.cloned().collect::<Vec<_>>().into()),
AssetValueProjection::Atom(()) => Ok(batch.cloned().collect::<Vec<_>>().into()),
AssetValueProjection::Numeric(proj) => fallible_selector::map_clone(
batch,
|item| match item {
Expand Down Expand Up @@ -627,7 +627,7 @@ impl EvaluateSelector<AssetValue> for AssetValueProjection<SelectorMarker> {
batch: impl Iterator<Item = AssetValue>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
AssetValueProjection::Atom(_) => Ok(batch.collect::<Vec<_>>().into()),
AssetValueProjection::Atom(()) => Ok(batch.collect::<Vec<_>>().into()),
AssetValueProjection::Numeric(proj) => fallible_selector::map(
batch,
|item| match item {
Expand Down Expand Up @@ -716,7 +716,7 @@ impl EvaluateSelector<Metadata> for MetadataProjection<SelectorMarker> {
batch: impl Iterator<Item = &'a Metadata>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
MetadataProjection::Atom(_) => Ok(batch.cloned().collect::<Vec<_>>().into()),
MetadataProjection::Atom(()) => Ok(batch.cloned().collect::<Vec<_>>().into()),
MetadataProjection::Key(proj) => fallible_selector::map_clone(
batch,
|item| {
Expand All @@ -733,7 +733,7 @@ impl EvaluateSelector<Metadata> for MetadataProjection<SelectorMarker> {
batch: impl Iterator<Item = Metadata>,
) -> Result<QueryOutputBatchBox, QueryExecutionFail> {
match self {
MetadataProjection::Atom(_) => Ok(batch.collect::<Vec<_>>().into()),
MetadataProjection::Atom(()) => Ok(batch.collect::<Vec<_>>().into()),
MetadataProjection::Key(proj) => fallible_selector::map(
batch,
|item| {
Expand Down
45 changes: 37 additions & 8 deletions crates/iroha_data_model/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
#![allow(clippy::missing_inline_in_public_items)]

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::String, vec::Vec};
use alloc::{
boxed::Box,
format,
string::String,
vec::{self, Vec},
};
#[cfg(feature = "std")]
use std::vec;

use derive_more::Constructor;
use iroha_crypto::{PublicKey, SignatureOf};
Expand Down Expand Up @@ -363,13 +370,15 @@ impl QueryOutputBatchBoxTuple {
///
/// Panics if the types or lengths of the two batche tuples do not match
pub fn extend(&mut self, other: Self) {
if self.tuple.len() != other.tuple.len() {
panic!("Cannot extend QueryOutputBatchBoxTuple with different number of elements");
}
assert_eq!(
self.tuple.len(),
other.tuple.len(),
"Cannot extend QueryOutputBatchBoxTuple with different number of elements"
);

self.tuple
.iter_mut()
.zip(other.tuple.into_iter())
.zip(other)
.for_each(|(self_batch, other_batch)| self_batch.extend(other_batch));
}

Expand All @@ -379,14 +388,34 @@ impl QueryOutputBatchBoxTuple {
self.tuple[0].len()
}

/// Returns `true` if this batch tuple is empty
pub fn is_empty(&self) -> bool {
self.tuple[0].len() == 0
}

/// Returns an iterator over the batches in this tuple
pub fn iter(&self) -> impl Iterator<Item = &QueryOutputBatchBox> {
self.tuple.iter()
}
}

impl IntoIterator for QueryOutputBatchBoxTuple {
type Item = QueryOutputBatchBox;
type IntoIter = QueryOutputBatchBoxIntoIter;

fn into_iter(self) -> Self::IntoIter {
QueryOutputBatchBoxIntoIter(self.tuple.into_iter())
}
}

/// An iterator over the batches in a [`QueryOutputBatchBoxTuple`]
pub struct QueryOutputBatchBoxIntoIter(vec::IntoIter<QueryOutputBatchBox>);

impl Iterator for QueryOutputBatchBoxIntoIter {
type Item = QueryOutputBatchBox;

/// Consumes this batch tuple and returns an iterator over the batches
pub fn into_iter(self) -> impl Iterator<Item = QueryOutputBatchBox> {
self.tuple.into_iter()
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/iroha_smart_contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ mod tests {
assert_eq!(remaining_items, 0);
assert!(
next_cursor.is_none(),
"Expected no cursor, but got {:?}",
next_cursor
"Expected no cursor, but got {next_cursor:?}",
);
}
}

0 comments on commit b91226d

Please sign in to comment.