Skip to content

Commit

Permalink
fixup! tough: migrate to async
Browse files Browse the repository at this point in the history
  • Loading branch information
phu-cinemo committed Oct 9, 2023
1 parent 9036d5c commit aa7a3bc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
5 changes: 3 additions & 2 deletions tough/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use crate::schema::{
DelegatedRole, Delegations, Role, RoleType, Root, Signed, Snapshot, Timestamp,
};
pub use crate::target_name::TargetName;
use crate::transport::IntoVec;
pub use crate::transport::IntoVec;
pub use crate::transport::{
DefaultTransport, FilesystemTransport, Transport, TransportError, TransportErrorKind,
};
Expand Down Expand Up @@ -443,7 +443,8 @@ impl Repository {
pub async fn read_target(
&self,
name: &TargetName,
) -> Result<Option<impl Stream<Item = error::Result<Bytes>> + Send>> {
) -> Result<Option<impl Stream<Item = error::Result<Bytes>> + IntoVec<error::Error> + Send>>
{
// Check for repository metadata expiration.
if self.expiration_enforcement == ExpirationEnforcement::Safe {
ensure!(
Expand Down
10 changes: 6 additions & 4 deletions tough/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ use url::Url;

pub type TransportStream = Pin<Box<dyn Stream<Item = Result<Bytes, TransportError>> + Send>>;

/// Fallible byte streams that collect into a `Vec<u8>`.
#[async_trait]
pub(crate) trait IntoVec {
async fn into_vec(self) -> Result<Vec<u8>, TransportError>;
pub trait IntoVec<E> {
/// Try to collect into `Vec<u8>`.
async fn into_vec(self) -> Result<Vec<u8>, E>;
}

#[async_trait]
impl IntoVec for TransportStream {
async fn into_vec(self) -> Result<Vec<u8>, TransportError> {
impl<S: Stream<Item = Result<Bytes, E>> + Send, E: Send> IntoVec<E> for S {
async fn into_vec(self) -> Result<Vec<u8>, E> {
self.try_fold(Vec::new(), |mut acc, bytes| {
acc.extend(bytes.as_ref());
std::future::ready(Ok(acc))
Expand Down
16 changes: 5 additions & 11 deletions tough/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use futures::TryStreamExt;
use futures_core::Stream;
use std::io::Read;
use std::path::{Path, PathBuf};
use tough::IntoVec;
use url::Url;

/// Utilities for tests. Not every test module uses every function, so we suppress unused warnings.
Expand All @@ -30,17 +31,10 @@ pub fn dir_url<P: AsRef<Path>>(path: P) -> Url {
}

/// Gets the goods from a read and makes a Vec
pub async fn read_to_end<S, T, E>(mut stream: S) -> Vec<u8>
pub async fn read_to_end<E, S>(mut stream: S) -> Vec<u8>
where
S: Stream<Item = Result<T, E>> + Send,
T: AsRef<[u8]>,
E: std::error::Error,
E: std::fmt::Debug,
S: IntoVec<E>,
{
stream
.try_fold(Vec::new(), |mut acc, bytes| {
acc.extend(bytes.as_ref());
std::future::ready(Ok(acc))
})
.await
.unwrap()
stream.into_vec().await.unwrap()
}
18 changes: 5 additions & 13 deletions tuftool/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

use assert_cmd::Command;
use chrono::{Duration, Utc};
use futures::TryStreamExt;
use futures_core::Stream;
use std::path::{Path, PathBuf};
use tough::IntoVec;
use url::Url;

/// Utilities for tests. Not every test module uses every function, so we suppress unused warnings.
Expand All @@ -26,19 +25,12 @@ pub fn dir_url<P: AsRef<Path>>(path: P) -> Url {

/// Returns a vector of bytes from any stream of byte results
#[allow(unused)]
pub async fn read_to_end<S, T, E>(mut stream: S) -> Vec<u8>
pub async fn read_to_end<E, S>(mut stream: S) -> Vec<u8>
where
S: Stream<Item = Result<T, E>> + Send,
T: AsRef<[u8]>,
E: std::error::Error,
E: std::fmt::Debug,
S: IntoVec<E>,
{
stream
.try_fold(Vec::new(), |mut acc, bytes| {
acc.extend(bytes.as_ref());
std::future::ready(Ok(acc))
})
.await
.unwrap()
stream.into_vec().await.unwrap()
}

/// Creates a repository with expired timestamp metadata.
Expand Down

0 comments on commit aa7a3bc

Please sign in to comment.