Remove more Send bounds and remove EraseViewState

This commit is contained in:
Antonio Scandurra 2023-11-02 09:38:28 +01:00
parent b2c7ddc41f
commit 2fb4c04fc3
11 changed files with 26 additions and 193 deletions

View file

@ -4,7 +4,7 @@ use collections::{HashMap, HashSet};
use serde::Deserialize;
use std::any::{type_name, Any};
pub trait Action: Any + Send {
pub trait Action: 'static {
fn qualified_name() -> SharedString
where
Self: Sized;
@ -19,7 +19,7 @@ pub trait Action: Any + Send {
impl<A> Action for A
where
A: for<'a> Deserialize<'a> + PartialEq + Any + Send + Clone + Default,
A: for<'a> Deserialize<'a> + PartialEq + Clone + Default + 'static,
{
fn qualified_name() -> SharedString {
type_name::<A>().into()

View file

@ -685,7 +685,7 @@ impl AppContext {
pub fn observe_release<E, T>(
&mut self,
handle: &E,
on_release: impl FnOnce(&mut T, &mut AppContext) + Send + 'static,
on_release: impl FnOnce(&mut T, &mut AppContext) + 'static,
) -> Subscription
where
E: Entity<T>,

View file

@ -163,7 +163,7 @@ impl AsyncWindowContext {
self.app.update_window(self.window, update)
}
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + 'static) {
self.window.update(self, |_, cx| cx.on_next_frame(f)).ok();
}
@ -184,13 +184,10 @@ impl AsyncWindowContext {
self.window.update(self, |_, cx| cx.update_global(update))
}
pub fn spawn<Fut, R>(
&self,
f: impl FnOnce(AsyncWindowContext) -> Fut + Send + 'static,
) -> Task<R>
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncWindowContext) -> Fut + 'static) -> Task<R>
where
Fut: Future<Output = R> + Send + 'static,
R: Send + 'static,
Fut: Future<Output = R> + 'static,
R: 'static,
{
let this = self.clone();
self.foreground_executor.spawn(async move { f(this).await })

View file

@ -451,9 +451,6 @@ pub struct WeakModel<T> {
entity_type: PhantomData<T>,
}
unsafe impl<T> Send for WeakModel<T> {}
unsafe impl<T> Sync for WeakModel<T> {}
impl<T> Clone for WeakModel<T> {
fn clone(&self) -> Self {
Self {

View file

@ -8,7 +8,7 @@ use std::{
sync::atomic::{AtomicUsize, Ordering::SeqCst},
};
pub trait AssetSource: 'static + Send + Sync {
pub trait AssetSource: 'static {
fn load(&self, path: &str) -> Result<Cow<[u8]>>;
fn list(&self, path: &str) -> Result<Vec<SharedString>>;
}

View file

@ -305,7 +305,6 @@ where
impl<V, I, F> Component<V> for Div<V, I, F>
where
// V: Any + Send + Sync,
I: ElementInteraction<V>,
F: ElementFocus<V>,
{

View file

@ -44,9 +44,6 @@ pub struct Text<V> {
state_type: PhantomData<V>,
}
unsafe impl<V> Send for Text<V> {}
unsafe impl<V> Sync for Text<V> {}
impl<V: 'static> Component<V> for Text<V> {
fn render(self) -> AnyElement<V> {
AnyElement::new(self)

View file

@ -8,7 +8,7 @@ use smallvec::SmallVec;
pub type FocusListeners<V> = SmallVec<[FocusListener<V>; 2]>;
pub type FocusListener<V> =
Box<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + Send + 'static>;
Box<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + 'static>;
pub trait Focusable<V: 'static>: Element<V> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V>;
@ -42,7 +42,7 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_focus(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
@ -58,7 +58,7 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_blur(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
@ -74,7 +74,7 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_focus_in(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
@ -99,7 +99,7 @@ pub trait Focusable<V: 'static>: Element<V> {
fn on_focus_out(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
@ -122,7 +122,7 @@ pub trait Focusable<V: 'static>: Element<V> {
}
}
pub trait ElementFocus<V: 'static>: 'static + Send {
pub trait ElementFocus<V: 'static>: 'static {
fn as_focusable(&self) -> Option<&FocusEnabled<V>>;
fn as_focusable_mut(&mut self) -> Option<&mut FocusEnabled<V>>;

View file

@ -117,7 +117,7 @@ pub trait VisualContext: Context {
}
pub trait Entity<T>: Sealed {
type Weak: 'static + Send;
type Weak: 'static;
fn entity_id(&self) -> EntityId;
fn downgrade(&self) -> Self::Weak;
@ -137,7 +137,7 @@ pub trait BorrowAppContext {
where
F: FnOnce(&mut Self) -> R;
fn set_global<T: Send + 'static>(&mut self, global: T);
fn set_global<T: 'static>(&mut self, global: T);
}
impl<C> BorrowAppContext for C
@ -154,7 +154,7 @@ where
result
}
fn set_global<G: 'static + Send>(&mut self, global: G) {
fn set_global<G: 'static>(&mut self, global: G) {
self.borrow_mut().set_global(global)
}
}

View file

@ -7,7 +7,6 @@ use anyhow::{Context, Result};
use std::{
any::{Any, TypeId},
hash::{Hash, Hasher},
marker::PhantomData,
};
pub trait Render: 'static + Sized {
@ -90,53 +89,7 @@ impl<V> Eq for View<V> {}
impl<V: Render, ParentViewState: 'static> Component<ParentViewState> for View<V> {
fn render(self) -> AnyElement<ParentViewState> {
AnyElement::new(EraseViewState {
view: self,
parent_view_state_type: PhantomData,
})
}
}
impl<V> Element<()> for View<V>
where
V: Render,
{
type ElementState = AnyElement<V>;
fn id(&self) -> Option<crate::ElementId> {
Some(ElementId::View(self.model.entity_id))
}
fn initialize(
&mut self,
_: &mut (),
_: Option<Self::ElementState>,
cx: &mut ViewContext<()>,
) -> Self::ElementState {
self.update(cx, |state, cx| {
let mut any_element = AnyElement::new(state.render(cx));
any_element.initialize(state, cx);
any_element
})
}
fn layout(
&mut self,
_: &mut (),
element: &mut Self::ElementState,
cx: &mut ViewContext<()>,
) -> LayoutId {
self.update(cx, |state, cx| element.layout(state, cx))
}
fn paint(
&mut self,
_: Bounds<Pixels>,
_: &mut (),
element: &mut Self::ElementState,
cx: &mut ViewContext<()>,
) {
self.update(cx, |state, cx| element.paint(state, cx))
AnyElement::new(AnyView::from(self))
}
}
@ -185,116 +138,6 @@ impl<V> PartialEq for WeakView<V> {
impl<V> Eq for WeakView<V> {}
struct EraseViewState<V, ParentV> {
view: View<V>,
parent_view_state_type: PhantomData<ParentV>,
}
unsafe impl<V, ParentV> Send for EraseViewState<V, ParentV> {}
impl<V: Render, ParentV: 'static> Component<ParentV> for EraseViewState<V, ParentV> {
fn render(self) -> AnyElement<ParentV> {
AnyElement::new(self)
}
}
impl<V: Render, ParentV: 'static> Element<ParentV> for EraseViewState<V, ParentV> {
type ElementState = Box<dyn Any>;
fn id(&self) -> Option<ElementId> {
Element::id(&self.view)
}
fn initialize(
&mut self,
_: &mut ParentV,
_: Option<Self::ElementState>,
cx: &mut ViewContext<ParentV>,
) -> Self::ElementState {
ViewObject::initialize(&mut self.view, cx)
}
fn layout(
&mut self,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<ParentV>,
) -> LayoutId {
ViewObject::layout(&mut self.view, element, cx)
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<ParentV>,
) {
ViewObject::paint(&mut self.view, bounds, element, cx)
}
}
trait ViewObject: Send + Sync {
fn entity_type(&self) -> TypeId;
fn entity_id(&self) -> EntityId;
fn model(&self) -> AnyModel;
fn initialize(&self, cx: &mut WindowContext) -> AnyBox;
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
fn paint(&self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;
}
impl<V> ViewObject for View<V>
where
V: Render,
{
fn entity_type(&self) -> TypeId {
TypeId::of::<V>()
}
fn entity_id(&self) -> EntityId {
Entity::entity_id(self)
}
fn model(&self) -> AnyModel {
self.model.clone().into_any()
}
fn initialize(&self, cx: &mut WindowContext) -> AnyBox {
cx.with_element_id(ViewObject::entity_id(self), |_global_id, cx| {
self.update(cx, |state, cx| {
let mut any_element = Box::new(AnyElement::new(state.render(cx)));
any_element.initialize(state, cx);
any_element
})
})
}
fn layout(&self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId {
cx.with_element_id(ViewObject::entity_id(self), |_global_id, cx| {
self.update(cx, |state, cx| {
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
element.layout(state, cx)
})
})
}
fn paint(&self, _: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext) {
cx.with_element_id(ViewObject::entity_id(self), |_global_id, cx| {
self.update(cx, |state, cx| {
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
element.paint(state, cx);
});
});
}
fn debug(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(&format!("AnyView<{}>", std::any::type_name::<V>()))
.field("entity_id", &ViewObject::entity_id(self).as_u64())
.finish()
}
}
#[derive(Clone, Debug)]
pub struct AnyView {
model: AnyModel,

View file

@ -362,7 +362,7 @@ impl<'a> WindowContext<'a> {
/// Schedules the given function to be run at the end of the current effect cycle, allowing entities
/// that are currently on the stack to be returned to the app.
pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static + Send) {
pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static) {
let handle = self.window.handle;
self.app.defer(move |cx| {
handle.update(cx, |_, cx| f(cx)).ok();
@ -372,7 +372,7 @@ impl<'a> WindowContext<'a> {
pub fn subscribe<Emitter, E>(
&mut self,
entity: &E,
mut on_event: impl FnMut(E, &Emitter::Event, &mut WindowContext<'_>) + Send + 'static,
mut on_event: impl FnMut(E, &Emitter::Event, &mut WindowContext<'_>) + 'static,
) -> Subscription
where
Emitter: EventEmitter,
@ -406,7 +406,7 @@ impl<'a> WindowContext<'a> {
}
/// Schedule the given closure to be run directly after the current frame is rendered.
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + 'static) {
let f = Box::new(f);
let display_id = self.window.display_id;
@ -1144,7 +1144,7 @@ impl<'a> WindowContext<'a> {
/// is updated.
pub fn observe_global<G: 'static>(
&mut self,
f: impl Fn(&mut WindowContext<'_>) + Send + 'static,
f: impl Fn(&mut WindowContext<'_>) + 'static,
) -> Subscription {
let window_handle = self.window.handle;
self.global_observers.insert(
@ -1578,9 +1578,9 @@ impl<'a, V: 'static> ViewContext<'a, V> {
result
}
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + Send + 'static)
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + 'static)
where
V: Any + Send,
V: 'static,
{
let view = self.view();
self.window_cx.on_next_frame(move |cx| view.update(cx, f));
@ -1588,7 +1588,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// Schedules the given function to be run at the end of the current effect cycle, allowing entities
/// that are currently on the stack to be returned to the app.
pub fn defer(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + 'static + Send) {
pub fn defer(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + 'static) {
let view = self.view().downgrade();
self.window_cx.defer(move |cx| {
view.update(cx, f).ok();
@ -1602,7 +1602,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
) -> Subscription
where
V2: 'static,
V: 'static + Send,
V: 'static,
E: Entity<V2>,
{
let view = self.view().downgrade();