Transfer

Struct Transfer 

Source
pub struct Transfer<R, W>
where R: Read + Send + 'static, W: Write + Send + 'static,
{ /* private fields */ }
Expand description

Monitors the progress of a transfer from a reader to a writer.

Implementations§

Source§

impl<R, W> Transfer<R, W>
where R: Read + Send + 'static, W: Write + Send + 'static,

Source

pub fn new(reader: R, writer: W) -> Self

Creates and starts a new Transfer.

§Example
use transfer_progress::Transfer;
use std::fs::File;
let reader = File::open("file1.txt")?;
let writer = File::create("file2.txt")?;
let transfer = Transfer::new(reader, writer);
Examples found in repository?
examples/copy.rs (line 13)
8fn main() -> io::Result<()> {
9    let reader = File::open("/dev/urandom")?.take(1024 * 1024 * 1024); // 1 GiB
10    let writer = io::sink();
11
12    // Create the transfer monitor
13    let transfer = Transfer::new(reader, writer);
14
15    while !transfer.is_complete() {
16        std::thread::sleep(std::time::Duration::from_secs(1));
17        // {:#} makes Transfer use SI units (MiB instead of MB)
18        println!("{:#}", transfer);
19    }
20
21    // Catch any errors and retrieve the reader and writer
22    let (_reader, _writer) = transfer.finish()?;
23    Ok(())
24}
Source

pub fn finish(self) -> Result<(R, W)>

Consumes the Transfer, blocking until the transfer is complete.

If the transfer was successful, returns Ok(reader, writer), otherwise returns the error.

If the transfer is already complete, returns immediately.

§Example
use transfer_progress::Transfer;
use std::fs::File;
let reader = File::open("file1.txt")?;
let writer = File::create("file2.txt")?;
let transfer = Transfer::new(reader, writer);
let (reader, writer) = transfer.finish()?;
Examples found in repository?
examples/copy.rs (line 22)
8fn main() -> io::Result<()> {
9    let reader = File::open("/dev/urandom")?.take(1024 * 1024 * 1024); // 1 GiB
10    let writer = io::sink();
11
12    // Create the transfer monitor
13    let transfer = Transfer::new(reader, writer);
14
15    while !transfer.is_complete() {
16        std::thread::sleep(std::time::Duration::from_secs(1));
17        // {:#} makes Transfer use SI units (MiB instead of MB)
18        println!("{:#}", transfer);
19    }
20
21    // Catch any errors and retrieve the reader and writer
22    let (_reader, _writer) = transfer.finish()?;
23    Ok(())
24}
Source

pub fn is_complete(&self) -> bool

Tests if the transfer is complete

§Example
use transfer_progress::Transfer;
use std::fs::File;
let reader = File::open("file1.txt")?;
let writer = File::create("file2.txt")?;
let transfer = Transfer::new(reader, writer);
while !transfer.is_complete() {
println!("Not complete yet");
std::thread::sleep(std::time::Duration::from_secs(1));
}
println!("Complete!");
Examples found in repository?
examples/copy.rs (line 15)
8fn main() -> io::Result<()> {
9    let reader = File::open("/dev/urandom")?.take(1024 * 1024 * 1024); // 1 GiB
10    let writer = io::sink();
11
12    // Create the transfer monitor
13    let transfer = Transfer::new(reader, writer);
14
15    while !transfer.is_complete() {
16        std::thread::sleep(std::time::Duration::from_secs(1));
17        // {:#} makes Transfer use SI units (MiB instead of MB)
18        println!("{:#}", transfer);
19    }
20
21    // Catch any errors and retrieve the reader and writer
22    let (_reader, _writer) = transfer.finish()?;
23    Ok(())
24}
More examples
Hide additional examples
examples/sized_copy.rs (line 18)
11fn main() -> io::Result<()> {
12    let reader = File::open("/dev/urandom")?.take(DATA_TO_TRANSFER);
13    let writer = io::sink();
14
15    // Create the transfer monitor
16    let transfer = SizedTransfer::new(reader, writer, DATA_TO_TRANSFER);
17
18    while !transfer.is_complete() {
19        std::thread::sleep(std::time::Duration::from_secs(1));
20        // {:#} makes Transfer use SI units (MiB instead of MB)
21        println!("{:#}", transfer);
22    }
23
24    // Catch any errors and retrieve the reader and writer
25    let (_reader, _writer) = transfer.finish()?;
26    Ok(())
27}
Source

pub fn transferred(&self) -> u64

Returns the number of bytes transferred thus far between the reader and the writer.

§Example
use transfer_progress::Transfer;
use std::fs::File;
let reader = File::open("file1.txt")?;
let writer = File::create("file2.txt")?;
let transfer = Transfer::new(reader, writer);
while !transfer.is_complete() {
println!("{} bytes transferred so far", transfer.transferred());
std::thread::sleep(std::time::Duration::from_secs(1));
}
println!("Complete!");
Source

pub fn running_time(&self) -> Duration

Returns the elapsed time since the transfer started.

§Example
use transfer_progress::Transfer;
use std::fs::File;
let reader = File::open("file1.txt")?;
let writer = File::create("file2.txt")?;
let transfer = Transfer::new(reader, writer);
while !transfer.is_complete() {}
println!("Transfer took {:?}", transfer.running_time());
Source

pub fn speed(&self) -> u64

Returns the average speed, in bytes per second, of the transfer.

§Example
use transfer_progress::Transfer;
use std::fs::File;
let reader = File::open("file1.txt")?;
let writer = File::create("file2.txt")?;
let transfer = Transfer::new(reader, writer);
while !transfer.is_complete() {
println!("{}B/s", transfer.speed());
std::thread::sleep(std::time::Duration::from_secs(1));
}

Trait Implementations§

Source§

impl<R, W> Debug for Transfer<R, W>
where R: Read + Send + 'static, W: Write + Send + 'static,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R, W> Display for Transfer<R, W>
where R: Read + Send + 'static, W: Write + Send + 'static,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R, W> Freeze for Transfer<R, W>

§

impl<R, W> !RefUnwindSafe for Transfer<R, W>

§

impl<R, W> Send for Transfer<R, W>

§

impl<R, W> Sync for Transfer<R, W>

§

impl<R, W> Unpin for Transfer<R, W>

§

impl<R, W> !UnwindSafe for Transfer<R, W>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.