Skip to content

Commit

Permalink
Merge pull request #37 from moka-rs/remove-error-bound
Browse files Browse the repository at this point in the history
Remove the `Error` bound from `get_or_try_insert_with` method
  • Loading branch information
tatsuya6502 authored Sep 7, 2021
2 parents 0ef445a + 28e001b commit 90969d3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Changed

- Change `get_or_try_insert_with` to return a concrete error type rather
than a trait object. ([#23][gh-pull-0023])
than a trait object. ([#23][gh-pull-0023], [#37][gh-pull-0037])


## Version 0.5.2
Expand Down Expand Up @@ -112,6 +112,7 @@

[caffeine-git]: https://github.com/ben-manes/caffeine

[gh-pull-0037]: https://github.com/moka-rs/moka/pull/37/
[gh-pull-0033]: https://github.com/moka-rs/moka/pull/33/
[gh-issue-0031]: https://github.com/moka-rs/moka/issues/31/
[gh-pull-0030]: https://github.com/moka-rs/moka/pull/30/
Expand Down
10 changes: 5 additions & 5 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::{
any::TypeId,
borrow::Borrow,
collections::hash_map::RandomState,
error::Error,
future::Future,
hash::{BuildHasher, Hash},
sync::Arc,
Expand Down Expand Up @@ -439,7 +438,7 @@ where
pub async fn get_or_try_insert_with<F, E>(&self, key: K, init: F) -> Result<V, Arc<E>>
where
F: Future<Output = Result<V, E>> + Send + 'static,
E: Error + Send + Sync + 'static,
E: Send + Sync + 'static,
{
let hash = self.base.hash(&key);
let key = Arc::new(key);
Expand Down Expand Up @@ -632,7 +631,7 @@ where
) -> Result<V, Arc<E>>
where
F: Future<Output = Result<V, E>>,
E: Error + Send + Sync + 'static,
E: Send + Sync + 'static,
{
if let Some(v) = self.base.get_with_hash(&key, hash) {
return Ok(v);
Expand Down Expand Up @@ -1166,8 +1165,9 @@ mod tests {
async fn get_or_try_insert_with() {
use std::sync::Arc;

#[derive(thiserror::Error, Debug)]
#[error("{}", _0)]
// Note that MyError does not implement std::error::Error trait
// like anyhow::Error.
#[derive(Debug)]
pub struct MyError(String);

type MyResult<T> = Result<T, Arc<MyError>>;
Expand Down
3 changes: 1 addition & 2 deletions src/future/value_initializer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use async_lock::RwLock;
use std::{
any::{Any, TypeId},
error::Error,
future::Future,
hash::{BuildHasher, Hash},
sync::Arc,
Expand Down Expand Up @@ -68,7 +67,7 @@ where
pub(crate) async fn try_init_or_read<F, E>(&self, key: Arc<K>, init: F) -> InitResult<V, E>
where
F: Future<Output = Result<V, E>>,
E: Error + Send + Sync + 'static,
E: Send + Sync + 'static,
{
use InitResult::*;

Expand Down
10 changes: 5 additions & 5 deletions src/sync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{
any::TypeId,
borrow::Borrow,
collections::hash_map::RandomState,
error::Error,
hash::{BuildHasher, Hash},
sync::Arc,
time::Duration,
Expand Down Expand Up @@ -423,7 +422,7 @@ where
pub fn get_or_try_insert_with<F, E>(&self, key: K, init: F) -> Result<V, Arc<E>>
where
F: FnOnce() -> Result<V, E>,
E: Error + Send + Sync + 'static,
E: Send + Sync + 'static,
{
let hash = self.base.hash(&key);
let key = Arc::new(key);
Expand All @@ -438,7 +437,7 @@ where
) -> Result<V, Arc<E>>
where
F: FnOnce() -> Result<V, E>,
E: Error + Send + Sync + 'static,
E: Send + Sync + 'static,
{
if let Some(v) = self.get_with_hash(&key, hash) {
return Ok(v);
Expand Down Expand Up @@ -1009,8 +1008,9 @@ mod tests {
thread::{sleep, spawn},
};

#[derive(thiserror::Error, Debug)]
#[error("{}", _0)]
// Note that MyError does not implement std::error::Error trait
// like anyhow::Error.
#[derive(Debug)]
pub struct MyError(String);

type MyResult<T> = Result<T, Arc<MyError>>;
Expand Down
3 changes: 1 addition & 2 deletions src/sync/value_initializer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use parking_lot::RwLock;
use std::{
any::{Any, TypeId},
error::Error,
hash::{BuildHasher, Hash},
sync::Arc,
};
Expand Down Expand Up @@ -63,7 +62,7 @@ where
pub(crate) fn try_init_or_read<F, E>(&self, key: Arc<K>, init: F) -> InitResult<V, E>
where
F: FnOnce() -> Result<V, E>,
E: Error + Send + Sync + 'static,
E: Send + Sync + 'static,
{
use InitResult::*;

Expand Down

0 comments on commit 90969d3

Please sign in to comment.