sailfish/sailfish/src/runtime/size_hint.rs

34 lines
782 B
Rust
Raw Normal View History

2020-06-04 16:39:33 -04:00
use std::sync::atomic::{AtomicUsize, Ordering};
2020-06-06 06:16:08 -04:00
/// Dynamic size hint
2020-06-04 16:39:33 -04:00
#[derive(Debug, Default)]
pub struct SizeHint {
value: AtomicUsize,
}
impl SizeHint {
pub const fn new() -> SizeHint {
SizeHint {
value: AtomicUsize::new(0),
}
}
/// Get the current value
#[inline]
pub fn get(&self) -> usize {
self.value.load(Ordering::Acquire)
}
/// Update size hint based on given value.
///
/// There is no guarantee that the value of get() after calling update() is same
/// as the value passed on update()
#[inline]
pub fn update(&self, mut value: usize) {
value = value + value / 8;
if self.get() < value {
self.value.store(value, Ordering::Release);
}
}
}