Skip to content

Commit

Permalink
NFC stack: move more of the set / put logic to associated functions
Browse files Browse the repository at this point in the history
We'll want to call these, with our own `sync` logic, for
`NamedTempFile`s.

TESTED=existing tests.
  • Loading branch information
pkhuong committed Sep 12, 2021
1 parent 7daed9c commit 8618dc1
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,16 @@ impl Cache {
Ok(ret)
}

fn set_impl(&self, key: Key, value: &Path) -> Result<()> {
match self.write_side.as_ref() {
Some(write) => write.set(key, value),
None => Err(Error::new(
ErrorKind::Unsupported,
"no kismet write cache defined",
)),
}
}

/// Inserts or overwrites the file at `value` as `key` in the
/// write cache directory. This will always fail with
/// [`ErrorKind::Unsupported`] if no write cache was defined.
Expand All @@ -494,21 +504,23 @@ impl Cache {
/// amortised to logarithmic, and constant number of file operations.
pub fn set<'a>(&self, key: impl Into<Key<'a>>, value: impl AsRef<Path>) -> Result<()> {
fn doit(this: &Cache, key: Key, value: &Path) -> Result<()> {
match this.write_side.as_ref() {
Some(write) => {
this.maybe_sync_path(value)?;
write.set(key, value)
}
None => Err(Error::new(
ErrorKind::Unsupported,
"no kismet write cache defined",
)),
}
this.maybe_sync_path(value)?;
this.set_impl(key, value)
}

doit(self, key.into(), value.as_ref())
}

fn put_impl(&self, key: Key, value: &Path) -> Result<()> {
match self.write_side.as_ref() {
Some(write) => write.put(key, value),
None => Err(Error::new(
ErrorKind::Unsupported,
"no kismet write cache defined",
)),
}
}

/// Inserts the file at `value` as `key` in the cache directory if
/// there is no such cached entry already, or touches the cached
/// file if it already exists. This will always fail with
Expand All @@ -533,16 +545,8 @@ impl Cache {
/// amortised to logarithmic, and constant number of file operations.
pub fn put<'a>(&self, key: impl Into<Key<'a>>, value: impl AsRef<Path>) -> Result<()> {
fn doit(this: &Cache, key: Key, value: &Path) -> Result<()> {
match this.write_side.as_ref() {
Some(write) => {
this.maybe_sync_path(value)?;
write.put(key, value)
}
None => Err(Error::new(
ErrorKind::Unsupported,
"no kismet write cache defined",
)),
}
this.maybe_sync_path(value)?;
this.put_impl(key, value)
}

doit(self, key.into(), value.as_ref())
Expand Down

0 comments on commit 8618dc1

Please sign in to comment.