Rename WeakPin -> PinWeak

This commit is contained in:
Olivier Goffart 2020-07-31 18:19:26 +02:00
parent 75671c18e7
commit cb67e40c32
5 changed files with 27 additions and 27 deletions

View file

@ -143,13 +143,13 @@ Hello := Rectangle {
fn main() {
let app = Hello::new();
let app_weak = sixtyfps::re_exports::WeakPin::downgrade(app.clone());
let app_weak = sixtyfps::re_exports::PinWeak::downgrade(app.clone());
app.plus_clicked.set_handler(move |()| {
let app = app_weak.upgrade().unwrap();
let counter = Hello::field_offsets().counter.apply_pin(app.as_ref());
counter.set(counter.get() + 1);
});
let app_weak = sixtyfps::re_exports::WeakPin::downgrade(app.clone());
let app_weak = sixtyfps::re_exports::PinWeak::downgrade(app.clone());
app.minus_clicked.set_handler(move |()| {
let app = app_weak.upgrade().unwrap();
let counter = Hello::field_offsets().counter.apply_pin(app.as_ref());

View file

@ -4,13 +4,13 @@ sixtyfps::include_modules!();
fn main() {
let app = Hello::new();
let app_weak = sixtyfps::re_exports::WeakPin::downgrade(app.clone());
let app_weak = sixtyfps::re_exports::PinWeak::downgrade(app.clone());
app.plus_clicked.set_handler(move |()| {
let app = app_weak.upgrade().unwrap();
let counter = Hello::field_offsets().counter.apply_pin(app.as_ref());
counter.set(counter.get() + 1);
});
let app_weak = sixtyfps::re_exports::WeakPin::downgrade(app.clone());
let app_weak = sixtyfps::re_exports::PinWeak::downgrade(app.clone());
app.minus_clicked.set_handler(move |()| {
let app = app_weak.upgrade().unwrap();
let counter = Hello::field_offsets().counter.apply_pin(app.as_ref());

View file

@ -30,15 +30,15 @@ let x = std::mem::replace(
In that example, `x` is the original `SomeStruct` which we moved in memory,
**that is undefined behavior**, do not do that at home.
## `WeakPin`
## `PinWeak`
This crate simply provide a `rc::WeakPin` and `sync::WeakPin` which allow to
This crate simply provide a `rc::PinWeak` and `sync::PinWeak` which allow to
get weak pointer from `Pin<std::rc::Rc>` and `Pin<srd::sync::Arc>`.
This is safe because you can one can only get back a `Pin` out of it when
trying to upgrade the weak pointer.
`WeakPin` can be created using the `WeakPin` downgrade function.
`PinWeak` can be created using the `PinWeak` downgrade function.
## Example
@ -47,7 +47,7 @@ use pin_weak::rc::*;
# use std::marker::PhantomPinned;
struct SomeStruct(PhantomPinned, usize);
let pinned = Rc::pin(SomeStruct(PhantomPinned, 42));
let weak = WeakPin::downgrade(pinned.clone());
let weak = PinWeak::downgrade(pinned.clone());
assert_eq!(weak.upgrade().unwrap().1, 42);
std::mem::drop(pinned);
assert!(weak.upgrade().is_none());
@ -66,16 +66,16 @@ macro_rules! implementation {
pub use core::pin::Pin;
/// This is a safe wrapper around something that could be compared to `Pin<Weak<T>>`
///
/// The typical way to obtain a `WeakPin` is to call `WeakPin::downgrade`
/// The typical way to obtain a `PinWeak` is to call `PinWeak::downgrade`
#[derive(Debug)]
pub struct WeakPin<T: ?Sized>(Weak<T>);
impl<T> Default for WeakPin<T> {
pub struct PinWeak<T: ?Sized>(Weak<T>);
impl<T> Default for PinWeak<T> {
fn default() -> Self { Self(Weak::default()) }
}
impl<T: ?Sized> Clone for WeakPin<T> {
impl<T: ?Sized> Clone for PinWeak<T> {
fn clone(&self) -> Self { Self(self.0.clone()) }
}
impl<T: ?Sized> WeakPin<T> {
impl<T: ?Sized> PinWeak<T> {
/// Equivalent function to `
#[doc = $rc_lit]
/// ::downgrade`, but taking a `Pin<
@ -106,20 +106,20 @@ macro_rules! implementation {
}
}
let c = $Rc::pin(Foo::new(44));
let weak1 = WeakPin::downgrade(c.clone());
let weak1 = PinWeak::downgrade(c.clone());
assert_eq!(weak1.upgrade().unwrap().u, 44);
assert_eq!(weak1.clone().upgrade().unwrap().u, 44);
let weak2 = WeakPin::downgrade(c.clone());
let weak2 = PinWeak::downgrade(c.clone());
assert_eq!(weak2.upgrade().unwrap().u, 44);
assert_eq!(weak1.upgrade().unwrap().u, 44);
// note that this moves c and therefore it will be dropped
let weak3 = WeakPin::downgrade(c);
let weak3 = PinWeak::downgrade(c);
assert!(weak3.upgrade().is_none());
assert!(weak2.upgrade().is_none());
assert!(weak1.upgrade().is_none());
assert!(weak1.clone().upgrade().is_none());
let def = WeakPin::<alloc::boxed::Box<&'static mut ()>>::default();
let def = PinWeak::<alloc::boxed::Box<&'static mut ()>>::default();
assert!(def.upgrade().is_none());
assert!(def.clone().upgrade().is_none());
}

View file

@ -237,7 +237,7 @@ pub fn generate(component: &Rc<Component>, diag: &mut BuildDiagnostics) -> Optio
if matches!(item.lookup_property(k.as_str()), Type::Signal) {
init.push(quote!(
self_pinned.#rust_property.set_handler({
let self_weak = sixtyfps::re_exports::WeakPin::downgrade(self_pinned.clone());
let self_weak = sixtyfps::re_exports::PinWeak::downgrade(self_pinned.clone());
move |()| {
let self_pinned = self_weak.upgrade().unwrap();
let _self = self_pinned.as_ref();
@ -254,7 +254,7 @@ pub fn generate(component: &Rc<Component>, diag: &mut BuildDiagnostics) -> Optio
&item_rc,
k,
quote!({
let self_weak = sixtyfps::re_exports::WeakPin::downgrade(self_pinned.clone());
let self_weak = sixtyfps::re_exports::PinWeak::downgrade(self_pinned.clone());
move || {
let self_pinned = self_weak.upgrade().unwrap();
let _self = self_pinned.as_ref();
@ -360,8 +360,8 @@ pub fn generate(component: &Rc<Component>, diag: &mut BuildDiagnostics) -> Optio
#(#declared_signals : sixtyfps::re_exports::Signal<()>,)*
#(#repeated_element_names : sixtyfps::re_exports::Repeater<#repeated_element_components>,)*
#(#repeated_dynmodel_names : sixtyfps::re_exports::PropertyListenerScope,)*
self_weak: sixtyfps::re_exports::OnceCell<sixtyfps::re_exports::WeakPin<#component_id>>,
#(parent : sixtyfps::re_exports::WeakPin<#parent_component_type>,)*
self_weak: sixtyfps::re_exports::OnceCell<sixtyfps::re_exports::PinWeak<#component_id>>,
#(parent : sixtyfps::re_exports::PinWeak<#parent_component_type>,)*
}
impl sixtyfps::re_exports::Component for #component_id {
@ -382,7 +382,7 @@ pub fn generate(component: &Rc<Component>, diag: &mut BuildDiagnostics) -> Optio
}
impl #component_id{
fn new(#(parent: sixtyfps::re_exports::WeakPin::<#parent_component_type>)*)
fn new(#(parent: sixtyfps::re_exports::PinWeak::<#parent_component_type>)*)
-> core::pin::Pin<std::rc::Rc<Self>>
{
#![allow(unused)]
@ -395,10 +395,10 @@ pub fn generate(component: &Rc<Component>, diag: &mut BuildDiagnostics) -> Optio
#(#repeated_element_names : ::core::default::Default::default(),)*
#(#repeated_dynmodel_names : ::core::default::Default::default(),)*
self_weak : ::core::default::Default::default(),
#(parent : parent as sixtyfps::re_exports::WeakPin::<#parent_component_type>,)*
#(parent : parent as sixtyfps::re_exports::PinWeak::<#parent_component_type>,)*
};
let self_pinned = std::rc::Rc::pin(self_);
self_pinned.self_weak.set(WeakPin::downgrade(self_pinned.clone())).map_err(|_|())
self_pinned.self_weak.set(PinWeak::downgrade(self_pinned.clone())).map_err(|_|())
.expect("Can only be pinned once");
#(#init)*
self_pinned

View file

@ -692,7 +692,7 @@ impl<T: InterpolatedPropertyValue> BindingCallable for AnimatedBindingCallable<T
#[test]
fn properties_simple_test() {
use std::rc::Rc;
use pin_weak::rc::WeakPin;
use pin_weak::rc::PinWeak;
fn g(prop: &Property<i32>) -> i32 {
unsafe { Pin::new_unchecked(prop).get() }
}
@ -705,7 +705,7 @@ fn properties_simple_test() {
}
let compo = Rc::pin(Component::default());
let w = WeakPin::downgrade(compo.clone());
let w = PinWeak::downgrade(compo.clone());
compo.area.set_binding(move || {
let compo = w.upgrade().unwrap();
g(&compo.width) * g(&compo.height)
@ -716,7 +716,7 @@ fn properties_simple_test() {
assert_eq!(g(&compo.height), 8);
assert_eq!(g(&compo.area), 4 * 8);
let w = WeakPin::downgrade(compo.clone());
let w = PinWeak::downgrade(compo.clone());
compo.width.set_binding(move || {
let compo = w.upgrade().unwrap();
g(&compo.height) * 2