[][src]Struct bi::RwBi

pub struct RwBi<R, W> where
    R: Read,
    W: Write
{ /* fields omitted */ }

Read/write Bi

Features

  • This bi supports a limited buffer for reads. If the buffer is full, all new reads will have to wait.
  • You can set max parallel reads/writes. This might be useful if you are reading from/writing to an old hard disk. In that case, you can set small numbers for parallel reads/writes, for example 1 for each.

Usage

  • For input, they will be wrapped inside BufReader. So you don't have to.
  • For output, they will not be wrapped inside BufWriter. So you might want to do that.
  • You must call ::release_read() when done handling your data. Or next reads will fail, when the internal buffer counter is full.
  • Be careful with thread panics. This bi uses Mutex for locks, so if your thread panics lead to poisoned mutexes, the whole bi will stop working.

Examples

use std::{
    fs::File,
    io::{self, Sink},
    path::PathBuf,
    sync::Arc,
    thread,
    time::Duration,
};

use bi::{Bi, RwBi};

let max_buf_size = 1024 * 1024;
let buf_reader_capacity = 64;
let max_parallel_reads = 4;
let max_parallel_writes = 4;
let timeout = Duration::from_secs(30);
let rw_bi: RwBi<File, Sink> = RwBi::make(
    max_buf_size, buf_reader_capacity, max_parallel_reads, max_parallel_writes, timeout,
)
    .unwrap();
let rw_bi = Arc::new(rw_bi);

let job = {
    let rw_bi = rw_bi.clone();
    thread::spawn(move || {
        // Read some bytes
        let file = File::open(PathBuf::from(file!()).canonicalize().unwrap()).unwrap();
        let (read_id, bytes) = rw_bi.read(file).unwrap();
        assert_eq!(rw_bi.buf_size(), bytes.len());

        // Release read
        rw_bi.release_read(read_id).unwrap();
        rw_bi.release_read(read_id).unwrap_err();
        assert_eq!(rw_bi.buf_size(), 0);

        // Write
        rw_bi.write(bytes, &mut io::sink()).unwrap();
    })
};
job.join().unwrap();

assert_eq!(rw_bi.buf_size(), 0);

Methods

impl<R, W> RwBi<R, W> where
    R: Read,
    W: Write
[src]

pub fn make(
    max_buf_size: usize,
    buf_reader_capacity: u64,
    max_parallel_reads: usize,
    max_parallel_writes: u64,
    timeout: Duration
) -> Result<Self>
[src]

Makes new instance

If any of input parameters is zero (except timeout), an error is returned.

Buf reader capacity and max parallel writes are declared in u64 to help prevent typos, while using them along with max buf size and max parallel reads. If their values are larger than max usize, an error is returned.

Trait Implementations

impl<R, W> Bi for RwBi<R, W> where
    R: Read,
    W: Write
[src]

type Input = R

Input

type Output = W

Output

Auto Trait Implementations

impl<R, W> Send for RwBi<R, W> where
    R: Send,
    W: Send

impl<R, W> Sync for RwBi<R, W> where
    R: Sync,
    W: Sync

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]