Skip to content

Commit

Permalink
fix doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Jul 7, 2022
1 parent 5d85283 commit 640deaf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 54 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ serde = {version = "1", optional = true}
serde_json = {version = "1", optional = true}
twox-hash = "1.6"
wg = "0.2"
thiserror = "1"

[dev-dependencies]
serde = {version = "1", features = ["serde_derive"]}
Expand Down
12 changes: 9 additions & 3 deletions src/cache/axync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ where
/// For example, if you use `tokio`, then pass `tokio::spawn` as spawner parameter.
///
/// ```no_run
/// AsyncCacheBuilder::new_with_key_builder(100, 10, TransparentKeyBuilder::default())
/// use stretto::{AsyncCacheBuilder, TransparentKeyBuilder};
///
/// AsyncCacheBuilder::<u64, u64>::new(100, 10)
/// .finalize(tokio::spawn)
/// .unwrap();
/// ```
Expand Down Expand Up @@ -398,7 +400,9 @@ impl<K: Hash + Eq, V: Send + Sync + 'static> AsyncCache<K, V> {
/// For example, if you use `tokio`, then pass `tokio::spawn` as spawner parameter.
///
/// ```no_run
/// AsyncCache::new(100, 10, tokio::spawn).unwrap();
/// use stretto::AsyncCache;
///
/// AsyncCache::<u64, u64>::new(100, 10, tokio::spawn).unwrap();
/// ```
#[inline]
pub fn new<SP, R>(num_counters: usize, max_cost: i64, spawner: SP) -> Result<Self, CacheError>
Expand Down Expand Up @@ -434,7 +438,9 @@ impl<K: Hash + Eq, V: Send + Sync + 'static, KH: KeyBuilder<K>> AsyncCache<K, V,
/// For example, if you use `tokio`, then pass `tokio::spawn` as spawner parameter.
///
/// ```no_run
/// AsyncCache::new_with_key_builder(100, 10, TransparentKeyBuilder::default(), tokio::spawn).unwrap();
/// use stretto::{AsyncCache, TransparentKeyBuilder};
///
/// AsyncCache::<u64, u64, TransparentKeyBuilder<_>>::new_with_key_builder(100, 10, TransparentKeyBuilder::<u64>::default(), tokio::spawn).unwrap();
/// ```
#[inline]
pub fn new_with_key_builder<SP, R>(
Expand Down
56 changes: 14 additions & 42 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,83 +1,55 @@
use std::fmt::{Debug, Display, Formatter};

/// CacheError contains the error of this crate
#[derive(thiserror::Error, Debug)]
pub enum CacheError {
/// Count Min sketch with wrong width.
#[error("invalid number of samples: {0}")]
InvalidCountMinWidth(u64),

/// Invalid Samples value for TinyLFU.
#[error("invalid count main sketch width: {0}")]
InvalidSamples(usize),

/// Invalid false positive ratio for TinyLFU.
#[error("invalid false positive ratio: {0}, which should be in range (0.0, 1.0)")]
InvalidFalsePositiveRatio(f64),

/// Invalid number of counters for the Cache.
#[error("num_counters can't be zero")]
InvalidNumCounters,

/// Invalid max cost for the Cache.
#[error("max_cost can't be zero")]
InvalidMaxCost,

/// Invalid insert buffer size for the Cache.
#[error("buffer_size can't be zero")]
InvalidBufferSize,

/// Error when send msg between threads.
#[error("fail to send msg to channel: {0}")]
SendError(String),

/// Error when receive msg between threads.
#[error("fail to receive msg from channel: {0}")]
RecvError(String),

/// Error when updating entries
#[error("update error: {0}")]
UpdateError(String),

/// Error when inserting entries
#[error("insert error: {0}")]
InsertError(String),

/// Error when removing entries
#[error("remove error: {0}")]
RemoveError(String),

/// Error when cleaning up
#[error("cleanup error: {0}")]
CleanupError(String),

/// Channel send error
#[error("channel error: {0}")]
ChannelError(String),
}

impl CacheError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
CacheError::InvalidSamples(v) => write!(f, "invalid number of samples: {}", *v),
CacheError::InvalidCountMinWidth(v) => {
write!(f, "invalid count main sketch width: {}", *v)
}
CacheError::InvalidFalsePositiveRatio(v) => write!(
f,
"invalid false positive ratio: {}, which should be in range (0.0, 1.0)",
*v
),
CacheError::SendError(msg) => write!(f, "fail to send msg to channel: {}", msg),
CacheError::RecvError(msg) => write!(f, "fail to receive msg from channel: {}", msg),
CacheError::InvalidNumCounters => write!(f, "num_counters can't be zero"),
CacheError::InvalidMaxCost => write!(f, "max_cost can't be zero"),
CacheError::InvalidBufferSize => write!(f, "buffer_size can't be zero"),
CacheError::UpdateError(msg) => write!(f, "update error: {} ", msg),
CacheError::InsertError(msg) => write!(f, "insert error: {} ", msg),
CacheError::RemoveError(msg) => write!(f, "remove error: {} ", msg),
CacheError::CleanupError(msg) => write!(f, "cleanup error: {} ", msg),
CacheError::ChannelError(msg) => write!(f, "channel error: {} ", msg),
}
}
}

impl Display for CacheError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
self.fmt(f)
}
}

impl Debug for CacheError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
self.fmt(f)
}
}

impl std::error::Error for CacheError {}
10 changes: 1 addition & 9 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,7 @@ impl<
})
.collect()
}))
}

#[cfg(feature = "async")]
pub fn cleanup_async<PS: BuildHasher + Clone + 'static>(
&self,
policy: Arc<crate::policy::AsyncLFUPolicy<PS>>,
) -> Vec<CrateItem<V>> {
self.try_cleanup_async(policy).unwrap()
}
}

#[cfg(feature = "async")]
pub fn try_cleanup_async<PS: BuildHasher + Clone + 'static>(
Expand Down

0 comments on commit 640deaf

Please sign in to comment.