feat: given move, also move back

if we're breaking public api already, might as well return the integrity object for possible re-use
This commit is contained in:
Jakub Koralewski 2025-11-20 17:30:46 +00:00
parent 89457c054a
commit 6310b21632
4 changed files with 19 additions and 18 deletions

View File

@ -118,7 +118,7 @@ pub fn read(cache: &Path, sri: &Integrity) -> Result<Vec<u8>> {
} }
#[cfg(any(feature = "async-std", feature = "tokio"))] #[cfg(any(feature = "async-std", feature = "tokio"))]
pub async fn read_async<'c, 's>(cache: &'c Path, sri: Integrity, verify_integrity: bool) -> Result<Vec<u8>> { pub async fn read_async<'c, 's>(cache: &'c Path, sri: Integrity, verify_integrity: bool) -> Result<(Vec<u8>, Integrity)> {
let cpath = path::content_path(&cache, &sri); let cpath = path::content_path(&cache, &sri);
let ret = crate::async_lib::read(&cpath).await.with_context(|| { let ret = crate::async_lib::read(&cpath).await.with_context(|| {
format!( 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() 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 || { let integrity_check = move || {
sri.check(&ret)?; sri.check(&ret)?;
Ok::<_, ssri::Error>(ret) Ok::<_, ssri::Error>((sri, ret))
}; };
#[cfg(feature = "tokio")] #[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")] #[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 { } else {
ret (ret, sri)
}; })
Ok(ret)
} }
pub fn reflink_unchecked(cache: &Path, sri: &Integrity, to: &Path) -> Result<()> { pub fn reflink_unchecked(cache: &Path, sri: &Integrity, to: &Path) -> Result<()> {

View File

@ -159,7 +159,7 @@ where
{ {
async fn inner(cache: &Path, key: &str) -> Result<Vec<u8>> { async fn inner(cache: &Path, key: &str) -> Result<Vec<u8>> {
if let Some(entry) = index::find_async(cache, key).await? { 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 { } else {
Err(Error::EntryNotFound(cache.to_path_buf(), key.into())) Err(Error::EntryNotFound(cache.to_path_buf(), key.into()))
} }
@ -192,7 +192,7 @@ where
{ {
async fn inner(cache: &Path, key: &str) -> Result<Vec<u8>> { async fn inner(cache: &Path, key: &str) -> Result<Vec<u8>> {
if let Some(entry) = index::find_async(cache, key).await? { 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 { } else {
Err(Error::EntryNotFound(cache.to_path_buf(), key.into())) Err(Error::EntryNotFound(cache.to_path_buf(), key.into()))
} }
@ -211,12 +211,12 @@ where
/// #[async_attributes::main] /// #[async_attributes::main]
/// async fn main() -> cacache::Result<()> { /// async fn main() -> cacache::Result<()> {
/// let sri = cacache::write("./my-cache", "my-key", b"hello").await?; /// let sri = cacache::write("./my-cache", "my-key", b"hello").await?;
/// let data: Vec<u8> = cacache::read_hash("./my-cache", sri).await?; /// let (data, _): (Vec<u8>, _) = cacache::read_hash("./my-cache", sri).await?;
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
#[cfg(any(feature = "async-std", feature = "tokio"))] #[cfg(any(feature = "async-std", feature = "tokio"))]
pub async fn read_hash<P>(cache: P, sri: Integrity) -> Result<Vec<u8>> pub async fn read_hash<P>(cache: P, sri: Integrity) -> Result<(Vec<u8>, Integrity)>
where where
P: AsRef<Path>, P: AsRef<Path>,
{ {
@ -234,7 +234,7 @@ where
/// #[async_attributes::main] /// #[async_attributes::main]
/// async fn main() -> cacache::Result<()> { /// async fn main() -> cacache::Result<()> {
/// let sri = cacache::write("./my-cache", "my-key", b"hello").await?; /// let sri = cacache::write("./my-cache", "my-key", b"hello").await?;
/// let data: Vec<u8> = cacache::read_hash_no_integrity("./my-cache", sri).await?; /// let (data, _): (Vec<u8>, _) = cacache::read_hash_no_integrity("./my-cache", sri).await?;
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
@ -242,7 +242,7 @@ where
/// If you have no integrity this function is for you. /// If you have no integrity this function is for you.
#[doc(hidden)] #[doc(hidden)]
#[cfg(any(feature = "async-std", feature = "tokio"))] #[cfg(any(feature = "async-std", feature = "tokio"))]
pub async fn read_hash_no_integrity<P>(cache: P, sri: Integrity) -> Result<Vec<u8>> pub async fn read_hash_no_integrity<P>(cache: P, sri: Integrity) -> Result<(Vec<u8>, Integrity)>
where where
P: AsRef<Path>, P: AsRef<Path>,
{ {
@ -1054,7 +1054,7 @@ mod tests {
let dir = tmp.path().to_owned(); let dir = tmp.path().to_owned();
let sri = crate::write(&dir, "my-key", b"hello world").await.unwrap(); 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"); assert_eq!(data, b"hello world");
} }

View File

@ -66,7 +66,7 @@
//! let sri = cacache::write("./my-cache", "key", b"hello").await?; //! let sri = cacache::write("./my-cache", "key", b"hello").await?;
//! //!
//! // ...data gets looked up by `sri` ("Subresource Integrity"). //! // ...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"); //! assert_eq!(data, b"hello");
//! //!
//! Ok(()) //! Ok(())

View File

@ -652,7 +652,7 @@ mod tests {
let integrity = crate::write_hash(&dir, &original) let integrity = crate::write_hash(&dir, &original)
.await .await
.expect("should be able to write a hash asynchronously"); .expect("should be able to write a hash asynchronously");
let bytes = crate::read_hash(&dir, integrity) let (bytes, _) = crate::read_hash(&dir, integrity)
.await .await
.expect("should be able to read back what we wrote"); .expect("should be able to read back what we wrote");
let result = let result =