Skip to content

Commit

Permalink
Use impl Trait syntax for args in the Storage trait instead of ge…
Browse files Browse the repository at this point in the history
…neric parameter
  • Loading branch information
khvzak committed Jan 16, 2024
1 parent ec2daa7 commit 41998b2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 42 deletions.
25 changes: 11 additions & 14 deletions casper-server/src/storage/backends/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,10 @@ impl Storage for MemoryBackend {
self.get_responses([key]).await.remove(0)
}

async fn get_responses<KI>(
async fn get_responses(
&self,
keys: KI,
) -> Vec<Result<Option<Response<Self::Body>>, Self::Error>>
where
KI: IntoIterator<Item = Key>,
{
keys: impl IntoIterator<Item = Key>,
) -> Vec<Result<Option<Response<Self::Body>>, Self::Error>> {
let mut memory = self.inner.lock().await;
let mut responses = Vec::new();
for key in keys {
Expand All @@ -193,10 +190,10 @@ impl Storage for MemoryBackend {
self.delete_responses_multi([key]).await.remove(0)
}

async fn delete_responses_multi<KI>(&self, keys: KI) -> Vec<Result<(), Self::Error>>
where
KI: IntoIterator<Item = ItemKey>,
{
async fn delete_responses_multi(
&self,
keys: impl IntoIterator<Item = ItemKey>,
) -> Vec<Result<(), Self::Error>> {
let mut memory = self.inner.lock().await;
let mut results = Vec::new();
for key in keys {
Expand All @@ -217,10 +214,10 @@ impl Storage for MemoryBackend {
self.store_responses([item]).await.remove(0)
}

async fn store_responses<'a, I>(&self, items: I) -> Vec<Result<(), Self::Error>>
where
I: IntoIterator<Item = Item<'a>>,
{
async fn store_responses(
&self,
items: impl IntoIterator<Item = Item<'_>>,
) -> Vec<Result<(), Self::Error>> {
let mut memory = self.inner.lock().await;
let mut results = Vec::new();
for item in items {
Expand Down
25 changes: 11 additions & 14 deletions casper-server/src/storage/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,32 @@ impl Storage for Backend {
}

#[inline]
async fn get_responses<KI>(
async fn get_responses(
&self,
keys: KI,
) -> Vec<Result<Option<Response<Self::Body>>, Self::Error>>
where
KI: IntoIterator<Item = Key>,
{
keys: impl IntoIterator<Item = Key>,
) -> Vec<Result<Option<Response<Self::Body>>, Self::Error>> {
match self {
Backend::Memory(inner) => inner.get_responses(keys).await,
Backend::Redis(inner) => inner.get_responses(keys).await,
}
}

#[inline]
async fn delete_responses_multi<KI>(&self, keys: KI) -> Vec<Result<(), Self::Error>>
where
KI: IntoIterator<Item = ItemKey>,
{
async fn delete_responses_multi(
&self,
keys: impl IntoIterator<Item = ItemKey>,
) -> Vec<Result<(), Self::Error>> {
match self {
Backend::Memory(inner) => inner.delete_responses_multi(keys).await,
Backend::Redis(inner) => inner.delete_responses_multi(keys).await,
}
}

#[inline]
async fn store_responses<'a, I>(&self, items: I) -> Vec<Result<(), Self::Error>>
where
I: IntoIterator<Item = Item<'a>>,
{
async fn store_responses(
&self,
items: impl IntoIterator<Item = Item<'_>>,
) -> Vec<Result<(), Self::Error>> {
match self {
Backend::Memory(inner) => inner.store_responses(items).await,
Backend::Redis(inner) => inner.store_responses(items).await,
Expand Down
25 changes: 11 additions & 14 deletions casper-server/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,32 @@ pub trait Storage {
// Provided implementation
//

async fn get_responses<KI>(
async fn get_responses(
&self,
keys: KI,
) -> Vec<Result<Option<Response<Self::Body>>, Self::Error>>
where
KI: IntoIterator<Item = Key>,
{
keys: impl IntoIterator<Item = Key>,
) -> Vec<Result<Option<Response<Self::Body>>, Self::Error>> {
// Create list of pending futures to poll them in parallel
stream::iter(keys.into_iter().map(|key| self.get_response(key)))
.buffered(Self::MAX_CONCURRENCY)
.collect()
.await
}

async fn delete_responses_multi<KI>(&self, keys: KI) -> Vec<Result<(), Self::Error>>
where
KI: IntoIterator<Item = ItemKey>,
{
async fn delete_responses_multi(
&self,
keys: impl IntoIterator<Item = ItemKey>,
) -> Vec<Result<(), Self::Error>> {
// Create list of pending futures to poll them in parallel
stream::iter(keys.into_iter().map(|key| self.delete_responses(key)))
.buffered(Self::MAX_CONCURRENCY)
.collect()
.await
}

async fn store_responses<'a, I>(&self, items: I) -> Vec<Result<(), Self::Error>>
where
I: IntoIterator<Item = Item<'a>>,
{
async fn store_responses(
&self,
items: impl IntoIterator<Item = Item<'_>>,
) -> Vec<Result<(), Self::Error>> {
// Create list of pending futures to poll them in parallel
stream::iter(items.into_iter().map(|it| self.store_response(it)))
.buffered(Self::MAX_CONCURRENCY)
Expand Down

0 comments on commit 41998b2

Please sign in to comment.