2017-01-18 20:39:46 +00:00
|
|
|
use std::mem;
|
2016-03-16 04:07:04 +00:00
|
|
|
|
2017-01-18 20:39:46 +00:00
|
|
|
pub trait Seq {
|
|
|
|
fn next(&self) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_seq {
|
|
|
|
($($ty:ty)*) => { $(
|
|
|
|
impl Seq for $ty {
|
2020-02-23 01:27:38 +00:00
|
|
|
fn next(&self) -> Self { (*self).wrapping_add(1) }
|
2017-01-18 20:39:46 +00:00
|
|
|
}
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_seq!(u8 u16 u32 u64 usize);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
|
|
|
|
pub struct SeqGenerator<T: Seq>(T);
|
|
|
|
|
2018-02-11 11:37:08 +00:00
|
|
|
impl<T: Seq> SeqGenerator<T> {
|
2017-01-18 20:39:46 +00:00
|
|
|
pub fn new(value: T) -> Self {
|
|
|
|
SeqGenerator(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&mut self) -> T {
|
|
|
|
let value = self.0.next();
|
|
|
|
mem::replace(&mut self.0, value)
|
|
|
|
}
|
|
|
|
}
|