mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-27 23:23:14 -04:00
Move Web backend to winit-web
This commit is contained in:
committed by
Kirill Chibisov
parent
2d4b9938f0
commit
e542a78deb
52
winit-web/src/async/concurrent_queue.rs
Normal file
52
winit-web/src/async/concurrent_queue.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ConcurrentQueue<T> {
|
||||
queue: RefCell<Vec<T>>,
|
||||
closed: Cell<bool>,
|
||||
}
|
||||
|
||||
pub enum PushError<T> {
|
||||
#[allow(dead_code)]
|
||||
Full(T),
|
||||
Closed(T),
|
||||
}
|
||||
|
||||
pub enum PopError {
|
||||
Empty,
|
||||
Closed,
|
||||
}
|
||||
|
||||
impl<T> ConcurrentQueue<T> {
|
||||
pub fn unbounded() -> Self {
|
||||
Self { queue: RefCell::new(Vec::new()), closed: Cell::new(false) }
|
||||
}
|
||||
|
||||
pub fn push(&self, value: T) -> Result<(), PushError<T>> {
|
||||
if self.closed.get() {
|
||||
return Err(PushError::Closed(value));
|
||||
}
|
||||
|
||||
self.queue.borrow_mut().push(value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn pop(&self) -> Result<T, PopError> {
|
||||
self.queue.borrow_mut().pop().ok_or_else(|| {
|
||||
if self.closed.get() {
|
||||
PopError::Closed
|
||||
} else {
|
||||
PopError::Empty
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn close(&self) -> bool {
|
||||
!self.closed.replace(true)
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY: Wasm without the `atomics` target feature is single-threaded.
|
||||
unsafe impl<T> Send for ConcurrentQueue<T> {}
|
||||
// SAFETY: Wasm without the `atomics` target feature is single-threaded.
|
||||
unsafe impl<T> Sync for ConcurrentQueue<T> {}
|
||||
Reference in New Issue
Block a user