diff --git a/src/content/read.rs b/src/content/read.rs index 0b46284..9c307ad 100644 --- a/src/content/read.rs +++ b/src/content/read.rs @@ -118,7 +118,7 @@ pub fn read(cache: &Path, sri: &Integrity) -> Result> { } #[cfg(any(feature = "async-std", feature = "tokio"))] -pub async fn read_async<'c, 's>(cache: &'c Path, sri: Integrity, verify_integrity: bool) -> Result> { +pub async fn read_async<'c, 's>(cache: &'c Path, sri: Integrity, verify_integrity: bool) -> Result<(Vec, Integrity)> { let cpath = path::content_path(&cache, &sri); let ret = crate::async_lib::read(&cpath).await.with_context(|| { format!( @@ -126,21 +126,22 @@ pub async fn read_async<'c, 's>(cache: &'c Path, sri: Integrity, verify_integrit path::content_path(&cache, &sri).display() ) })?; - let ret = if verify_integrity { + Ok(if verify_integrity { + // we require owned Integrity because of the below move which would otherwise try to + // provide a reference to a closure that moves to another thread, i.e. `'s: 'static` which fails let integrity_check = move || { sri.check(&ret)?; - Ok::<_, ssri::Error>(ret) + Ok::<_, ssri::Error>((sri, ret)) }; #[cfg(feature = "tokio")] - let ret = tokio::task::spawn_blocking(integrity_check).await??; + let (sri, ret) = tokio::task::spawn_blocking(integrity_check).await??; #[cfg(feature = "async-std")] - let ret = async_std::task::spawn_blocking(integrity_check).await?; + let (sri, ret) = async_std::task::spawn_blocking(integrity_check).await?; - ret + (ret, sri) } else { - ret - }; - Ok(ret) + (ret, sri) + }) } pub fn reflink_unchecked(cache: &Path, sri: &Integrity, to: &Path) -> Result<()> { diff --git a/src/get.rs b/src/get.rs index e6370d6..faee3f3 100644 --- a/src/get.rs +++ b/src/get.rs @@ -159,7 +159,7 @@ where { async fn inner(cache: &Path, key: &str) -> Result> { if let Some(entry) = index::find_async(cache, key).await? { - read_hash(cache, entry.integrity).await + read_hash(cache, entry.integrity).await.map(|(r, _i)| r) } else { Err(Error::EntryNotFound(cache.to_path_buf(), key.into())) } @@ -192,7 +192,7 @@ where { async fn inner(cache: &Path, key: &str) -> Result> { if let Some(entry) = index::find_async(cache, key).await? { - read_hash_no_integrity(cache, entry.integrity).await + read_hash_no_integrity(cache, entry.integrity).await.map(|(r, _i)| r) } else { Err(Error::EntryNotFound(cache.to_path_buf(), key.into())) } @@ -211,12 +211,12 @@ where /// #[async_attributes::main] /// async fn main() -> cacache::Result<()> { /// let sri = cacache::write("./my-cache", "my-key", b"hello").await?; -/// let data: Vec = cacache::read_hash("./my-cache", sri).await?; +/// let (data, _): (Vec, _) = cacache::read_hash("./my-cache", sri).await?; /// Ok(()) /// } /// ``` #[cfg(any(feature = "async-std", feature = "tokio"))] -pub async fn read_hash

(cache: P, sri: Integrity) -> Result> +pub async fn read_hash

(cache: P, sri: Integrity) -> Result<(Vec, Integrity)> where P: AsRef, { @@ -234,7 +234,7 @@ where /// #[async_attributes::main] /// async fn main() -> cacache::Result<()> { /// let sri = cacache::write("./my-cache", "my-key", b"hello").await?; -/// let data: Vec = cacache::read_hash_no_integrity("./my-cache", sri).await?; +/// let (data, _): (Vec, _) = cacache::read_hash_no_integrity("./my-cache", sri).await?; /// Ok(()) /// } /// ``` @@ -242,7 +242,7 @@ where /// If you have no integrity this function is for you. #[doc(hidden)] #[cfg(any(feature = "async-std", feature = "tokio"))] -pub async fn read_hash_no_integrity

(cache: P, sri: Integrity) -> Result> +pub async fn read_hash_no_integrity

(cache: P, sri: Integrity) -> Result<(Vec, Integrity)> where P: AsRef, { @@ -1054,7 +1054,7 @@ mod tests { let dir = tmp.path().to_owned(); let sri = crate::write(&dir, "my-key", b"hello world").await.unwrap(); - let data = crate::read_hash(&dir, sri).await.unwrap(); + let (data, _) = crate::read_hash(&dir, sri).await.unwrap(); assert_eq!(data, b"hello world"); } diff --git a/src/lib.rs b/src/lib.rs index 15fe683..132addd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,7 +66,7 @@ //! let sri = cacache::write("./my-cache", "key", b"hello").await?; //! //! // ...data gets looked up by `sri` ("Subresource Integrity"). -//! let data = cacache::read_hash("./my-cache", sri).await?; +//! let (data, _) = cacache::read_hash("./my-cache", sri).await?; //! assert_eq!(data, b"hello"); //! //! Ok(()) diff --git a/src/put.rs b/src/put.rs index ca41187..5efc2fc 100644 --- a/src/put.rs +++ b/src/put.rs @@ -652,7 +652,7 @@ mod tests { let integrity = crate::write_hash(&dir, &original) .await .expect("should be able to write a hash asynchronously"); - let bytes = crate::read_hash(&dir, integrity) + let (bytes, _) = crate::read_hash(&dir, integrity) .await .expect("should be able to read back what we wrote"); let result =