1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
use std::{num::NonZeroUsize, thread};
use futures::TryStreamExt;
use tokio::io::AsyncRead;
use super::{Inflater, Reader};
use crate::Block;
/// An async BGZF reader builder.
#[derive(Default)]
pub struct Builder {
worker_count: Option<NonZeroUsize>,
}
impl Builder {
/// Sets a worker count.
///
/// By default, the worker count is set to the number of available logical CPUs.
///
/// # Examples
///
/// ```
/// use std::num::NonZeroUsize;
///
/// use noodles_bgzf as bgzf;
///
/// let worker_count = NonZeroUsize::try_from(1)?;
/// let builder = bgzf::r#async::reader::Builder::default()
/// .set_worker_count(worker_count);
/// # Ok::<_, std::num::TryFromIntError>(())
/// ```
pub fn set_worker_count(mut self, worker_count: NonZeroUsize) -> Self {
self.worker_count = Some(worker_count);
self
}
/// Builds an async BGZF reader with an async reader.
///
/// # Examples
///
/// ```
/// # use tokio::io;
/// use noodles_bgzf as bgzf;
/// let reader = bgzf::r#async::reader::Builder::default()
/// .build_with_reader(io::empty());
/// ```
pub fn build_with_reader<R>(self, reader: R) -> Reader<R>
where
R: AsyncRead,
{
let worker_count = self.worker_count.unwrap_or_else(|| {
thread::available_parallelism().unwrap_or_else(|_| NonZeroUsize::new(1).unwrap())
});
Reader {
stream: Some(Inflater::new(reader).try_buffered(worker_count.get())),
block: Block::default(),
position: 0,
worker_count,
}
}
}