FINALLY transition events to global; slow text sending bug tho
This commit is contained in:
1 parent
2537284372
commit
baaeb6b027
21 files changed
+650
-557
No files matched your search
+45
-9
@@ -1,3 +1,5 @@
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Eq, Hash, PartialEq, Debug, Clone, Copy, bytemuck::Zeroable)]
|
||||
pub struct Id<I = u64>(I);
|
||||
@@ -9,7 +11,14 @@ pub struct IdTracker<I = u64> {
|
||||
cur: Id<I>,
|
||||
}
|
||||
|
||||
impl<I: IdNum> IdTracker<I> {
|
||||
impl<I: const IdNum> IdTracker<I> {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
free: Vec::new(),
|
||||
cur: Id(I::first()),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn next(&mut self) -> Id<I> {
|
||||
if let Some(id) = self.free.pop() {
|
||||
@@ -43,22 +52,19 @@ impl<I: IdNum> Id<I> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: IdNum> Default for IdTracker<I> {
|
||||
impl<I: const IdNum> Default for IdTracker<I> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
free: Vec::new(),
|
||||
cur: Id(I::first()),
|
||||
}
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IdNum {
|
||||
pub const trait IdNum {
|
||||
fn first() -> Self;
|
||||
fn next(&self) -> Self;
|
||||
fn idx(&self) -> usize;
|
||||
}
|
||||
|
||||
impl IdNum for u64 {
|
||||
impl const IdNum for u64 {
|
||||
fn first() -> Self {
|
||||
0
|
||||
}
|
||||
@@ -72,7 +78,7 @@ impl IdNum for u64 {
|
||||
}
|
||||
}
|
||||
|
||||
impl IdNum for u32 {
|
||||
impl const IdNum for u32 {
|
||||
fn first() -> Self {
|
||||
0
|
||||
}
|
||||
@@ -85,3 +91,33 @@ impl IdNum for u32 {
|
||||
*self as usize
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StaticIdTracker<I = u64>(Mutex<IdTracker<I>>);
|
||||
|
||||
impl<I: const IdNum> StaticIdTracker<I> {
|
||||
pub const fn new() -> Self {
|
||||
Self(Mutex::new(IdTracker::new()))
|
||||
}
|
||||
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn next(&self) -> Id<I> {
|
||||
let mut s = self.0.lock().unwrap();
|
||||
if let Some(id) = s.free.pop() {
|
||||
return id;
|
||||
}
|
||||
let next = s.cur.next();
|
||||
std::mem::replace(&mut s.cur, next)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn free(&self, id: Id<I>) {
|
||||
let mut s = self.0.lock().unwrap();
|
||||
s.free.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: const IdNum> Default for StaticIdTracker<I> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user