[−][src]Struct bi::RwBi
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]
R: Read,
W: Write,
pub fn make(
max_buf_size: usize,
buf_reader_capacity: u64,
max_parallel_reads: usize,
max_parallel_writes: u64,
timeout: Duration
) -> Result<Self>
[src]
max_buf_size: usize,
buf_reader_capacity: u64,
max_parallel_reads: usize,
max_parallel_writes: u64,
timeout: Duration
) -> Result<Self>
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]
R: Read,
W: Write,
type Input = R
Input
type Output = W
Output
fn max_buf_size(&self) -> usize
[src]
fn buf_size(&self) -> usize
[src]
fn read(&self, res: Self::Input) -> Result<(usize, Vec<u8>)>
[src]
fn read_to_string(&self, res: Self::Input) -> Result<(usize, String)>
[src]
fn release_read(&self, read_id: usize) -> Result<()>
[src]
fn write<B: AsRef<[u8]>>(&self, bytes: B, res: &mut Self::Output) -> Result<()>
[src]
Auto Trait Implementations
impl<R, W> Send for RwBi<R, W> where
R: Send,
W: Send,
R: Send,
W: Send,
impl<R, W> Sync for RwBi<R, W> where
R: Sync,
W: Sync,
R: Sync,
W: Sync,
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,