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

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,)*);
}
};