Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(builder): add missing Send and Sync bounds to the builder #7

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ use crate::{
///
/// This structure has methods for building up an archive from scratch into any
/// arbitrary writer.
pub struct Builder<W: Write + Unpin> {
pub struct Builder<W: Write + Unpin + Send + Sync> {
mode: HeaderMode,
follow: bool,
finished: bool,
obj: Option<W>,
}

impl<W: Write + Unpin> Builder<W> {
impl<W: Write + Unpin + Send + Sync> Builder<W> {
/// Create a new archive builder with the underlying object as the
/// destination of all data written. The builder will use
/// `HeaderMode::Complete` by default.
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<W: Write + Unpin> Builder<W> {
/// #
/// # Ok(()) }) }
/// ```
pub async fn append<R: Read + Unpin>(
pub async fn append<R: Read + Unpin + Sync + Send>(
&mut self,
header: &Header,
mut data: R,
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<W: Write + Unpin> Builder<W> {
/// #
/// # Ok(()) }) }
/// ```
pub async fn append_data<P: AsRef<Path>, R: Read + Unpin>(
pub async fn append_data<P: AsRef<Path>, R: Read + Unpin + Sync + Send>(
&mut self,
header: &mut Header,
path: P,
Expand Down Expand Up @@ -406,9 +406,9 @@ impl<W: Write + Unpin> Builder<W> {
}

async fn append(
mut dst: &mut (dyn Write + Unpin),
mut dst: &mut (dyn Write + Unpin + Send + Sync),
header: &Header,
mut data: &mut (dyn Read + Unpin),
mut data: &mut (dyn Read + Unpin + Send + Sync),
) -> io::Result<()> {
dst.write_all(header.as_bytes()).await?;
let len = io::copy(&mut data, &mut dst).await?;
Expand All @@ -424,7 +424,7 @@ async fn append(
}

async fn append_path_with_name(
dst: &mut (dyn Write + Unpin),
dst: &mut (dyn Write + Unpin + Sync + Send),
path: &Path,
name: Option<&Path>,
mode: HeaderMode,
Expand Down Expand Up @@ -478,7 +478,7 @@ async fn append_path_with_name(
}

async fn append_file(
dst: &mut (dyn Write + Unpin),
dst: &mut (dyn Write + Unpin + Send + Sync),
path: &Path,
file: &mut fs::File,
mode: HeaderMode,
Expand All @@ -489,7 +489,7 @@ async fn append_file(
}

async fn append_dir(
dst: &mut (dyn Write + Unpin),
dst: &mut (dyn Write + Unpin + Send + Sync),
path: &Path,
src_path: &Path,
mode: HeaderMode,
Expand All @@ -515,7 +515,7 @@ fn prepare_header(size: u64, entry_type: EntryType) -> Header {
}

async fn prepare_header_path(
dst: &mut (dyn Write + Unpin),
dst: &mut (dyn Write + Unpin + Send + Sync),
header: &mut Header,
path: &Path,
) -> io::Result<()> {
Expand Down Expand Up @@ -544,7 +544,7 @@ async fn prepare_header_path(
}

async fn prepare_header_link(
dst: &mut (dyn Write + Unpin),
dst: &mut (dyn Write + Unpin + Send + Sync),
header: &mut Header,
link_name: &Path,
) -> io::Result<()> {
Expand All @@ -562,10 +562,10 @@ async fn prepare_header_link(
}

async fn append_fs(
dst: &mut (dyn Write + Unpin),
dst: &mut (dyn Write + Unpin + Send + Sync),
path: &Path,
meta: &fs::Metadata,
read: &mut (dyn Read + Unpin),
read: &mut (dyn Read + Unpin + Sync + Send),
mode: HeaderMode,
link_name: Option<&Path>,
) -> io::Result<()> {
Expand All @@ -583,7 +583,7 @@ async fn append_fs(
}

async fn append_dir_all(
dst: &mut (dyn Write + Unpin),
dst: &mut (dyn Write + Unpin + Send + Sync),
path: &Path,
src_path: &Path,
mode: HeaderMode,
Expand Down Expand Up @@ -615,10 +615,18 @@ async fn append_dir_all(
Ok(())
}

impl<W: Write + Unpin> Drop for Builder<W> {
impl<W: Write + Unpin + Send + Sync> Drop for Builder<W> {
fn drop(&mut self) {
async_std::task::block_on(async move {
let _ = self.finish().await;
});
}
}

#[cfg(test)]
mod tests {
use super::*;

assert_impl_all!(async_std::fs::File: Send, Sync);
assert_impl_all!(Builder<async_std::fs::File>: Send, Sync);
}
2 changes: 1 addition & 1 deletion tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ async fn extracting_malicious_tarball() {

{
let mut a = Builder::new(&mut evil_tar);
async fn append<R: Write + Unpin>(a: &mut Builder<R>, path: &'static str) {
async fn append<R: Write + Unpin + Send + Sync>(a: &mut Builder<R>, path: &'static str) {
let mut header = Header::new_gnu();
assert!(header.set_path(path).is_err(), "was ok: {:?}", path);
{
Expand Down