Reduce the need for Send + Sync bounds on generics

This commit is contained in:
Nathan Sobo 2023-10-24 14:20:01 +02:00
parent 4d621f355d
commit 0910760b76
17 changed files with 405 additions and 362 deletions

View file

@ -6,12 +6,12 @@ use crate::{
};
use std::{marker::PhantomData, sync::Arc};
pub struct View<V: Send + Sync> {
pub struct View<V> {
state: Handle<V>,
render: Arc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyElement<V> + Send + Sync + 'static>,
}
impl<V: 'static + Send + Sync> View<V> {
impl<V: 'static> View<V> {
pub fn into_any(self) -> AnyView {
AnyView {
view: Arc::new(Mutex::new(self)),
@ -19,7 +19,7 @@ impl<V: 'static + Send + Sync> View<V> {
}
}
impl<V: Send + Sync> Clone for View<V> {
impl<V> Clone for View<V> {
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
@ -34,7 +34,6 @@ pub fn view<V, E>(
) -> View<V>
where
E: IntoAnyElement<V>,
V: 'static + Send + Sync,
{
View {
state,
@ -42,9 +41,7 @@ where
}
}
impl<V: 'static + Send + Sync, ParentViewState: 'static + Send + Sync>
IntoAnyElement<ParentViewState> for View<V>
{
impl<V: 'static, ParentViewState: 'static> IntoAnyElement<ParentViewState> for View<V> {
fn into_any(self) -> AnyElement<ParentViewState> {
AnyElement::new(EraseViewState {
view: self,
@ -53,7 +50,7 @@ impl<V: 'static + Send + Sync, ParentViewState: 'static + Send + Sync>
}
}
impl<V: 'static + Send + Sync> Element for View<V> {
impl<V: 'static> Element for View<V> {
type ViewState = ();
type ElementState = AnyElement<V>;
@ -94,26 +91,21 @@ impl<V: 'static + Send + Sync> Element for View<V> {
}
}
struct EraseViewState<V: 'static + Send + Sync, ParentV> {
struct EraseViewState<V, ParentV> {
view: View<V>,
parent_view_state_type: PhantomData<ParentV>,
}
impl<V, ParentV> IntoAnyElement<ParentV> for EraseViewState<V, ParentV>
where
V: 'static + Send + Sync,
ParentV: 'static + Send + Sync,
{
unsafe impl<V, ParentV> Send for EraseViewState<V, ParentV> {}
unsafe impl<V, ParentV> Sync for EraseViewState<V, ParentV> {}
impl<V: 'static, ParentV: 'static> IntoAnyElement<ParentV> for EraseViewState<V, ParentV> {
fn into_any(self) -> AnyElement<ParentV> {
AnyElement::new(self)
}
}
impl<V, ParentV> Element for EraseViewState<V, ParentV>
where
V: 'static + Send + Sync,
ParentV: 'static + Send + Sync,
{
impl<V: 'static, ParentV: 'static> Element for EraseViewState<V, ParentV> {
type ViewState = ParentV;
type ElementState = AnyBox;
@ -150,14 +142,14 @@ where
}
}
trait ViewObject: 'static + Send + Sync {
trait ViewObject: Send + Sync {
fn entity_id(&self) -> EntityId;
fn initialize(&mut self, cx: &mut WindowContext) -> AnyBox;
fn layout(&mut self, element: &mut AnyBox, cx: &mut WindowContext) -> LayoutId;
fn paint(&mut self, bounds: Bounds<Pixels>, element: &mut AnyBox, cx: &mut WindowContext);
}
impl<V: Send + Sync + 'static> ViewObject for View<V> {
impl<V: 'static> ViewObject for View<V> {
fn entity_id(&self) -> EntityId {
self.state.entity_id
}
@ -195,10 +187,7 @@ pub struct AnyView {
view: Arc<Mutex<dyn ViewObject>>,
}
impl<ParentV> IntoAnyElement<ParentV> for AnyView
where
ParentV: 'static + Send + Sync,
{
impl<ParentV: 'static> IntoAnyElement<ParentV> for AnyView {
fn into_any(self) -> AnyElement<ParentV> {
AnyElement::new(EraseAnyViewState {
view: self,
@ -249,19 +238,16 @@ struct EraseAnyViewState<ParentViewState> {
parent_view_state_type: PhantomData<ParentViewState>,
}
impl<ParentV> IntoAnyElement<ParentV> for EraseAnyViewState<ParentV>
where
ParentV: 'static + Send + Sync,
{
unsafe impl<ParentV> Send for EraseAnyViewState<ParentV> {}
unsafe impl<ParentV> Sync for EraseAnyViewState<ParentV> {}
impl<ParentV: 'static> IntoAnyElement<ParentV> for EraseAnyViewState<ParentV> {
fn into_any(self) -> AnyElement<ParentV> {
AnyElement::new(self)
}
}
impl<ParentV> Element for EraseAnyViewState<ParentV>
where
ParentV: 'static + Send + Sync,
{
impl<ParentV: 'static> Element for EraseAnyViewState<ParentV> {
type ViewState = ParentV;
type ElementState = AnyBox;