diff --git a/Cargo.lock b/Cargo.lock index 37595cd7..5c47814d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -981,7 +981,7 @@ checksum = "0bd4b30a6560bbd9b4620f4de34c3f14f60848e58a9b7216801afcb4c7b31c3c" [[package]] name = "dyn-any" -version = "0.2.1" +version = "0.3.1" dependencies = [ "dyn-any-derive", "glam", @@ -990,7 +990,7 @@ dependencies = [ [[package]] name = "dyn-any-derive" -version = "0.2.1" +version = "0.3.0" dependencies = [ "dyn-any", "proc-macro2", diff --git a/libraries/bezier-rs/Cargo.toml b/libraries/bezier-rs/Cargo.toml index 08f6376a..b2658b03 100644 --- a/libraries/bezier-rs/Cargo.toml +++ b/libraries/bezier-rs/Cargo.toml @@ -16,5 +16,5 @@ documentation = "https://graphite.rs/libraries/bezier-rs/" [dependencies] glam = { version = "0.22", features = ["serde"] } -dyn-any = { version = "0.2.1", path = "../dyn-any", optional = true } +dyn-any = { version = "0.3.0", path = "../dyn-any", optional = true } serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/libraries/bezier-rs/src/bezier/mod.rs b/libraries/bezier-rs/src/bezier/mod.rs index 52612509..86914240 100644 --- a/libraries/bezier-rs/src/bezier/mod.rs +++ b/libraries/bezier-rs/src/bezier/mod.rs @@ -33,7 +33,7 @@ enum BezierHandles { } #[cfg(feature = "dyn-any")] -impl dyn_any::StaticType for BezierHandles { +unsafe impl dyn_any::StaticType for BezierHandles { type Static = BezierHandles; } @@ -63,6 +63,6 @@ impl Debug for Bezier { } #[cfg(feature = "dyn-any")] -impl dyn_any::StaticType for Bezier { +unsafe impl dyn_any::StaticType for Bezier { type Static = Bezier; } diff --git a/libraries/bezier-rs/src/subpath/mod.rs b/libraries/bezier-rs/src/subpath/mod.rs index 449d1104..dfb8f12d 100644 --- a/libraries/bezier-rs/src/subpath/mod.rs +++ b/libraries/bezier-rs/src/subpath/mod.rs @@ -20,7 +20,7 @@ pub struct Subpath { } #[cfg(feature = "dyn-any")] -impl dyn_any::StaticType for Subpath { +unsafe impl dyn_any::StaticType for Subpath { type Static = Subpath; } diff --git a/libraries/bezier-rs/src/subpath/structs.rs b/libraries/bezier-rs/src/subpath/structs.rs index e553cad0..74ef188f 100644 --- a/libraries/bezier-rs/src/subpath/structs.rs +++ b/libraries/bezier-rs/src/subpath/structs.rs @@ -46,7 +46,7 @@ impl Hash for ManipulatorGroup dyn_any::StaticType for ManipulatorGroup { +unsafe impl dyn_any::StaticType for ManipulatorGroup { type Static = ManipulatorGroup; } diff --git a/libraries/dyn-any/Cargo.toml b/libraries/dyn-any/Cargo.toml index 7e398cba..b6347340 100644 --- a/libraries/dyn-any/Cargo.toml +++ b/libraries/dyn-any/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dyn-any" -version = "0.2.1" +version = "0.3.1" rust-version = "1.66.0" edition = "2021" authors = ["Graphite Authors "] @@ -8,11 +8,11 @@ description = "An Any trait that works for arbitrary lifetimes" license = "MIT OR Apache-2.0" readme = "./README.md" homepage = "https://graphite.rs/libraries/dyn-any" -repository = "https://github.com/GraphiteEditor/Graphite/libraries/dyn-any" +repository = "https://github.com/GraphiteEditor/Graphite/tree/master/libraries/dyn-any" documentation = "https://docs.rs/dyn-any" [dependencies] -dyn-any-derive = { path = "derive", version = "0.2.0", optional = true } +dyn-any-derive = { path = "derive", version = "0.3.0", optional = true } log = { version = "0.4", optional = true } glam = { version = "0.22", optional = true, default-features = false } diff --git a/libraries/dyn-any/derive/Cargo.toml b/libraries/dyn-any/derive/Cargo.toml index d4e357cc..8bc8adeb 100644 --- a/libraries/dyn-any/derive/Cargo.toml +++ b/libraries/dyn-any/derive/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "dyn-any-derive" -version = "0.2.1" +version = "0.3.0" edition = "2021" authors = ["Graphite Authors "] description = "#[derive(DynAny)]" documentation = "https://docs.rs/dyn-any-derive" -repository = "https://github.com/GraphiteEditor/Graphite" +repository = "https://github.com/GraphiteEditor/Graphite/tree/master/libraries/dyn-any/derive" license = "MIT OR Apache-2.0" readme = "../README.md" @@ -16,7 +16,13 @@ proc-macro = true [dependencies] proc-macro2 = "1" quote = "1" -syn = { version = "1", default-features = false, features = ["derive", "parsing", "proc-macro", "printing"] } +syn = { version = "1", default-features = false, features = [ + "derive", + "parsing", + "proc-macro", + "printing", + "clone-impls", +] } [dev-dependencies] dyn-any = { path = "..", features = ["derive"] } diff --git a/libraries/dyn-any/derive/src/lib.rs b/libraries/dyn-any/derive/src/lib.rs index 64e16a46..73a72088 100644 --- a/libraries/dyn-any/derive/src/lib.rs +++ b/libraries/dyn-any/derive/src/lib.rs @@ -46,7 +46,7 @@ pub fn system_desc_derive(input: TokenStream) -> TokenStream { let old_params = &generics.params.iter().collect::>(); quote! { - impl<'dyn_any, #(#old_params,)*> StaticType for #struct_name <#(#dyn_params,)*> { + unsafe impl<'dyn_any, #(#old_params,)*> StaticType for #struct_name <#(#dyn_params,)*> { type Static = #struct_name <#(#static_params,)*>; } } diff --git a/libraries/dyn-any/src/lib.rs b/libraries/dyn-any/src/lib.rs index 94662ccd..8974d640 100644 --- a/libraries/dyn-any/src/lib.rs +++ b/libraries/dyn-any/src/lib.rs @@ -104,32 +104,32 @@ pub fn downcast<'a, V: StaticType>(i: Box + 'a>) -> Result } } -pub trait StaticType { +pub unsafe trait StaticType { type Static: 'static + ?Sized; fn type_id(&self) -> core::any::TypeId { core::any::TypeId::of::() } } -pub trait StaticTypeSized { +pub unsafe trait StaticTypeSized { type Static: 'static; fn type_id(&self) -> core::any::TypeId { - core::any::TypeId::of::() + core::any::TypeId::of::<::Static>() } } -impl StaticTypeSized for T +unsafe impl StaticTypeSized for T where T::Static: Sized, { type Static = ::Static; } -pub trait StaticTypeClone { +pub unsafe trait StaticTypeClone { type Static: 'static + Clone; fn type_id(&self) -> core::any::TypeId { - core::any::TypeId::of::() + core::any::TypeId::of::<::Static>() } } -impl StaticTypeClone for T +unsafe impl StaticTypeClone for T where T::Static: Clone, { @@ -139,7 +139,7 @@ where macro_rules! impl_type { ($($id:ident$(<$($(($l:lifetime, $s:lifetime)),*|)?$($T:ident),*>)?),*) => { $( - impl< $($($T: $crate::StaticTypeSized ,)*)?> $crate::StaticType for $id $(<$($($l,)*)?$($T, )*>)?{ + unsafe impl< $($($T: $crate::StaticTypeSized ,)*)?> $crate::StaticType for $id $(<$($($l,)*)?$($T, )*>)?{ type Static = $id$(<$($($s,)*)?$(<$T as $crate::StaticTypeSized>::Static,)*>)?; } )* @@ -147,22 +147,22 @@ macro_rules! impl_type { } #[cfg(feature = "alloc")] -impl<'a, T: StaticTypeClone + Clone> StaticType for Cow<'a, T> { - type Static = Cow<'static, T::Static>; +unsafe impl<'a, T: StaticTypeClone + Clone> StaticType for Cow<'a, T> { + type Static = Cow<'static, ::Static>; } -impl StaticType for *const [T] { +unsafe impl StaticType for *const [T] { type Static = *const [::Static]; } -impl StaticType for *mut [T] { +unsafe impl StaticType for *mut [T] { type Static = *mut [::Static]; } -impl<'a, T: StaticTypeSized> StaticType for &'a [T] { +unsafe impl<'a, T: StaticTypeSized> StaticType for &'a [T] { type Static = &'static [::Static]; } macro_rules! impl_slice { ($($id:ident),*) => { $( - impl<'a, T: StaticTypeSized> StaticType for $id<'a, T> { + unsafe impl<'a, T: StaticTypeSized> StaticType for $id<'a, T> { type Static = $id<'static, ::Static>; } )* @@ -176,24 +176,24 @@ mod slice { } #[cfg(feature = "alloc")] -impl<'a, T: StaticTypeSized> StaticType for Box + 'a + Send + Sync> { - type Static = Box + Send + Sync>; +unsafe impl<'a, T: StaticTypeSized> StaticType for Box + 'a + Send + Sync> { + type Static = Box::Static> + Send + Sync>; } -impl<'a> StaticType for &'a str { +unsafe impl<'a> StaticType for &'a str { type Static = &'static str; } -impl StaticType for () { +unsafe impl StaticType for () { type Static = (); } -impl<'a, T: 'a + StaticType + ?Sized> StaticType for &'a T { +unsafe impl<'a, T: 'a + StaticType + ?Sized> StaticType for &'a T { type Static = &'static ::Static; } -impl StaticType for [T; N] { +unsafe impl StaticType for [T; N] { type Static = [::Static; N]; } -impl<'a> StaticType for dyn DynAny<'a> + '_ { +unsafe impl<'a> StaticType for dyn DynAny<'a> + '_ { type Static = dyn DynAny<'static>; } #[cfg(feature = "alloc")] @@ -275,7 +275,7 @@ impl_type!( ); #[cfg(feature = "alloc")] -impl crate::StaticType for Box { +unsafe impl crate::StaticType for Box { type Static = Box<::Static>; } #[test] @@ -293,7 +293,7 @@ macro_rules! impl_tuple { impl_tuple! { @rec $($t)* } }; (@impl $($t:ident)*) => { - impl< $($t: StaticTypeSized,)*> StaticType for ($($t,)*) { + unsafe impl< $($t: StaticTypeSized,)*> StaticType for ($($t,)*) { type Static = ($(<$t as $crate::StaticTypeSized>::Static,)*); } }; diff --git a/node-graph/borrow_stack/src/lib.rs b/node-graph/borrow_stack/src/lib.rs index ee9116ec..321544ae 100644 --- a/node-graph/borrow_stack/src/lib.rs +++ b/node-graph/borrow_stack/src/lib.rs @@ -42,7 +42,7 @@ impl<'n, T: 'n + dyn_any::StaticTypeSized> FixedSizeStack { pub fn is_empty(&self) -> bool { self.len.load(Ordering::SeqCst) == 0 } - pub fn push_fn<'a>(&self, f: impl FnOnce(&'a [T::Static]) -> T) { + pub fn push_fn<'a>(&self, f: impl FnOnce(&'a [::Static]) -> T) { assert_eq!(std::mem::size_of::(), std::mem::size_of::()); unsafe { self.push(f(self.get())) } } diff --git a/node-graph/gcore/src/raster.rs b/node-graph/gcore/src/raster.rs index 8f217b3a..0fab24d5 100644 --- a/node-graph/gcore/src/raster.rs +++ b/node-graph/gcore/src/raster.rs @@ -456,7 +456,7 @@ pub struct ImageSlice<'a, Pixel> { pub data: &'a (), } -impl StaticType for ImageSlice<'_, P> { +unsafe impl StaticType for ImageSlice<'_, P> { type Static = ImageSlice<'static, P::Static>; } @@ -585,7 +585,7 @@ mod image { pub data: Vec

, } - impl StaticType for Image

+ unsafe impl StaticType for Image

where P::Static: Pixel, { @@ -770,7 +770,7 @@ mod image { } } - impl StaticType for ImageFrame

+ unsafe impl StaticType for ImageFrame

where P::Static: Pixel, { diff --git a/node-graph/gcore/src/raster/adjustments.rs b/node-graph/gcore/src/raster/adjustments.rs index f7773fc0..a50b8451 100644 --- a/node-graph/gcore/src/raster/adjustments.rs +++ b/node-graph/gcore/src/raster/adjustments.rs @@ -358,10 +358,6 @@ pub struct BlendNode { opacity: Opacity, } -impl StaticType for BlendNode { - type Static = BlendNode; -} - #[node_macro::node_fn(BlendNode)] fn blend_node(input: (Color, Color), blend_mode: BlendMode, opacity: f64) -> Color { let opacity = opacity / 100.; diff --git a/node-graph/gcore/src/value.rs b/node-graph/gcore/src/value.rs index 1498c9dc..32286455 100644 --- a/node-graph/gcore/src/value.rs +++ b/node-graph/gcore/src/value.rs @@ -16,10 +16,6 @@ impl<'i, const N: u32> Node<'i, ()> for IntNode { #[derive(Default, Debug)] pub struct ValueNode(pub T); -impl StaticType for ValueNode { - type Static = ValueNode; -} - impl<'i, T: 'i> Node<'i, ()> for ValueNode { type Output = &'i T; fn eval<'s: 'i>(&'s self, _input: ()) -> Self::Output { @@ -48,13 +44,6 @@ impl Copy for ValueNode {} #[derive(Clone)] pub struct ClonedNode(pub T); -impl StaticType for ClonedNode -where - T::Static: Clone, -{ - type Static = ClonedNode; -} - impl<'i, T: Clone + 'i> Node<'i, ()> for ClonedNode { type Output = T; fn eval<'s: 'i>(&'s self, _input: ()) -> Self::Output { diff --git a/node-graph/gstd/src/raster.rs b/node-graph/gstd/src/raster.rs index 21a137e9..419d595e 100644 --- a/node-graph/gstd/src/raster.rs +++ b/node-graph/gstd/src/raster.rs @@ -264,10 +264,6 @@ pub struct BlendImageTupleNode { _p: PhantomData

, } -impl StaticType for BlendImageTupleNode { - type Static = BlendImageTupleNode; -} - #[node_macro::node_fn(BlendImageTupleNode<_P>)] fn blend_image_tuple<_P: Pixel + Debug, MapFn>(images: (ImageFrame<_P>, ImageFrame<_P>), map_fn: &'any_input MapFn) -> ImageFrame<_P> where @@ -286,10 +282,6 @@ pub struct BlendImageNode { _p: PhantomData

, } -impl StaticType for BlendImageNode { - type Static = BlendImageNode; -} - // TODO: Implement proper blending #[node_macro::node_fn(BlendImageNode<_P>)] fn blend_image<_P: Clone, MapFn, Frame: Sample + Transform, Background: RasterMut + Transform>(