Skip to content

Commit

Permalink
refactor: insert with interface allow return fetch value error (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <mrcroxx@outlook.com>
  • Loading branch information
MrCroxx committed Jul 21, 2023
1 parent 67c99d2 commit a8c1c78
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions foyer-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
async-channel = "1.8"
async-trait = "0.1"
bitflags = "2.3.1"
Expand Down
18 changes: 12 additions & 6 deletions foyer-storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,31 @@ pub enum Error {
#[error("channel full")]
ChannelFull,
#[error("event listener error: {0}")]
EventListener(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
EventListener(Box<dyn std::error::Error + Send + Sync + 'static>),
#[error("fetch value error: {0}")]
FetchValue(anyhow::Error),
#[error("other error: {0}")]
Other(String),
Other(#[from] anyhow::Error),
}

impl Error {
pub fn device(e: super::device::error::Error) -> Self {
Self::Device(e)
}

pub fn fetch_value(e: impl Into<anyhow::Error>) -> Self {
Self::Other(e.into())
}

pub fn other(e: impl Into<anyhow::Error>) -> Self {
Self::Other(e.into())
}

pub fn event_listener(
e: impl Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
) -> Self {
Self::EventListener(e.into())
}

pub fn other(e: impl Into<Box<dyn std::error::Error>>) -> Self {
Self::Other(e.into().to_string())
}
}

pub type Result<T> = core::result::Result<T, Error>;
12 changes: 6 additions & 6 deletions foyer-storage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ where
#[tracing::instrument(skip(self, f))]
pub async fn insert_with<F>(&self, key: K, f: F, weight: usize) -> Result<bool>
where
F: Fn(&K) -> V,
F: Fn(&K) -> anyhow::Result<V>,
{
let mut writer = self.writer(key, weight);
if !writer.judge() {
return Ok(false);
}
let value = f(&writer.key);
let value = f(&writer.key).map_err(Error::fetch_value)?;
writer.finish(value).await
}

Expand All @@ -292,20 +292,20 @@ where
#[tracing::instrument(skip(self, f))]
pub async fn insert_with_future<F>(&self, key: K, f: F, weight: usize) -> Result<bool>
where
F: Fn(&K) -> Pin<Box<dyn Future<Output = V>>>,
F: Fn(&K) -> Pin<Box<dyn Future<Output = anyhow::Result<V>>>>,
{
let mut writer = self.writer(key, weight);
if !writer.judge() {
return Ok(false);
}
let value = f(&writer.key).await;
let value = f(&writer.key).await.map_err(Error::fetch_value)?;
writer.finish(value).await
}

#[tracing::instrument(skip(self, f))]
pub async fn insert_if_not_exists_with<F>(&self, key: K, f: F, weight: usize) -> Result<bool>
where
F: Fn(&K) -> V,
F: Fn(&K) -> anyhow::Result<V>,
{
if !self.exists(&key)? {
return Ok(false);
Expand All @@ -321,7 +321,7 @@ where
weight: usize,
) -> Result<bool>
where
F: Fn(&K) -> Pin<Box<dyn Future<Output = V>>>,
F: Fn(&K) -> Pin<Box<dyn Future<Output = anyhow::Result<V>>>>,
{
if !self.exists(&key)? {
return Ok(false);
Expand Down

0 comments on commit a8c1c78

Please sign in to comment.