Remove ComponentHandle and ComponentWeakHandle

This commit is contained in:
Simon Hausmann 2020-12-03 08:12:40 +01:00
parent 4f412dcde4
commit 83191c82cc
2 changed files with 2 additions and 89 deletions

View file

@ -218,93 +218,6 @@ pub trait Component: re_exports::HasStaticVTable<re_exports::ComponentVTable> {
fn run(self: core::pin::Pin<&Self>);
}
/// Holds a strong reference to a component created from a .60 file.
///
/// The SixtyFPS compiler will generate component structure. The T type parameter
/// is one of these structure. In order to create a ComponentHandle, you would
/// use the `T::new()` function, which returns a `ComponentHandle<T>`.
///
/// Internally, this is like a Rc pointer.
/// You can get a weak pointer out of it using the `as_weak` function.
pub struct ComponentHandle<T> {
inner: vtable::VRc<re_exports::ComponentVTable, T>,
}
impl<T> Clone for ComponentHandle<T> {
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}
impl<T: Component> core::ops::Deref for ComponentHandle<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&*self.inner
}
}
impl<T> From<vtable::VRc<re_exports::ComponentVTable, T>> for ComponentHandle<T> {
fn from(value: vtable::VRc<re_exports::ComponentVTable, T>) -> Self {
Self { inner: value }
}
}
impl<T> From<ComponentHandle<T>> for vtable::VRc<re_exports::ComponentVTable, T> {
fn from(value: ComponentHandle<T>) -> Self {
value.inner
}
}
impl<T: Component> ComponentHandle<T> {
/// Create a weak handle to this handle.
pub fn as_weak(&self) -> ComponentWeakHandle<T> {
ComponentWeakHandle { inner: vtable::VRc::downgrade(&self.inner) }
}
/// Returns a pinned reference to this component.
pub fn as_ref(&self) -> core::pin::Pin<&T> {
vtable::VRc::as_pin_ref(&self.inner)
}
/// Show this component and run the event loop
pub fn run(self) {
self.as_ref().run()
}
}
impl<T> ComponentHandle<T> {
/// Temporary addition until this class is gone.
pub fn as_rc(&self) -> vtable::VRc<re_exports::ComponentVTable, T> {
self.inner.clone()
}
}
/// A weak handle to a component created from a .60 file.
///
/// One create a ComponentWeakHandle using the [`ComponentHandle::as_weak`] function, and one
/// can get back the original ComponentHandle using the [`Self::upgrade`] function.
pub struct ComponentWeakHandle<T> {
inner: vtable::VWeak<re_exports::ComponentVTable, T>,
}
impl<T> Clone for ComponentWeakHandle<T> {
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}
impl<T> Default for ComponentWeakHandle<T> {
fn default() -> Self {
Self { inner: Default::default() }
}
}
impl<T> ComponentWeakHandle<T> {
/// Convert this weak pointer back to an actual
pub fn upgrade(&self) -> Option<ComponentHandle<T>> {
self.inner.upgrade().map(|inner| ComponentHandle { inner })
}
}
/// This trait describes the conversion of a strongly referenced SixtyFPS component,
/// held by a [vtable::VRc] into a weak reference.
pub trait IntoWeak {

View file

@ -777,7 +777,7 @@ fn generate_component(
let self_weak = if !component.is_global() { Some(quote!(self_weak)) } else { None };
let self_weak = self_weak.into_iter().collect::<Vec<_>>();
let component_handle = if !component.is_global() {
quote!(sixtyfps::ComponentHandle<Self>)
quote!(vtable::VRc<sixtyfps::re_exports::ComponentVTable, Self>)
} else {
quote!(::core::pin::Pin<::std::rc::Rc<Self>>)
};
@ -806,7 +806,7 @@ fn generate_component(
impl #public_struct {
pub fn new(#(parent: sixtyfps::re_exports::VWeak::<sixtyfps::re_exports::ComponentVTable, #parent_component_type>)* #window_parent_param) -> Self {
Self(#component_id::new(#parent_name #window_parent_name).as_rc())
Self(#component_id::new(#parent_name #window_parent_name))
}
#(#public_property_and_signal_accessors)*