Bump dyn-any version + format code

This commit is contained in:
Dennis 2022-08-04 09:08:48 +02:00 committed by Keavon Chambers
parent c44e9d22e3
commit d09f023618
12 changed files with 464 additions and 513 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "dyn-any"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Dennis Kobert <dennis@kobert.dev>"]

View file

@ -1,6 +1,6 @@
[package]
name = "dyn-any-derive"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Dennis Kobert"]

View file

@ -8,48 +8,48 @@ pub use dyn_any_derive::DynAny;
use std::any::TypeId;
pub trait DynAny<'a> {
fn type_id(&self) -> TypeId;
fn type_id(&self) -> TypeId;
}
impl<'a, T: StaticType> DynAny<'a> for T {
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<T::Static>()
}
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<T::Static>()
}
}
pub fn downcast_ref<'a, V: StaticType>(i: &'a dyn DynAny<'a>) -> Option<&'a V> {
if i.type_id() == std::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
}
if i.type_id() == std::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
}
}
pub trait StaticType {
type Static: 'static + ?Sized;
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self::Static>()
}
type Static: 'static + ?Sized;
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self::Static>()
}
}
pub trait StaticTypeSized {
type Static: 'static;
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self::Static>()
}
type Static: 'static;
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self::Static>()
}
}
impl<'a, T: StaticTypeSized> StaticType for T {
type Static = <T as StaticTypeSized>::Static;
type Static = <T as StaticTypeSized>::Static;
}
pub trait StaticTypeClone {
type Static: 'static + Clone;
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self::Static>()
}
type Static: 'static + Clone;
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self::Static>()
}
}
impl<'a, T: StaticTypeClone> StaticTypeSized for T {
type Static = <T as StaticTypeClone>::Static;
type Static = <T as StaticTypeClone>::Static;
}
macro_rules! impl_type {
@ -61,54 +61,52 @@ macro_rules! impl_type {
)*
};
}
impl<'a, T: Clone + StaticTypeClone> StaticTypeClone
for std::borrow::Cow<'a, T>
{
type Static = std::borrow::Cow<'static, <T as StaticTypeSized>::Static>;
impl<'a, T: Clone + StaticTypeClone> StaticTypeClone for std::borrow::Cow<'a, T> {
type Static = std::borrow::Cow<'static, <T as StaticTypeSized>::Static>;
}
impl<'a, T: StaticTypeSized> StaticTypeSized for *const [T] {
type Static = *const [<T as StaticTypeSized>::Static];
type Static = *const [<T as StaticTypeSized>::Static];
}
impl<'a, T: StaticTypeSized> StaticTypeSized for *mut [T] {
type Static = *mut [<T as StaticTypeSized>::Static];
type Static = *mut [<T as StaticTypeSized>::Static];
}
impl<'a, T: StaticTypeSized> StaticTypeSized for &'a [T] {
type Static = &'static [<T as StaticTypeSized>::Static];
type Static = &'static [<T as StaticTypeSized>::Static];
}
impl<'a> StaticTypeSized for &'a str {
type Static = &'static str;
type Static = &'static str;
}
impl<'a> StaticTypeSized for () {
type Static = ();
type Static = ();
}
impl<'a, T: 'a + StaticTypeClone> StaticTypeClone for &'a T {
type Static = &'static <T as StaticTypeClone>::Static;
type Static = &'static <T as StaticTypeClone>::Static;
}
impl<'a, T: StaticTypeSized, const N: usize> StaticTypeSized for [T; N] {
type Static = [<T as StaticTypeSized>::Static; N];
type Static = [<T as StaticTypeSized>::Static; N];
}
use core::{
cell::{Cell, RefCell, UnsafeCell},
iter::Empty,
marker::{PhantomData, PhantomPinned},
mem::{ManuallyDrop, MaybeUninit},
num::Wrapping,
time::Duration,
cell::{Cell, RefCell, UnsafeCell},
iter::Empty,
marker::{PhantomData, PhantomPinned},
mem::{ManuallyDrop, MaybeUninit},
num::Wrapping,
time::Duration,
};
use std::{
collections::*,
sync::{atomic::*, *},
vec::Vec,
collections::*,
sync::{atomic::*, *},
vec::Vec,
};
impl_type!(Option<T>,Result<T, E>,Cell<T>,UnsafeCell<T>,RefCell<T>,MaybeUninit<T>,
Vec<T>, String, BTreeMap<K,V>,BTreeSet<V>, LinkedList<T>, VecDeque<T>,
BinaryHeap<T>, ManuallyDrop<T>, PhantomData<T>, PhantomPinned,Empty<T>,
Wrapping<T>, Duration, Once, Mutex<T>, RwLock<T>, bool, f32, f64, char,
u8, AtomicU8, u16,AtomicU16, u32,AtomicU32, u64,AtomicU64, usize,AtomicUsize,
i8,AtomicI8, i16,AtomicI16, i32,AtomicI32, i64,AtomicI64, isize,AtomicIsize,
i128, u128, AtomicBool, AtomicPtr<T>
Vec<T>, String, BTreeMap<K,V>,BTreeSet<V>, LinkedList<T>, VecDeque<T>,
BinaryHeap<T>, ManuallyDrop<T>, PhantomData<T>, PhantomPinned,Empty<T>,
Wrapping<T>, Duration, Once, Mutex<T>, RwLock<T>, bool, f32, f64, char,
u8, AtomicU8, u16,AtomicU16, u32,AtomicU32, u64,AtomicU64, usize,AtomicUsize,
i8,AtomicI8, i16,AtomicI16, i32,AtomicI32, i64,AtomicI64, isize,AtomicIsize,
i128, u128, AtomicBool, AtomicPtr<T>
);
macro_rules! impl_tuple {
(@rec $t:ident) => { };
@ -127,5 +125,5 @@ macro_rules! impl_tuple {
}
impl_tuple! {
A B C D E F G H I J K L
A B C D E F G H I J K L
}