You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.8 KiB

//! Configuration
use super::*;
use prov::prelude::*;
/// Application state
pub struct State
{
pub throttle_generator: Option<Box<dyn DynThrottleProvider<'static> + 'static>>,
pub buffer_size: Box<dyn BufferProvider + 'static>,
}
impl State
{
/// Convert into an instance that can be used to create a `StateThrottleAdaptor`
#[inline(always)]
pub fn with_stream<S>(self, stream: S) -> (Self, S)
{
(self, stream)
}
}
/// Configuration for application
#[derive(Debug, Clone)]
pub struct Config
{
pub throttle: Option<(Duration, Option<Duration>)>,
pub buffer_size: Option<(usize, Option<usize>)>,
}
impl From<Config> for State
{
#[inline]
fn from(from: Config) -> Self
{
Self {
throttle_generator: if let Some(throttle) = from.throttle {
Some(match throttle {
(Duration::ZERO, Some(Duration::ZERO) | None) => Box::new(NoThrottleProvider),
(low, Some(high)) if low == high => Box::new(SingleThrottleProvider::from(low)),
(low, Some(high)) => Box::new(UniformThrottleProvider::from(low..high)),
(low, _) => Box::new(SingleThrottleProvider::from(low)),
})
} else {
None
},
buffer_size: if let Some(size) = from.buffer_size {
match size {
(0, Some(0) | None) => Box::new(NoBufferProvider),
(low, Some(high)) if low == high => Box::new(low),
(low, Some(high)) => Box::new(UniformBufferProvider::from(low..high)),
(low, _) => Box::new(low)
}
} else {
Box::new(DefaultBufferSize)
}
}
}
}
impl Default for Config
{
#[inline]
fn default() -> Self
{
Self::new()
}
}
impl Config
{
/// Create an empty new config
#[inline(always)]
pub const fn new() -> Self
{
Self {
throttle: None,
buffer_size: None,
}
}
}