1
0
Fork 0
forked from wry/wry

autocommit 2022-04-05 21:08:07 CEST

This commit is contained in:
Julian Orth 2022-04-05 21:08:07 +02:00
parent a3e9f21fc5
commit 1d33088dba
27 changed files with 982 additions and 699 deletions

View file

@ -1,5 +1,6 @@
use crate::utils::linkedlist::NodeRef;
use crate::utils::ptr_ext::{MutPtrExt, PtrExt};
use jay_config::keyboard::mods::Modifiers;
use std::cell::UnsafeCell;
use std::fmt::{Debug, Formatter};
use std::mem;
@ -65,3 +66,6 @@ unsafe impl<T: ?Sized> UnsafeCellCloneSafe for Weak<T> {}
unsafe impl<T> UnsafeCellCloneSafe for NodeRef<T> {}
unsafe impl UnsafeCellCloneSafe for () {}
unsafe impl UnsafeCellCloneSafe for u64 {}
unsafe impl UnsafeCellCloneSafe for Modifiers {}

View file

@ -1,12 +1,15 @@
use crate::utils::clonecell::UnsafeCellCloneSafe;
use crate::utils::ptr_ext::{MutPtrExt, PtrExt};
use ahash::AHashMap;
use std::borrow::Borrow;
use std::cell::{RefCell, RefMut};
use std::cell::UnsafeCell;
use std::fmt::{Debug, Formatter};
use std::hash::Hash;
use std::mem;
use std::ops::{Deref, DerefMut};
pub struct CopyHashMap<K, V> {
map: RefCell<AHashMap<K, V>>,
map: UnsafeCell<AHashMap<K, V>>,
}
impl<K: Debug, V: Debug> Debug for CopyHashMap<K, V> {
@ -29,20 +32,22 @@ impl<K: Eq + Hash, V> CopyHashMap<K, V> {
}
pub fn set(&self, k: K, v: V) {
self.map.borrow_mut().insert(k, v);
unsafe {
self.map.get().deref_mut().insert(k, v);
}
}
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<V>
where
V: Clone,
V: UnsafeCellCloneSafe,
Q: Hash + Eq,
K: Borrow<Q>,
{
self.map.borrow_mut().get(k).cloned()
unsafe { self.map.get().deref().get(k).cloned() }
}
pub fn remove(&self, k: &K) -> Option<V> {
self.map.borrow_mut().remove(k)
unsafe { self.map.get().deref_mut().remove(k) }
}
pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool
@ -50,18 +55,48 @@ impl<K: Eq + Hash, V> CopyHashMap<K, V> {
Q: Hash + Eq,
K: Borrow<Q>,
{
self.map.borrow_mut().contains_key(k)
unsafe { self.map.get().deref().contains_key(k) }
}
pub fn lock(&self) -> RefMut<AHashMap<K, V>> {
self.map.borrow_mut()
pub fn lock(&self) -> Locked<K, V> {
Locked {
source: self,
map: self.clear(),
}
}
pub fn clear(&self) {
mem::take(&mut *self.map.borrow_mut());
pub fn clear(&self) -> AHashMap<K, V> {
unsafe { mem::take(self.map.get().deref_mut()) }
}
pub fn is_empty(&self) -> bool {
self.map.borrow_mut().is_empty()
unsafe { self.map.get().deref().is_empty() }
}
}
pub struct Locked<'a, K, V> {
source: &'a CopyHashMap<K, V>,
map: AHashMap<K, V>,
}
impl<'a, K, V> Drop for Locked<'a, K, V> {
fn drop(&mut self) {
unsafe {
mem::swap(&mut self.map, self.source.map.get().deref_mut());
}
}
}
impl<'a, K, V> Deref for Locked<'a, K, V> {
type Target = AHashMap<K, V>;
fn deref(&self) -> &Self::Target {
&self.map
}
}
impl<'a, K, V> DerefMut for Locked<'a, K, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.map
}
}

View file

@ -1,17 +1,26 @@
use std::fmt::{Debug, Formatter};
use std::fmt::{Debug, Display, Formatter};
pub fn debug_fn<F>(f: F) -> impl Debug
pub fn debug_fn<F>(f: F) -> Printable<F>
where
F: Fn(&mut Formatter<'_>) -> std::fmt::Result,
{
DebugFn { f }
Printable { f }
}
struct DebugFn<F> {
pub struct Printable<F> {
f: F,
}
impl<F> Debug for DebugFn<F>
impl<F> Debug for Printable<F>
where
F: Fn(&mut Formatter<'_>) -> std::fmt::Result,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
(self.f)(f)
}
}
impl<F> Display for Printable<F>
where
F: Fn(&mut Formatter<'_>) -> std::fmt::Result,
{