a8m / a8m/pb

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

Aperta Adatta ai principianti
#66 1 commento 1 reazione 0 assegnatari Vedi su GitHub
help wanted
Lingua principale
Rust
Stelle
600
Fork
59
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

The "broadcast writing (simple file copying)" example on the [README.md](https://github.com/a8m/pb/blob/1747edaf2fc839b9542a0476c070ee6fb3effa07/README.md) no longer compiles. This is because `Writer::broadcast()` was never stabilized and [removed from Nightly libstd on Mar 8, 2016](https://github.com/rust-lang/rust/commit/b53764c73bd722ea22142bace6249d5950066253#diff-668f8f358d4a93474b396dcb3727399eL1128).

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

```rust
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 {
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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

The issue points to a specific example in README.md that uses a removed `broadcast()` method. The reporter provides a full replacement example using a custom `TeeWriter`. To fix, update the README.md file with the provided code, ensuring it compiles with current Rust. Verify by running `cargo test` or checking the example in a Rust project. The change is confined to documentation, but requires understanding of Rust's I/O traits and the crate's progress bar usage.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
rust
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
75/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.