refcount ids and delete unused
This commit is contained in:
147
src/layout/id.rs
Normal file
147
src/layout/id.rs
Normal file
@@ -0,0 +1,147 @@
|
||||
use std::{
|
||||
any::TypeId,
|
||||
marker::PhantomData,
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicU32, Ordering},
|
||||
mpsc::Sender,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{FnTag, Ui, Widget, WidgetLike, WidgetTag, util::Id};
|
||||
|
||||
pub struct AnyWidget;
|
||||
|
||||
/// An identifier for a widget that can index a UI to get the associated widget.
|
||||
/// It should always remain valid; it keeps a ref count and removes the widget from the UI if all
|
||||
/// references are dropped.
|
||||
///
|
||||
/// W does not need to implement widget so that AnyWidget is valid;
|
||||
/// Instead, add generic bounds on methods that take an ID if they need specific data.
|
||||
#[repr(C)]
|
||||
pub struct WidgetId<W = AnyWidget> {
|
||||
pub(super) ty: TypeId,
|
||||
pub(super) id: Id,
|
||||
pub(super) refcount: Arc<AtomicU32>,
|
||||
pub(super) send: Sender<Id>,
|
||||
_pd: PhantomData<W>,
|
||||
}
|
||||
|
||||
impl<W> std::fmt::Debug for WidgetId<W> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.id.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> Clone for WidgetId<W> {
|
||||
fn clone(&self) -> Self {
|
||||
self.refcount.fetch_add(1, Ordering::Release);
|
||||
Self {
|
||||
id: self.id.duplicate(),
|
||||
ty: self.ty,
|
||||
refcount: self.refcount.clone(),
|
||||
send: self.send.clone(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> WidgetId<W> {
|
||||
pub(super) fn new(id: Id, ty: TypeId, send: Sender<Id>) -> Self {
|
||||
Self {
|
||||
ty,
|
||||
id,
|
||||
refcount: AtomicU32::new(0).into(),
|
||||
send,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn erase_type(self) -> WidgetId<AnyWidget> {
|
||||
self.cast_type()
|
||||
}
|
||||
|
||||
pub fn as_any(&self) -> &WidgetId<AnyWidget> {
|
||||
// safety: self is repr(C) and generic only used for phantom data
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub(super) fn cast_type<W2>(self) -> WidgetId<W2> {
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub fn refs(&self) -> u32 {
|
||||
self.refcount.load(Ordering::Acquire)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> Drop for WidgetId<W> {
|
||||
fn drop(&mut self) {
|
||||
let refs = self.refcount.fetch_sub(1, Ordering::Release);
|
||||
if refs == 0 {
|
||||
let _ = self.send.send(self.id.duplicate());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IdTag;
|
||||
|
||||
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
|
||||
macro_rules! WidgetIdFnRet {
|
||||
($W:ty, $Ctx:ty) => {
|
||||
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W>
|
||||
};
|
||||
($W:ty, $Ctx:ty, $($use:tt)*) => {
|
||||
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W> + use<$($use)*>
|
||||
};
|
||||
}
|
||||
pub(crate) use WidgetIdFnRet;
|
||||
|
||||
pub trait Idable<Ctx, Tag> {
|
||||
type Widget: Widget<Ctx>;
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
|
||||
fn id<'a>(
|
||||
self,
|
||||
id: &WidgetId<Self::Widget>,
|
||||
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let id = id.clone();
|
||||
move |ui| {
|
||||
self.set(ui, &id);
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
|
||||
type Widget = W;
|
||||
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
||||
ui.set(id, self);
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
|
||||
type Widget = W;
|
||||
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
||||
let w = self(ui);
|
||||
ui.set(id, w);
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: 'static, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user