Bump version of dyn-any to address missues which could lead to ub (#1145)

* Make StaticType trait unsafe

* Mark StaticTypeSized and StaticTypeClone as unsafe as well

* Update dyn-any dependency

* Update manifest links
This commit is contained in:
Dennis Kobert 2023-04-17 23:35:04 +02:00 committed by Keavon Chambers
parent 9db5ad43bf
commit 1d6c4f13dd
14 changed files with 47 additions and 64 deletions

4
Cargo.lock generated
View File

@ -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",

View File

@ -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 }

View File

@ -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;
}

View File

@ -20,7 +20,7 @@ pub struct Subpath<ManipulatorGroupId: crate::Identifier> {
}
#[cfg(feature = "dyn-any")]
impl<ManipulatorGroupId: crate::Identifier> dyn_any::StaticType for Subpath<ManipulatorGroupId> {
unsafe impl<ManipulatorGroupId: crate::Identifier> dyn_any::StaticType for Subpath<ManipulatorGroupId> {
type Static = Subpath<ManipulatorGroupId>;
}

View File

@ -46,7 +46,7 @@ impl<ManipulatorGroupId: crate::Identifier> Hash for ManipulatorGroup<Manipulato
}
#[cfg(feature = "dyn-any")]
impl<ManipulatorGroupId: crate::Identifier> dyn_any::StaticType for ManipulatorGroup<ManipulatorGroupId> {
unsafe impl<ManipulatorGroupId: crate::Identifier> dyn_any::StaticType for ManipulatorGroup<ManipulatorGroupId> {
type Static = ManipulatorGroup<ManipulatorGroupId>;
}

View File

@ -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 <contact@graphite.rs>"]
@ -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 }

View File

@ -1,12 +1,12 @@
[package]
name = "dyn-any-derive"
version = "0.2.1"
version = "0.3.0"
edition = "2021"
authors = ["Graphite Authors <contact@graphite.rs>"]
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"] }

View File

@ -46,7 +46,7 @@ pub fn system_desc_derive(input: TokenStream) -> TokenStream {
let old_params = &generics.params.iter().collect::<Vec<_>>();
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,)*>;
}
}

View File

@ -104,32 +104,32 @@ pub fn downcast<'a, V: StaticType>(i: Box<dyn DynAny<'a> + 'a>) -> Result<Box<V>
}
}
pub trait StaticType {
pub unsafe trait StaticType {
type Static: 'static + ?Sized;
fn type_id(&self) -> core::any::TypeId {
core::any::TypeId::of::<Self::Static>()
}
}
pub trait StaticTypeSized {
pub unsafe trait StaticTypeSized {
type Static: 'static;
fn type_id(&self) -> core::any::TypeId {
core::any::TypeId::of::<Self::Static>()
core::any::TypeId::of::<<Self as StaticTypeSized>::Static>()
}
}
impl<T: StaticType + Sized> StaticTypeSized for T
unsafe impl<T: StaticType + Sized> StaticTypeSized for T
where
T::Static: Sized,
{
type Static = <T as StaticType>::Static;
}
pub trait StaticTypeClone {
pub unsafe trait StaticTypeClone {
type Static: 'static + Clone;
fn type_id(&self) -> core::any::TypeId {
core::any::TypeId::of::<Self::Static>()
core::any::TypeId::of::<<Self as StaticTypeClone>::Static>()
}
}
impl<T: StaticType + Clone> StaticTypeClone for T
unsafe impl<T: StaticType + Clone> 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, <T as StaticTypeClone>::Static>;
}
impl<T: StaticTypeSized> StaticType for *const [T] {
unsafe impl<T: StaticTypeSized> StaticType for *const [T] {
type Static = *const [<T as StaticTypeSized>::Static];
}
impl<T: StaticTypeSized> StaticType for *mut [T] {
unsafe impl<T: StaticTypeSized> StaticType for *mut [T] {
type Static = *mut [<T as StaticTypeSized>::Static];
}
impl<'a, T: StaticTypeSized> StaticType for &'a [T] {
unsafe impl<'a, T: StaticTypeSized> StaticType for &'a [T] {
type Static = &'static [<T as StaticTypeSized>::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, <T as StaticTypeSized>::Static>;
}
)*
@ -176,24 +176,24 @@ mod slice {
}
#[cfg(feature = "alloc")]
impl<'a, T: StaticTypeSized> StaticType for Box<dyn Iterator<Item = T> + 'a + Send + Sync> {
type Static = Box<dyn Iterator<Item = T::Static> + Send + Sync>;
unsafe impl<'a, T: StaticTypeSized> StaticType for Box<dyn Iterator<Item = T> + 'a + Send + Sync> {
type Static = Box<dyn Iterator<Item = <T as StaticTypeSized>::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 <T as StaticType>::Static;
}
impl<T: StaticTypeSized, const N: usize> StaticType for [T; N] {
unsafe impl<T: StaticTypeSized, const N: usize> StaticType for [T; N] {
type Static = [<T as StaticTypeSized>::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<T: crate::StaticType + ?Sized> crate::StaticType for Box<T> {
unsafe impl<T: crate::StaticType + ?Sized> crate::StaticType for Box<T> {
type Static = Box<<T as crate::StaticType>::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,)*);
}
};

View File

@ -42,7 +42,7 @@ impl<'n, T: 'n + dyn_any::StaticTypeSized> FixedSizeStack<T> {
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 [<T as StaticTypeSized>::Static]) -> T) {
assert_eq!(std::mem::size_of::<T>(), std::mem::size_of::<T::Static>());
unsafe { self.push(f(self.get())) }
}

View File

@ -456,7 +456,7 @@ pub struct ImageSlice<'a, Pixel> {
pub data: &'a (),
}
impl<P: StaticTypeSized> StaticType for ImageSlice<'_, P> {
unsafe impl<P: StaticTypeSized> StaticType for ImageSlice<'_, P> {
type Static = ImageSlice<'static, P::Static>;
}
@ -585,7 +585,7 @@ mod image {
pub data: Vec<P>,
}
impl<P: StaticTypeSized + Pixel> StaticType for Image<P>
unsafe impl<P: StaticTypeSized + Pixel> StaticType for Image<P>
where
P::Static: Pixel,
{
@ -770,7 +770,7 @@ mod image {
}
}
impl<P: StaticTypeSized + Pixel> StaticType for ImageFrame<P>
unsafe impl<P: StaticTypeSized + Pixel> StaticType for ImageFrame<P>
where
P::Static: Pixel,
{

View File

@ -358,10 +358,6 @@ pub struct BlendNode<BlendMode, Opacity> {
opacity: Opacity,
}
impl<Opacity: dyn_any::StaticTypeSized, Blend: dyn_any::StaticTypeSized> StaticType for BlendNode<Blend, Opacity> {
type Static = BlendNode<Blend::Static, Opacity::Static>;
}
#[node_macro::node_fn(BlendNode)]
fn blend_node(input: (Color, Color), blend_mode: BlendMode, opacity: f64) -> Color {
let opacity = opacity / 100.;

View File

@ -16,10 +16,6 @@ impl<'i, const N: u32> Node<'i, ()> for IntNode<N> {
#[derive(Default, Debug)]
pub struct ValueNode<T>(pub T);
impl<T: StaticTypeSized> StaticType for ValueNode<T> {
type Static = ValueNode<T::Static>;
}
impl<'i, T: 'i> Node<'i, ()> for ValueNode<T> {
type Output = &'i T;
fn eval<'s: 'i>(&'s self, _input: ()) -> Self::Output {
@ -48,13 +44,6 @@ impl<T: Clone + Copy> Copy for ValueNode<T> {}
#[derive(Clone)]
pub struct ClonedNode<T: Clone>(pub T);
impl<T: Clone + StaticTypeSized> StaticType for ClonedNode<T>
where
T::Static: Clone,
{
type Static = ClonedNode<T::Static>;
}
impl<'i, T: Clone + 'i> Node<'i, ()> for ClonedNode<T> {
type Output = T;
fn eval<'s: 'i>(&'s self, _input: ()) -> Self::Output {

View File

@ -264,10 +264,6 @@ pub struct BlendImageTupleNode<P, MapFn> {
_p: PhantomData<P>,
}
impl<MapFn: StaticTypeSized, P: StaticTypeSized> StaticType for BlendImageTupleNode<P, MapFn> {
type Static = BlendImageTupleNode<P::Static, MapFn::Static>;
}
#[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, Background, MapFn> {
_p: PhantomData<P>,
}
impl<P: StaticTypeSized, Background: StaticTypeSized, MapFn: StaticTypeSized> StaticType for BlendImageNode<P, Background, MapFn> {
type Static = BlendImageNode<P::Static, Background::Static, MapFn::Static>;
}
// TODO: Implement proper blending
#[node_macro::node_fn(BlendImageNode<_P>)]
fn blend_image<_P: Clone, MapFn, Frame: Sample<Pixel = _P> + Transform, Background: RasterMut<Pixel = _P> + Transform>(