utils: add DoubleBuffered
This commit is contained in:
parent
c712efcd35
commit
80310f4c0d
2 changed files with 41 additions and 0 deletions
|
|
@ -12,6 +12,7 @@ pub mod clone3;
|
||||||
pub mod clonecell;
|
pub mod clonecell;
|
||||||
pub mod copyhashmap;
|
pub mod copyhashmap;
|
||||||
pub mod debug_fn;
|
pub mod debug_fn;
|
||||||
|
pub mod double_buffered;
|
||||||
pub mod double_click_state;
|
pub mod double_click_state;
|
||||||
pub mod errorfmt;
|
pub mod errorfmt;
|
||||||
pub mod event_listener;
|
pub mod event_listener;
|
||||||
|
|
|
||||||
40
src/utils/double_buffered.rs
Normal file
40
src/utils/double_buffered.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
use std::{cell::Cell, ops::Deref};
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct DoubleBuffered<T> {
|
||||||
|
bufs: [T; 2],
|
||||||
|
front: Cell<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> DoubleBuffered<T> {
|
||||||
|
#[expect(dead_code)]
|
||||||
|
pub fn new(bufs: [T; 2]) -> Self {
|
||||||
|
Self {
|
||||||
|
bufs,
|
||||||
|
front: Cell::new(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(dead_code)]
|
||||||
|
pub fn front(&self) -> &T {
|
||||||
|
unsafe { self.bufs.get_unchecked(self.front.get()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(dead_code)]
|
||||||
|
pub fn back(&self) -> &T {
|
||||||
|
unsafe { self.bufs.get_unchecked(1 - self.front.get()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(dead_code)]
|
||||||
|
pub fn flip(&self) {
|
||||||
|
self.front.set(1 - self.front.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for DoubleBuffered<T> {
|
||||||
|
type Target = [T; 2];
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.bufs
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue