fix(clippy): appease the paperclip

This commit is contained in:
Kat Marchán 2021-09-15 19:57:58 -07:00
parent 97890872d5
commit 8d08e45298
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
9 changed files with 64 additions and 74 deletions

View File

@ -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));
})
});

View File

@ -53,7 +53,7 @@ impl AsyncReader {
}
pub fn open(cache: &Path, sri: Integrity) -> Result<Reader> {
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<Reader> {
}
pub async fn open_async(cache: &Path, sri: Integrity) -> Result<AsyncReader> {
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<AsyncReader> {
}
pub fn read(cache: &Path, sri: &Integrity) -> Result<Vec<u8>> {
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<Vec<u8>> {
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<u64> {
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<u64> {
}
pub async fn copy_async<'a>(cache: &'a Path, sri: &'a Integrity, to: &'a Path) -> Result<u64> {
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<Integrity> {
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<Integrity> {
}
pub async fn has_content_async(cache: &Path, sri: &Integrity) -> Option<Integrity> {
if async_std::fs::metadata(path::content_path(&cache, &sri))
if async_std::fs::metadata(path::content_path(cache, sri))
.await
.is_ok()
{

View File

@ -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(())

View File

@ -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<usize> {
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<std::io::Result<usize>> {
@ -273,7 +273,7 @@ impl AsyncWrite for AsyncWriter {
}
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
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<std::io::Result<()>> {
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
let state = &mut *self.0.lock().unwrap();
loop {

View File

@ -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<P: AsRef<Path>>(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<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>>
where
P: AsRef<Path>,
{
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<Path>,
K: AsRef<str>,
{
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<P: AsRef<Path>>(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() {

View File

@ -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<Integrity> {
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> {
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<Integrity> {
}
pub async fn insert_async<'a>(cache: &'a Path, key: &'a str, opts: WriteOpts) -> Result<Integrity> {
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<Option<Metadata>> {
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<Option<Metadata>> {
}
pub async fn find_async(cache: &Path, key: &str) -> Result<Option<Metadata>> {
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<Item = Result<Metadata>> {
}
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<Vec<SerializableM
mod tests {
use super::*;
use async_std::task;
use tempfile;
use serde_json::json;
const MOCK_ENTRY: &str = "\n251d18a2b33264ea8655695fd23c88bd874cdea2c3dc9d8f9b7596717ad30fec\t{\"key\":\"hello\",\"integrity\":\"sha1-deadbeef\",\"time\":1234567,\"size\":0,\"metadata\":null}";

View File

@ -12,7 +12,6 @@ pub fn list_sync<P: AsRef<Path>>(cache: P) -> impl Iterator<Item = Result<index:
#[cfg(test)]
mod tests {
use super::*;
use tempfile;
#[test]
fn test_list_sync() {

View File

@ -144,7 +144,7 @@ impl Writer {
let writer_sri = self.writer.close().await?;
if let Some(sri) = &self.opts.sri {
if sri.matches(&writer_sri).is_none() {
return Err(ssri::Error::IntegrityCheckError(sri.clone(), writer_sri))?;
return Err(ssri::Error::IntegrityCheckError(sri.clone(), writer_sri).into());
}
} else {
self.opts.sri = Some(writer_sri.clone());
@ -403,14 +403,14 @@ impl SyncWriter {
let writer_sri = self.writer.close()?;
if let Some(sri) = &self.opts.sri {
if sri.matches(&writer_sri).is_none() {
return Err(ssri::Error::IntegrityCheckError(sri.clone(), writer_sri))?;
return Err(ssri::Error::IntegrityCheckError(sri.clone(), writer_sri).into());
}
} else {
self.opts.sri = Some(writer_sri.clone());
}
if let Some(size) = self.opts.size {
if size != self.written {
return Err(Error::SizeError(size, self.written))?;
return Err(Error::SizeError(size, self.written));
}
}
if let Some(key) = self.key {
@ -423,8 +423,6 @@ impl SyncWriter {
#[cfg(test)]
mod tests {
use async_attributes;
#[async_attributes::test]
async fn round_trip() {
let tmp = tempfile::tempdir().unwrap();

View File

@ -66,7 +66,7 @@ where
/// }
/// ```
pub async fn remove_hash<P: AsRef<Path>>(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<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<()
/// }
/// ```
pub async fn clear<P: AsRef<Path>>(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<P: AsRef<Path>>(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<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<()>
/// }
/// ```
pub fn clear_sync<P: AsRef<Path>>(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);
}
}