diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index 90c4b94..3c07903 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -2,9 +2,9 @@ use async_std::{fs as afs, task}; use std::fs::{self, File}; use std::io::prelude::*; -use cacache; + use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use tempfile; + const NUM_REPEATS: usize = 10; @@ -97,7 +97,7 @@ fn read_hash_many_sync(c: &mut Criterion) { c.bench_function("get::data_hash_many_sync", move |b| { b.iter(|| { for sri in sris.iter() { - cacache::read_hash_sync(black_box(&cache), black_box(&sri)).unwrap(); + cacache::read_hash_sync(black_box(&cache), black_box(sri)).unwrap(); } }) }); @@ -138,7 +138,7 @@ fn read_hash_many_async(c: &mut Criterion) { b.iter(|| { let tasks = sris .iter() - .map(|sri| cacache::read_hash(black_box(&cache), black_box(&sri))); + .map(|sri| cacache::read_hash(black_box(&cache), black_box(sri))); task::block_on(futures::future::join_all(tasks)); }) }); diff --git a/src/content/read.rs b/src/content/read.rs index 6bd3913..ade2c97 100644 --- a/src/content/read.rs +++ b/src/content/read.rs @@ -53,7 +53,7 @@ impl AsyncReader { } pub fn open(cache: &Path, sri: Integrity) -> Result { - let cpath = path::content_path(&cache, &sri); + let cpath = path::content_path(cache, &sri); Ok(Reader { fd: File::open(cpath).to_internal()?, checker: IntegrityChecker::new(sri), @@ -61,7 +61,7 @@ pub fn open(cache: &Path, sri: Integrity) -> Result { } pub async fn open_async(cache: &Path, sri: Integrity) -> Result { - let cpath = path::content_path(&cache, &sri); + let cpath = path::content_path(cache, &sri); Ok(AsyncReader { fd: async_std::fs::File::open(cpath).await.to_internal()?, checker: IntegrityChecker::new(sri), @@ -69,21 +69,21 @@ pub async fn open_async(cache: &Path, sri: Integrity) -> Result { } pub fn read(cache: &Path, sri: &Integrity) -> Result> { - let cpath = path::content_path(&cache, &sri); + let cpath = path::content_path(cache, sri); let ret = fs::read(&cpath).to_internal()?; sri.check(&ret)?; Ok(ret) } pub async fn read_async<'a>(cache: &'a Path, sri: &'a Integrity) -> Result> { - let cpath = path::content_path(&cache, &sri); + let cpath = path::content_path(cache, sri); let ret = async_std::fs::read(&cpath).await.to_internal()?; sri.check(&ret)?; Ok(ret) } pub fn copy(cache: &Path, sri: &Integrity, to: &Path) -> Result { - let cpath = path::content_path(&cache, &sri); + let cpath = path::content_path(cache, sri); let ret = fs::copy(&cpath, to).to_internal()?; let data = fs::read(cpath).to_internal()?; sri.check(data)?; @@ -91,7 +91,7 @@ pub fn copy(cache: &Path, sri: &Integrity, to: &Path) -> Result { } pub async fn copy_async<'a>(cache: &'a Path, sri: &'a Integrity, to: &'a Path) -> Result { - let cpath = path::content_path(&cache, &sri); + let cpath = path::content_path(cache, sri); let ret = async_std::fs::copy(&cpath, to).await.to_internal()?; let data = async_std::fs::read(cpath).await.to_internal()?; sri.check(data)?; @@ -99,7 +99,7 @@ pub async fn copy_async<'a>(cache: &'a Path, sri: &'a Integrity, to: &'a Path) - } pub fn has_content(cache: &Path, sri: &Integrity) -> Option { - if path::content_path(&cache, &sri).exists() { + if path::content_path(cache, sri).exists() { Some(sri.clone()) } else { None @@ -107,7 +107,7 @@ pub fn has_content(cache: &Path, sri: &Integrity) -> Option { } pub async fn has_content_async(cache: &Path, sri: &Integrity) -> Option { - if async_std::fs::metadata(path::content_path(&cache, &sri)) + if async_std::fs::metadata(path::content_path(cache, sri)) .await .is_ok() { diff --git a/src/content/rm.rs b/src/content/rm.rs index 5b85f0d..7a60869 100644 --- a/src/content/rm.rs +++ b/src/content/rm.rs @@ -8,12 +8,12 @@ use crate::content::path; use crate::errors::{Internal, Result}; pub fn rm(cache: &Path, sri: &Integrity) -> Result<()> { - fs::remove_file(path::content_path(&cache, &sri)).to_internal()?; + fs::remove_file(path::content_path(cache, sri)).to_internal()?; Ok(()) } pub async fn rm_async(cache: &Path, sri: &Integrity) -> Result<()> { - afs::remove_file(path::content_path(&cache, &sri)) + afs::remove_file(path::content_path(cache, sri)) .await .to_internal()?; Ok(()) diff --git a/src/content/write.rs b/src/content/write.rs index a6079d8..7861525 100644 --- a/src/content/write.rs +++ b/src/content/write.rs @@ -16,7 +16,7 @@ use tempfile::NamedTempFile; use crate::content::path; use crate::errors::{Internal, Result}; -pub const MAX_MMAP_SIZE: usize = 1 * 1024 * 1024; +pub const MAX_MMAP_SIZE: usize = 1024 * 1024; pub struct Writer { cache: PathBuf, @@ -73,12 +73,12 @@ impl Writer { impl Write for Writer { fn write(&mut self, buf: &[u8]) -> std::io::Result { - self.builder.input(&buf); + self.builder.input(buf); if let Some(mmap) = &mut self.mmap { - mmap.copy_from_slice(&buf); + mmap.copy_from_slice(buf); Ok(buf.len()) } else { - self.tmpfile.write(&buf) + self.tmpfile.write(buf) } } @@ -216,7 +216,7 @@ impl AsyncWriter { impl AsyncWrite for AsyncWriter { fn poll_write( - mut self: Pin<&mut Self>, + self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { @@ -273,7 +273,7 @@ impl AsyncWrite for AsyncWriter { } } - fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let state = &mut *self.0.lock().unwrap(); loop { @@ -313,7 +313,7 @@ impl AsyncWrite for AsyncWriter { } } - fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let state = &mut *self.0.lock().unwrap(); loop { diff --git a/src/get.rs b/src/get.rs index 625ab0d..178bc34 100644 --- a/src/get.rs +++ b/src/get.rs @@ -86,10 +86,10 @@ impl Reader { if let Some(entry) = index::find_async(cache.as_ref(), key.as_ref()).await? { Reader::open_hash(cache, entry.integrity).await } else { - Err(Error::EntryNotFound( + return Err(Error::EntryNotFound( cache.as_ref().to_path_buf(), key.as_ref().into(), - ))? + )) } } @@ -143,10 +143,10 @@ where if let Some(entry) = index::find_async(cache.as_ref(), key.as_ref()).await? { read_hash(cache, &entry.integrity).await } else { - Err(Error::EntryNotFound( + return Err(Error::EntryNotFound( cache.as_ref().to_path_buf(), key.as_ref().into(), - ))? + )) } } @@ -195,10 +195,10 @@ where if let Some(entry) = index::find_async(cache.as_ref(), key.as_ref()).await? { copy_hash(cache, &entry.integrity, to).await } else { - Err(Error::EntryNotFound( + return Err(Error::EntryNotFound( cache.as_ref().to_path_buf(), key.as_ref().into(), - ))? + )) } } @@ -240,7 +240,7 @@ where /// Returns true if the given hash exists in the cache. pub async fn exists>(cache: P, sri: &Integrity) -> bool { - read::has_content_async(cache.as_ref(), &sri) + read::has_content_async(cache.as_ref(), sri) .await .is_some() } @@ -310,10 +310,10 @@ impl SyncReader { if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? { SyncReader::open_hash(cache, entry.integrity) } else { - Err(Error::EntryNotFound( + return Err(Error::EntryNotFound( cache.as_ref().to_path_buf(), key.as_ref().into(), - ))? + )) } } @@ -363,10 +363,10 @@ where if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? { read_hash_sync(cache, &entry.integrity) } else { - Err(Error::EntryNotFound( + return Err(Error::EntryNotFound( cache.as_ref().to_path_buf(), key.as_ref().into(), - ))? + )) } } @@ -387,7 +387,7 @@ pub fn read_hash_sync

(cache: P, sri: &Integrity) -> Result> where P: AsRef, { - Ok(read::read(cache.as_ref(), sri)?) + read::read(cache.as_ref(), sri) } /// Copies a cache entry by key to a specified location. Returns the number of @@ -411,10 +411,10 @@ where if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? { copy_hash_sync(cache, &entry.integrity, to) } else { - Err(Error::EntryNotFound( + return Err(Error::EntryNotFound( cache.as_ref().to_path_buf(), key.as_ref().into(), - ))? + )) } } @@ -449,21 +449,19 @@ where P: AsRef, K: AsRef, { - Ok(index::find(cache.as_ref(), key.as_ref())?) + index::find(cache.as_ref(), key.as_ref()) } /// Returns true if the given hash exists in the cache. pub fn exists_sync>(cache: P, sri: &Integrity) -> bool { - read::has_content(cache.as_ref(), &sri).is_some() + read::has_content(cache.as_ref(), sri).is_some() } #[cfg(test)] mod tests { - use async_attributes; use async_std::fs as afs; use async_std::prelude::*; use std::fs; - use tempfile; #[async_attributes::test] async fn test_open() { diff --git a/src/index.rs b/src/index.rs index 21bace8..3963973 100644 --- a/src/index.rs +++ b/src/index.rs @@ -11,9 +11,8 @@ use digest::Digest; use either::{Left, Right}; use futures::io::{AsyncBufReadExt, AsyncWriteExt}; use futures::stream::StreamExt; -use hex; use serde_derive::{Deserialize, Serialize}; -use serde_json::{json, Value}; +use serde_json::Value; use sha1::Sha1; use sha2::Sha256; use ssri::Integrity; @@ -63,7 +62,7 @@ impl Hash for SerializableMetadata { } pub fn insert(cache: &Path, key: &str, opts: WriteOpts) -> Result { - let bucket = bucket_path(&cache, &key); + let bucket = bucket_path(cache, key); fs::create_dir_all(bucket.parent().unwrap()).with_context(|| { format!( "Failed to create index bucket directory: {:?}", @@ -75,7 +74,7 @@ pub fn insert(cache: &Path, key: &str, opts: WriteOpts) -> Result { integrity: opts.sri.clone().map(|x| x.to_string()), time: opts.time.unwrap_or_else(now), size: opts.size.unwrap_or(0), - metadata: opts.metadata.unwrap_or_else(|| json!(null)), + metadata: opts.metadata.unwrap_or(serde_json::Value::Null), }) .with_context(|| format!("Failed to serialize entry with key `{}`", key))?; @@ -97,7 +96,7 @@ pub fn insert(cache: &Path, key: &str, opts: WriteOpts) -> Result { } pub async fn insert_async<'a>(cache: &'a Path, key: &'a str, opts: WriteOpts) -> Result { - let bucket = bucket_path(&cache, &key); + let bucket = bucket_path(cache, key); afs::create_dir_all(bucket.parent().unwrap()) .await .with_context(|| { @@ -111,7 +110,7 @@ pub async fn insert_async<'a>(cache: &'a Path, key: &'a str, opts: WriteOpts) -> integrity: opts.sri.clone().map(|x| x.to_string()), time: opts.time.unwrap_or_else(now), size: opts.size.unwrap_or(0), - metadata: opts.metadata.unwrap_or_else(|| json!(null)), + metadata: opts.metadata.unwrap_or(serde_json::Value::Null), }) .with_context(|| format!("Failed to serialize entry with key `{}`", key))?; @@ -136,7 +135,7 @@ pub async fn insert_async<'a>(cache: &'a Path, key: &'a str, opts: WriteOpts) -> } pub fn find(cache: &Path, key: &str) -> Result> { - let bucket = bucket_path(cache, &key); + let bucket = bucket_path(cache, key); Ok(bucket_entries(&bucket) .with_context(|| format!("Failed to read index bucket entries from {:?}", bucket))? .into_iter() @@ -164,7 +163,7 @@ pub fn find(cache: &Path, key: &str) -> Result> { } pub async fn find_async(cache: &Path, key: &str) -> Result> { - let bucket = bucket_path(cache, &key); + let bucket = bucket_path(cache, key); Ok(bucket_entries_async(&bucket) .await .with_context(|| format!("Failed to read index bucket entries from {:?}", bucket))? @@ -258,7 +257,7 @@ pub fn ls(cache: &Path) -> impl Iterator> { } fn bucket_path(cache: &Path, key: &str) -> PathBuf { - let hashed = hash_key(&key); + let hashed = hash_key(key); cache .join(format!("index-v{}", INDEX_VERSION)) .join(&hashed[0..2]) @@ -343,7 +342,7 @@ async fn bucket_entries_async(bucket: &Path) -> InternalResult>(cache: P) -> impl Iterator>(cache: P, sri: &Integrity) -> Result<()> { - Ok(rm::rm_async(cache.as_ref(), &sri).await?) + Ok(rm::rm_async(cache.as_ref(), sri).await?) } /// Removes entire contents of the cache, including temporary files, the entry @@ -92,10 +92,8 @@ pub async fn remove_hash>(cache: P, sri: &Integrity) -> Result<() /// } /// ``` pub async fn clear>(cache: P) -> Result<()> { - for entry in cache.as_ref().read_dir().to_internal()? { - if let Ok(entry) = entry { - afs::remove_dir_all(entry.path()).await.to_internal()?; - } + for entry in (cache.as_ref().read_dir().to_internal()?).flatten() { + afs::remove_dir_all(entry.path()).await.to_internal()?; } Ok(()) } @@ -152,7 +150,7 @@ where /// } /// ``` pub fn remove_hash_sync>(cache: P, sri: &Integrity) -> Result<()> { - Ok(rm::rm(cache.as_ref(), &sri)?) + rm::rm(cache.as_ref(), sri) } /// Removes entire contents of the cache synchronously, including temporary @@ -176,10 +174,8 @@ pub fn remove_hash_sync>(cache: P, sri: &Integrity) -> Result<()> /// } /// ``` pub fn clear_sync>(cache: P) -> Result<()> { - for entry in cache.as_ref().read_dir().to_internal()? { - if let Ok(entry) = entry { - fs::remove_dir_all(entry.path()).to_internal()?; - } + for entry in (cache.as_ref().read_dir().to_internal()?).flatten() { + fs::remove_dir_all(entry.path()).to_internal()?; } Ok(()) } @@ -201,7 +197,7 @@ mod tests { assert_eq!(entry, None); let data_exists = crate::exists(&dir, &sri).await; - assert_eq!(data_exists, true); + assert!(data_exists); }); } @@ -215,10 +211,10 @@ mod tests { crate::remove_hash(&dir, &sri).await.unwrap(); let entry = crate::metadata(&dir, "key").await.unwrap(); - assert_eq!(entry.is_some(), true); + assert!(entry.is_some()); let data_exists = crate::exists(&dir, &sri).await; - assert_eq!(data_exists, false); + assert!(data_exists); }); } @@ -232,10 +228,10 @@ mod tests { crate::clear(&dir).await.unwrap(); let entry = crate::metadata(&dir, "key").await.unwrap(); - assert_eq!(entry.is_some(), false); + assert!(entry.is_some()); let data_exists = crate::exists(&dir, &sri).await; - assert_eq!(data_exists, false); + assert!(data_exists); }); } @@ -251,7 +247,7 @@ mod tests { assert_eq!(new_entry, None); let data_exists = crate::exists_sync(&dir, &sri); - assert_eq!(data_exists, true); + assert!(data_exists); } #[test] @@ -263,10 +259,10 @@ mod tests { crate::remove_hash_sync(&dir, &sri).unwrap(); let entry = crate::metadata_sync(&dir, "key").unwrap(); - assert_eq!(entry.is_some(), true); + assert!(entry.is_some()); let data_exists = crate::exists_sync(&dir, &sri); - assert_eq!(data_exists, false); + assert!(data_exists); } #[test] @@ -281,6 +277,6 @@ mod tests { assert_eq!(entry, None); let data_exists = crate::exists_sync(&dir, &sri); - assert_eq!(data_exists, false); + assert!(data_exists); } }