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.
149 lines
3.3 KiB
149 lines
3.3 KiB
//! Throttle provider
|
|
use std::time::Duration;
|
|
use std::iter;
|
|
|
|
pub trait DurationProvider
|
|
{
|
|
fn get_next_duration(&mut self) -> Option<Duration>;
|
|
}
|
|
|
|
/// An iterator adaptor for a `DurationProvider`.
|
|
#[derive(Debug, Clone)]
|
|
pub struct DurationProviderIter<T: ?Sized>(T);
|
|
|
|
impl<T> DurationProviderIter<T>
|
|
{
|
|
/// Consume into the backing `DurationProvider`
|
|
#[inline(always)]
|
|
pub fn into_inner(self) -> T
|
|
{
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl<T: ?Sized + DurationProvider> Iterator for DurationProviderIter<T>
|
|
{
|
|
type Item = Duration;
|
|
#[inline(always)]
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
self.0.get_next_duration()
|
|
}
|
|
}
|
|
|
|
|
|
pub trait DurationProviderIterExt<'a>
|
|
{
|
|
type Iter: iter::Iterator<Item=Duration> + 'a;
|
|
fn get_all_durations(self) -> Self::Iter;
|
|
}
|
|
|
|
pub trait DurationProviderExt<'a>
|
|
{
|
|
fn get_all_durations_dyn(self: Box<Self>) -> Box<dyn iter::Iterator<Item=Duration> + 'a>;
|
|
}
|
|
|
|
impl<'a, T: DurationProvider + 'a> DurationProviderIterExt<'a> for T
|
|
{
|
|
type Iter = DurationProviderIter<Self>;
|
|
#[inline(always)]
|
|
fn get_all_durations(self) -> Self::Iter {
|
|
DurationProviderIter(self)
|
|
}
|
|
}
|
|
|
|
impl<'a, T: ?Sized + DurationProvider + 'a> DurationProviderExt<'a> for T
|
|
{
|
|
#[inline]
|
|
fn get_all_durations_dyn(self: Box<Self>) -> Box<dyn iter::Iterator<Item=Duration> + 'a>
|
|
{
|
|
#[derive(Debug)]
|
|
struct Iter<T: ?Sized>(Box<T>);
|
|
impl<T: DurationProvider+?Sized> Iterator for Iter<T>
|
|
{
|
|
type Item = Duration;
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
self.0.get_next_duration()
|
|
}
|
|
}
|
|
|
|
Box::new(Iter(self))
|
|
}
|
|
}
|
|
|
|
impl DurationProvider for Duration
|
|
{
|
|
#[inline(always)]
|
|
fn get_next_duration(&mut self) -> Option<Duration> {
|
|
Some(*self)
|
|
}
|
|
}
|
|
|
|
pub trait ThrottleProvider
|
|
{
|
|
type Timer: DurationProvider;
|
|
fn get_timeout(&self) -> Self::Timer;
|
|
}
|
|
|
|
|
|
pub trait DynThrottleProvider
|
|
{
|
|
fn get_timeout(&self) -> Box<dyn DurationProvider + '_>;
|
|
}
|
|
impl<T: ?Sized> DynThrottleProvider for T
|
|
where T: ThrottleProvider
|
|
{
|
|
#[inline(always)]
|
|
fn get_timeout(&self) -> Box<dyn DurationProvider + '_>
|
|
{
|
|
Box::new(ThrottleProvider::get_timeout(&self))
|
|
}
|
|
}
|
|
|
|
impl<'a, T: ?Sized> ThrottleProvider for &'a T
|
|
where T: ThrottleProvider
|
|
{
|
|
type Timer = T::Timer;
|
|
#[inline(always)]
|
|
fn get_timeout(&self) -> Self::Timer {
|
|
T::get_timeout(self)
|
|
}
|
|
}
|
|
|
|
impl ThrottleProvider for Duration
|
|
{
|
|
type Timer = Self;
|
|
fn get_timeout(&self) -> Self::Timer {
|
|
self.clone()
|
|
}
|
|
}
|
|
|
|
/// Provides a throttle adaptor that returns a uniformly-distributed `T` each time `get_timeout()` is called.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
pub struct UniformThrottleProvider<T: ?Sized, R: std::ops::RangeBounds<T>>(R, std::marker::PhantomData<T>);
|
|
|
|
impl<T: ?Sized, R> From<R> for UniformThrottleProvider<T, R>
|
|
where T: DurationProvider,
|
|
R: std::ops::RangeBounds<T>
|
|
{
|
|
#[inline(always)]
|
|
fn from(from: R) -> Self
|
|
{
|
|
Self(from, std::marker::PhantomData)
|
|
}
|
|
}
|
|
|
|
impl<T, R> ThrottleProvider for UniformThrottleProvider<T, R>
|
|
where T: DurationProvider + rand::distributions::uniform::SampleUniform,
|
|
R: std::ops::RangeBounds<T>,
|
|
for <'r> &'r R: rand::distributions::uniform::SampleRange<T>,
|
|
{
|
|
type Timer = T;
|
|
#[inline]
|
|
fn get_timeout(&self) -> Self::Timer {
|
|
use rand::prelude::*;
|
|
rand::thread_rng().gen_range(&self.0)
|
|
}
|
|
}
|