1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#![doc(html_root_url = "http://docs.rs/const-default/1.0.0")]
#![cfg_attr(feature = "unstable-docs", feature(doc_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "derive")]
#[cfg_attr(feature = "unstable-docs", doc(cfg(feature = "derive")))]
pub use dyn_any_derive::DynAny;

/// Implement this trait for your `dyn Trait` types for all `T: Trait`
pub trait UpcastFrom<T: ?Sized> {
	fn up_from(value: &T) -> &Self;
	fn up_from_mut(value: &mut T) -> &mut Self;
	#[cfg(feature = "alloc")]
	fn up_from_box(value: Box<T>) -> Box<Self>;
}

/// Use this trait to perform your upcasts on dyn traits. Make sure to require it in the supertrait!
pub trait Upcast<U: ?Sized> {
	fn up(&self) -> &U;
	fn up_mut(&mut self) -> &mut U;
	#[cfg(feature = "alloc")]
	fn up_box(self: Box<Self>) -> Box<U>;
}

impl<T: ?Sized, U: ?Sized> Upcast<U> for T
where
	U: UpcastFrom<T>,
{
	fn up(&self) -> &U {
		U::up_from(self)
	}
	fn up_mut(&mut self) -> &mut U {
		U::up_from_mut(self)
	}
	#[cfg(feature = "alloc")]
	fn up_box(self: Box<Self>) -> Box<U> {
		U::up_from_box(self)
	}
}

use core::any::TypeId;

impl<'a, T: DynAny<'a> + 'a> UpcastFrom<T> for dyn DynAny<'a> + 'a {
	fn up_from(value: &T) -> &(dyn DynAny<'a> + 'a) {
		value
	}
	fn up_from_mut(value: &mut T) -> &mut (dyn DynAny<'a> + 'a) {
		value
	}
	#[cfg(feature = "alloc")]
	fn up_from_box(value: Box<T>) -> Box<Self> {
		value
	}
}

pub trait DynAny<'a> {
	fn type_id(&self) -> TypeId;
	#[cfg(feature = "log-bad-types")]
	fn type_name(&self) -> &'static str;
}

impl<'a, T: StaticType> DynAny<'a> for T {
	fn type_id(&self) -> core::any::TypeId {
		core::any::TypeId::of::<T::Static>()
	}
	#[cfg(feature = "log-bad-types")]
	fn type_name(&self) -> &'static str {
		core::any::type_name::<T>()
	}
}
pub fn downcast_ref<'a, V: StaticType>(i: &'a dyn DynAny<'a>) -> Option<&'a V> {
	if i.type_id() == core::any::TypeId::of::<<V as StaticType>::Static>() {
		// SAFETY: caller guarantees that T is the correct type
		let ptr = i as *const dyn DynAny<'a> as *const V;
		Some(unsafe { &*ptr })
	} else {
		None
	}
}

#[cfg(feature = "alloc")]
pub fn downcast<'a, V: StaticType>(i: Box<dyn DynAny<'a> + 'a>) -> Result<Box<V>, String> {
	let type_id = DynAny::type_id(i.as_ref());
	if type_id == core::any::TypeId::of::<<V as StaticType>::Static>() {
		// SAFETY: caller guarantees that T is the correct type
		let ptr = Box::into_raw(i) as *mut dyn DynAny<'a> as *mut V;
		Ok(unsafe { Box::from_raw(ptr) })
	} else {
		if type_id == core::any::TypeId::of::<&dyn DynAny<'static>>() {
			panic!("downcast error: type_id == core::any::TypeId::of::<dyn DynAny<'a>>()");
		}
		#[cfg(feature = "log-bad-types")]
		{
			Err(format!("Incorrect type, expected {} but found {}", core::any::type_name::<V>(), DynAny::type_name(i.as_ref())))
		}

		#[cfg(not(feature = "log-bad-types"))]
		{
			Err(format!("Incorrect type, expected {}", core::any::type_name::<V>()))
		}
	}
}

pub unsafe trait StaticType {
	type Static: 'static + ?Sized;
	fn type_id(&self) -> core::any::TypeId {
		core::any::TypeId::of::<Self::Static>()
	}
}

pub unsafe trait StaticTypeSized {
	type Static: 'static;
	fn type_id(&self) -> core::any::TypeId {
		core::any::TypeId::of::<<Self as StaticTypeSized>::Static>()
	}
}
unsafe impl<T: StaticType + Sized> StaticTypeSized for T
where
	T::Static: Sized,
{
	type Static = <T as StaticType>::Static;
}
pub unsafe trait StaticTypeClone {
	type Static: 'static + Clone;
	fn type_id(&self) -> core::any::TypeId {
		core::any::TypeId::of::<<Self as StaticTypeClone>::Static>()
	}
}
unsafe impl<T: StaticType + Clone> StaticTypeClone for T
where
	T::Static: Clone,
{
	type Static = <T as StaticType>::Static;
}

macro_rules! impl_type {
	($($id:ident$(<$($(($l:lifetime, $s:lifetime)),*|)?$($T:ident),*>)?),*) => {
		$(
		unsafe impl< $($($T:  $crate::StaticTypeSized ,)*)?> $crate::StaticType for $id $(<$($($l,)*)?$($T, )*>)?{
			type Static = $id$(<$($($s,)*)?$(<$T as $crate::StaticTypeSized>::Static,)*>)?;
		}
		)*
	};
}

#[cfg(feature = "alloc")]
unsafe impl<'a, T: StaticTypeClone + Clone> StaticType for Cow<'a, T> {
	type Static = Cow<'static, <T as StaticTypeClone>::Static>;
}
unsafe impl<T: StaticTypeSized> StaticType for *const [T] {
	type Static = *const [<T as StaticTypeSized>::Static];
}
unsafe impl<T: StaticTypeSized> StaticType for *mut [T] {
	type Static = *mut [<T as StaticTypeSized>::Static];
}
unsafe impl<'a, T: StaticTypeSized> StaticType for &'a [T] {
	type Static = &'static [<T as StaticTypeSized>::Static];
}
macro_rules! impl_slice {
    ($($id:ident),*) => {
        $(
        unsafe impl<'a, T: StaticTypeSized> StaticType for $id<'a, T> {
            type Static = $id<'static, <T as StaticTypeSized>::Static>;
        }
        )*
    };
}

mod slice {
	use super::*;
	use core::slice::*;
	impl_slice!(Iter, IterMut, Chunks, ChunksMut, RChunks, RChunksMut, Windows);
}

#[cfg(feature = "alloc")]
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>;
}

unsafe impl<'a> StaticType for &'a str {
	type Static = &'static str;
}
unsafe impl StaticType for () {
	type Static = ();
}
unsafe impl<'a, T: 'a + StaticType + ?Sized> StaticType for &'a T {
	type Static = &'static <T as StaticType>::Static;
}
unsafe impl<T: StaticTypeSized, const N: usize> StaticType for [T; N] {
	type Static = [<T as StaticTypeSized>::Static; N];
}

unsafe impl<'a> StaticType for dyn DynAny<'a> + '_ {
	type Static = dyn DynAny<'static>;
}
#[cfg(feature = "alloc")]
pub trait IntoDynAny<'n>: Sized + StaticType + 'n {
	fn into_dyn(self) -> Box<dyn DynAny<'n> + 'n> {
		Box::new(self)
	}
}
#[cfg(feature = "alloc")]
impl<'n, T: StaticType + 'n> IntoDynAny<'n> for T {}

#[cfg(feature = "alloc")]
impl From<()> for Box<dyn DynAny<'static>> {
	fn from(_: ()) -> Box<dyn DynAny<'static>> {
		Box::new(())
	}
}

#[cfg(feature = "alloc")]
use alloc::{
	borrow::Cow,
	boxed::Box,
	collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque},
	string::String,
	vec::Vec,
};
use core::sync::atomic::*;
use core::{
	cell::{Cell, RefCell, UnsafeCell},
	iter::Empty,
	marker::{PhantomData, PhantomPinned},
	mem::{ManuallyDrop, MaybeUninit},
	num::Wrapping,
	ops::Range,
	time::Duration,
};

impl_type!(
	Option<T>, Result<T, E>, Cell<T>, UnsafeCell<T>, RefCell<T>, MaybeUninit<T>,
	 ManuallyDrop<T>, PhantomData<T>, PhantomPinned, Empty<T>, Range<T>,
	Wrapping<T>, Duration, bool, f32, f64, char,
	u8, AtomicU8, u16, AtomicU16, u32, AtomicU32, u64,  usize, AtomicUsize,
	i8, AtomicI8, i16, AtomicI16, i32, AtomicI32, i64,  isize, AtomicIsize,
	i128, u128, AtomicBool, AtomicPtr<T>
);
#[cfg(feature = "large-atomics")]
impl_type!(AtomicU64, AtomicI64);

#[cfg(feature = "alloc")]
impl_type!(
	Vec<T>, String, BTreeMap<K,V>,BTreeSet<V>, LinkedList<T>, VecDeque<T>,
	BinaryHeap<T>
);

#[cfg(feature = "std")]
use std::sync::*;

#[cfg(feature = "std")]
impl_type!(Once, Mutex<T>, RwLock<T>);

#[cfg(feature = "rc")]
use std::rc::Rc;
#[cfg(feature = "rc")]
impl_type!(Rc<T>);
#[cfg(all(feature = "rc", feature = "alloc"))]
use std::sync::Arc;
#[cfg(all(feature = "rc", feature = "alloc"))]
impl_type!(Arc<T>);

#[cfg(feature = "glam")]
use glam::*;
#[cfg(feature = "glam")]
#[rustfmt::skip]
impl_type!(
	IVec2, IVec3, IVec4, UVec2, UVec3, UVec4, BVec2, BVec3, BVec4,
	Vec2, Vec3, Vec3A, Vec4, DVec2, DVec3, DVec4,
	Mat2, Mat3, Mat3A, Mat4, DMat2, DMat3, DMat4,
	Quat, Affine2, Affine3A, DAffine2, DAffine3, DQuat
);

#[cfg(feature = "alloc")]
unsafe impl<T: crate::StaticType + ?Sized> crate::StaticType for Box<T> {
	type Static = Box<<T as crate::StaticType>::Static>;
}
#[test]
fn test_tuple_of_boxes() {
	let tuple = (Box::new(&1 as &dyn DynAny<'static>), Box::new(&2 as &dyn DynAny<'static>));
	let dyn_any = &tuple as &dyn DynAny;
	assert_eq!(&1, downcast_ref(*downcast_ref::<(Box<&dyn DynAny>, Box<&dyn DynAny>)>(dyn_any).unwrap().0).unwrap());
	assert_eq!(&2, downcast_ref(*downcast_ref::<(Box<&dyn DynAny>, Box<&dyn DynAny>)>(dyn_any).unwrap().1).unwrap());
}

macro_rules! impl_tuple {
	(@rec $t:ident) => { };
	(@rec $_:ident $($t:ident)+) => {
		impl_tuple! { @impl $($t)* }
		impl_tuple! { @rec $($t)* }
	};
	(@impl $($t:ident)*) => {
		unsafe impl< $($t: StaticTypeSized,)*> StaticType for ($($t,)*) {
			type Static = ($(<$t as $crate::StaticTypeSized>::Static,)*);
		}
	};
	($($t:ident)*) => {
		impl_tuple! { @rec _t $($t)* }
	};
}

impl_tuple! {
	A B C D E F G H I J K L
}

#[test]
fn simple_downcast() {
	let x = Box::new(3_u32) as Box<dyn DynAny>;
	assert_eq!(*downcast::<u32>(x).unwrap(), 3_u32);
}
#[test]
#[should_panic]
fn simple_downcast_panic() {
	let x = Box::new(3_i32) as Box<dyn DynAny>;
	assert_eq!(*downcast::<u32>(x).expect("attempted to perform invalid downcast"), 3_u32);
}