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

Broadcast writing on README no longer compiles (due to removal of broadcast()) #66

Open
tatsuya6502 opened this issue Apr 30, 2018 · 1 comment

Comments

@tatsuya6502
Copy link

The "broadcast writing (simple file copying)" example on the README.md no longer compiles. This is because Writer::broadcast() was never stabilized and removed from Nightly libstd on Mar 8, 2016.

I think a simple workaround is to change the example as the followings:

extern crate pbr;

use std::io::copy;
use pbr::{ProgressBar, Units};
use std::fs::File;
use std::io::Write;

fn main() {
    let mut file = File::open("/usr/share/dict/words").unwrap();
    let n_bytes = file.metadata().unwrap().len() as usize;
    let mut pb = ProgressBar::new(n_bytes as u64);
    pb.set_units(Units::Bytes);
    let mut handle = File::create("copy-words").unwrap();
    {
        let mut tee = TeeWriter::new(&mut handle, &mut pb);
        copy(&mut file, &mut tee).unwrap();
    }
    pb.finish_print("done");
}

/// Tee (T-Split) Writer writes the same data to two child writers.
struct TeeWriter<'a, T1: Write + 'a, T2: Write + 'a> {
    w1: &'a mut T1,
    w2: &'a mut T2,
}

impl<'a, T1, T2> TeeWriter<'a, T1, T2>
where
    T1: Write + 'a,
    T2: Write + 'a,
{
    fn new(w1: &'a mut T1, w2: &'a mut T2) -> Self {
        Self { w1, w2 }
    }
}

impl<'a, T1, T2> Write for TeeWriter<'a, T1, T2>
where
    T1: Write + 'a,
    T2: Write + 'a,
{
    #[inline]
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let size1 = self.w1.write(buf)?;
        let size2 = self.w2.write(buf)?;
        Ok(size1.min(size2))
    }

    #[inline]
    fn flush(&mut self) -> std::io::Result<()> {
        self.w1.flush()?;
        self.w2.flush()
    }
}

Or, for convenience, maybe we can add something like TeeWriter above to this crate.

I could work on either ideas. Please let me know your thoughts.

@a8m
Copy link
Owner

a8m commented May 5, 2018

Thanks for reporting this issue @tatsuya6502. I'm not sure if having something like TeeWriter should be part of this crate. However, I think we can start with updating the README, and then see what other people have to say about it.

@a8m a8m added the help wanted label May 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants