Struct Bi

Source
pub struct Bi { /* private fields */ }
Expand description

§Bi

This struct helps with controlling multiple tasks reading data into memory.

For examples, your program parses files. Naturally it scans for files on the system, then reads them into memory, and does the job. Now, if you want to limit total memory it uses at a time to 256 MiB, you can do this:

use {
    core::time::Duration,
    std::{
        io::{self, Read},
        sync::Arc,
        thread,
    },

    bi::{Bi, Options},
};

let bi = Arc::new(Bi::from(Options {
    limit: 1024 * 1024 * 256,
    buf_size: 1024 * 64,
    wait_timeout: Duration::from_secs(10),
}));

let threads = (0..100).map(|_| {
    let bi = bi.clone();
    thread::spawn(move || {
        // For demonstration, we use io::repeat()
        let bytes = bi.read(&mut io::repeat(0).take(100), 100)?;

        // Do something with bytes...

        io::Result::Ok(())
    })
}).collect::<Vec<_>>();

for t in threads {
    if let Err(err) = t.join().unwrap() {
        eprintln!("{}", err);
    }
}

Implementations§

Source§

impl Bi

Source

pub fn read<R>(&self, src: &mut R, capacity: usize) -> IoResult<Bytes>
where R: Read,

§Reads into new Bytes

Capacity is used to request memory upfront. It would help if you provide a good value. If capacity is lower than real data, the function will request more memory.

Source

pub fn move<B>(&self, src: B) -> Result<Bytes, Vec<u8>>
where B: Into<Vec<u8>>,

§Moves src into new Bytes

On failure, source will be returned.

Trait Implementations§

Source§

impl Debug for Bi

Source§

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

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

impl From<Options> for Bi

Source§

fn from(options: Options) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Bi

§

impl RefUnwindSafe for Bi

§

impl Send for Bi

§

impl Sync for Bi

§

impl Unpin for Bi

§

impl UnwindSafe for Bi

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, 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.