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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![allow(clippy::needless_borrowed_reference)]
#![forbid(unsafe_op_in_unsafe_fn)]
#![feature(coerce_unsized)]
#![feature(const_nonnull_new)]
#![feature(const_option)]
#![feature(const_trait_impl)]
#![feature(const_type_id)]
#![cfg_attr(feature = "dynamic-names", feature(const_type_name))]
#![feature(doc_auto_cfg)]
#![feature(effects)]
#![feature(ptr_metadata)]
#![feature(unsize)]
#[cfg(feature = "alloc")]
extern crate alloc;
mod cast_target;
mod fat;
mod table;
#[doc(hidden)]
pub mod internal;
/// Declares a trait as being a base trait for downcasting.
///
/// This macro marks a trait as being a base for dynamic trait object downcasting. All `impl` blocks for this trait will need to use the
/// [`#[dyn_dyn_impl]`](dyn_dyn_impl) attribute to declare what traits they wish to expose.
pub use dyn_dyn_macros::dyn_dyn_base;
/// Performs a dynamic downcast of a reference to a trait object where the trait was declared with [`#[dyn_dyn_base]`](dyn_dyn_base).
///
/// This macro allows for trying to cast such a reference to a reference to another trait object, returning an [`Option`] containing the
/// reference to the downcast trait object if the object in question implements that trait.
///
/// This macro accepts the following types for a given base trait `B`, with the first matching set of conditions determining how the
/// dereference will occur:
///
/// - A (mutable) reference to a type that implements `B`, returning a (mutable) reference referring to the same object as the original
/// reference
/// - A (mutable) reference to a pointer type that implements [`DynDyn<B>`], returning a (mutable) reference referring to the pointee of
/// that pointer
/// - A (mutable) reference to a pointer type that implements Deref with a target that implements `B`, returning a (mutable) reference
/// referring to the pointee of that pointer
///
/// # Examples
///
/// ```rust
/// # use dyn_dyn::{dyn_dyn_base, dyn_dyn_cast, dyn_dyn_impl};
/// #[dyn_dyn_base]
/// trait Base {}
/// trait Trait {}
///
/// struct Struct;
///
/// #[dyn_dyn_impl(Trait)]
/// impl Base for Struct {}
/// impl Trait for Struct {}
///
/// fn downcast(r: &dyn Base) -> Result<&dyn Trait, &dyn Base> {
/// dyn_dyn_cast!(Base => Trait, r)
/// }
///
/// fn downcast_mut(r: &mut dyn Base) -> Result<&mut dyn Trait, &mut dyn Base> {
/// dyn_dyn_cast!(mut Base => Trait, r)
/// }
///
/// fn downcast_with_auto(r: &(dyn Base + Send)) -> Result<&(dyn Trait + Send), &(dyn Base + Send)> {
/// dyn_dyn_cast!(Base + Send => Trait + Send, r)
/// }
///
/// fn downcast_box(r: Box<dyn Base>) -> Result<Box<dyn Trait>, Box<dyn Base>> {
/// dyn_dyn_cast!(move Base => Trait, r)
/// }
///
/// fn main() {
/// let mut s = Struct;
///
/// assert!(downcast(&s).is_ok());
/// assert!(downcast_mut(&mut s).is_ok());
/// assert!(downcast_with_auto(&s).is_ok());
/// assert!(downcast_box(Box::new(s)).is_ok());
/// }
/// ```
pub use dyn_dyn_macros::dyn_dyn_cast;
/// Marks an `impl` block as targeting a trait that was declared with the [`#[dyn_dyn_base]`](dyn_dyn_base) attribute.
///
/// This attribute allows the `impl` block to specify what other traits should be exposed for downcasting via the base trait that's being
/// implemented in this block.
///
/// # Examples
///
/// ```rust
/// # use core::fmt::Debug;
/// # use dyn_dyn::{dyn_dyn_base, dyn_dyn_impl};
/// #[dyn_dyn_base]
/// trait Base {}
///
/// #[derive(Debug)]
/// struct Struct;
///
/// #[dyn_dyn_impl(Debug)]
/// impl Base for Struct {}
/// ```
///
/// ```rust
/// # use dyn_dyn::{dyn_dyn_base, dyn_dyn_impl};
/// #[dyn_dyn_base]
/// trait Base {}
/// trait Trait<T> {}
///
/// struct Struct<T>(T);
///
/// impl<T> Trait<T> for Struct<T> {}
///
/// #[dyn_dyn_impl(Trait<T>)]
/// impl<T: 'static> Base for Struct<T> {}
/// ```
pub use dyn_dyn_macros::dyn_dyn_impl;
pub use cast_target::DynDynCastTarget;
pub use fat::DynDynFat;
pub use table::{AnyDynMetadata, DynDynTable, DynDynTableEntry, DynDynTableIterator};
#[cfg(doc)]
use core::ops::Deref;
use cfg_if::cfg_if;
use core::marker::{PhantomData, Unsize};
use core::ops::DerefMut;
use core::ptr::{DynMetadata, NonNull};
use stable_deref_trait::StableDeref;
/// A type that can be dynamically downcast to other traits using the [`dyn_dyn_cast!`] macro.
///
/// This trait should not be manually implemented by user code. Instead, this trait should be implemented by using the
/// [`#[dyn_dyn_base]`](dyn_dyn_base) attribute on the trait in question. The exact shape of this trait is subject to change at any time, so
/// it generally shouldn't be relied upon in external code except as a trait bound.
///
/// # Safety
///
/// The result of calling [`DynDynBase::get_dyn_dyn_table`] on an object through a given base must never change for the lifetime of that
/// object, even if the object itself is mutated.
pub unsafe trait DynDynBase {
/// Gets the [`DynDynTable`] for this object, for traits exposed via this base trait.
///
/// In user code, it is generally preferred to use the implementation of [`GetDynDynTable`] for references rather than calling this
/// method directly to avoid potential future breakage.
fn get_dyn_dyn_table(&self) -> DynDynTable;
}
/// Wraps a reference to a pointer implementing [`GetDynDynTable<B>`] and which can be dereferenced to perform the downcast.
///
/// Using [`dyn_dyn_cast!`] on this struct will call [`GetDynDynTable::get_dyn_dyn_table`] on the pointer itself, then dereference this
/// pointer to perform the downcast. This allows a pointer implementing [`DynDyn<B>`] to be downcast into a reference without moving the
/// pointer itself.
pub struct DynDynRef<'a, B: ?Sized + DynDynBase, T: GetDynDynTable<B> + StableDeref>(
&'a T,
PhantomData<fn(B) -> B>,
);
impl<'a, B: ?Sized + DynDynBase, T: GetDynDynTable<B> + StableDeref> DynDynRef<'a, B, T>
where
T::Target: Unsize<B>,
{
/// Creates a new [`DynDynRef`] for the provided reference to a pointer.
pub fn new(r: &'a T) -> Self {
DynDynRef(r, PhantomData)
}
}
/// Wraps a mutable reference to a pointer implementing [`GetDynDynTable<B>`] and which can be dereferenced to perform the downcast.
///
/// Using [`dyn_dyn_cast!`] on this struct will call [`GetDynDynTable::get_dyn_dyn_table`] on the pointer itself, then dereference this
/// pointer to perform the downcast. This allows a pointer implementing [`DynDyn<B>`] to be downcast into a mutable reference without moving
/// the pointer itself.
pub struct DynDynRefMut<'a, B: ?Sized + DynDynBase, T: GetDynDynTable<B> + StableDeref + DerefMut>(
&'a mut T,
PhantomData<fn(B) -> B>,
);
impl<'a, B: ?Sized + DynDynBase, T: GetDynDynTable<B> + StableDeref + DerefMut>
DynDynRefMut<'a, B, T>
{
/// Creates a new [`DynDynRefMut`] for the provided mutable reference to a pointer.
pub fn new(r: &'a mut T) -> Self {
DynDynRefMut(r, PhantomData)
}
}
/// A pointer to an object which has a [`DynDynTable`] associated with it.
///
/// # Safety
///
/// - If this type implements [`Deref`], then the reference returned by calling [`Deref::deref`] must not change for the lifetime of this
/// pointer unless the pointer itself is mutated.
/// - If this type implements [`DerefMut`], then the reference returned by calling [`DerefMut::deref_mut`] must not change for the lifetime
/// of this pointer unless the pointer itself is mutated and must point to the same object as a reference returned by calling
/// [`Deref::deref`], including having identical metadata. Additionally, calling [`DerefMut::deref_mut`] must not mutate the pointer.
/// - If this type implements [`Deref`], then the reference returned by calling [`Deref::deref`] must be unsize-coercible to a reference to
/// [`GetDynDynTable::get_dyn_dyn_table`].
/// - If this type implements [`Deref`], then the returned table must be equivalent to calling [`GetDynDynTable::get_dyn_dyn_table`] on a
/// reference returned by calling [`Deref::deref`].
/// - If this type implements [`DowncastUnchecked<B>`], then the result of calling [`DowncastUnchecked::downcast_unchecked`] with
/// metadata retrieved from the table returned by calling [`GetDynDynTable::get_dyn_dyn_table`] on this pointer shall be valid and safe to
/// use.
pub unsafe trait GetDynDynTable<B: ?Sized + DynDynBase> {
/// The actual type that this pointer currently points to. This type is used to allow propagation of auto trait bounds such as `Send`
/// and `Sync` in the `dyn_dyn_cast!` macro.
type DynTarget: ?Sized + Unsize<B>;
/// Gets the [`DynDynTable`] for the object that this pointer points to.
fn get_dyn_dyn_table(&self) -> DynDynTable;
}
/// A pointer to an object that can be unsafely downcast to point to another type.
pub trait DowncastUnchecked<'a, B: ?Sized + DynDynBase> {
/// The result of downcasting this pointer to point to the type `D`. Note that this type need not have the same outer wrapper as the
/// type implementing `DowncastUnchecked`, since the result of the downcast may involve coercions and dereferences.
type DowncastResult<D: ?Sized + 'a>;
/// Downcasts this pointer into a new pointer pointing to the same object, but having type `D`.
///
/// Generally, the result of calling this function should be equivalent to turning this pointer type into a raw pointer, removing its
/// metadata, unsafely casting that pointer into a pointer to `D` using the provided metadata, and then turning that raw pointer into
/// another pointer type.
///
/// As long as the concrete type of the pointee matches the concrete type of the metadata provided, then this is guaranteed to result
/// in a pointer which is valid and safe to use.
///
/// # Safety
///
/// Attaching the provided metadata to a pointer to the same data address as that held by this pointer must be guaranteed to be valid
/// and safe to use before this function can be called.
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(
self,
metadata: DynMetadata<D::Root>,
) -> Self::DowncastResult<D>;
}
/// A pointer object that can be safely downcast to refer to other trait types by using the `dyn_dyn_cast!` macro.
pub trait DynDyn<'a, B: ?Sized + DynDynBase>: GetDynDynTable<B> + DowncastUnchecked<'a, B> {}
impl<'a, B: ?Sized + DynDynBase, T: GetDynDynTable<B> + DowncastUnchecked<'a, B>> DynDyn<'a, B>
for T
{
}
// SAFETY: The referent of a shared reference will never change unexpectedly and the table returned matches that returned by dereferencing
// it by definition. The DowncastUnchecked implementation is also a simple cast via converting to/from a pointer and so should be
// correct.
unsafe impl<'a, B: ?Sized + DynDynBase, T: ?Sized + Unsize<B>> GetDynDynTable<B> for &'a T {
type DynTarget = T;
fn get_dyn_dyn_table(&self) -> DynDynTable {
B::get_dyn_dyn_table(*self)
}
}
impl<'a, B: ?Sized + DynDynBase, T: ?Sized + Unsize<B>> DowncastUnchecked<'a, B> for &'a T {
type DowncastResult<D: ?Sized + 'a> = &'a D;
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(
self,
metadata: DynMetadata<D::Root>,
) -> &'a D {
// SAFETY: Safety invariants for this fn require that the provided metadata is valid for self. Since the input reference has the
// lifetime 'a and the returned reference also has lifetime 'a, this dereference does not extend the reference's lifetime
// and only serves to re-attach the metadata.
unsafe { &*D::ptr_from_parts(NonNull::from(self).cast(), metadata).as_ptr() }
}
}
// SAFETY: Since T is StableDeref, the results of its Deref implementation should meet the stability requirements and the table returned is
// simply passed through from T's GetDynDynTable<B> implementation, which is unsafe itself and can be assumed to be correct. The
// DowncastUnchecked implementation defers to the impl for &T::Target, so it should be correct.
unsafe impl<'a, B: ?Sized + DynDynBase, T: GetDynDynTable<B> + StableDeref + 'a> GetDynDynTable<B>
for DynDynRef<'a, B, T>
where
T::Target: Unsize<B>,
{
type DynTarget = T::DynTarget;
fn get_dyn_dyn_table(&self) -> DynDynTable {
<T as GetDynDynTable<B>>::get_dyn_dyn_table(self.0)
}
}
impl<'a, B: ?Sized + DynDynBase, T: DynDyn<'a, B> + StableDeref + 'a> DowncastUnchecked<'a, B>
for DynDynRef<'a, B, T>
where
T::Target: Unsize<B>,
{
type DowncastResult<D: ?Sized + 'a> = &'a D;
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(
self,
metadata: DynMetadata<D::Root>,
) -> Self::DowncastResult<D> {
// SAFETY: Just passing through to the implementation for &'a T.
unsafe { <&T::Target as DowncastUnchecked<B>>::downcast_unchecked(&**self.0, metadata) }
}
}
// SAFETY: The referent of a mutable reference will never change unexpectedly and the table is returned by deferring to &T's implementation
// and so should be correct. The DowncastUnchecked implementation is also a simple cast via converting to/from a pointer and so
// should also be correct.
unsafe impl<'a, B: ?Sized + DynDynBase, T: ?Sized + Unsize<B>> GetDynDynTable<B> for &'a mut T {
type DynTarget = T;
fn get_dyn_dyn_table(&self) -> DynDynTable {
B::get_dyn_dyn_table(*self)
}
}
impl<'a, B: ?Sized + DynDynBase, T: ?Sized + Unsize<B>> DowncastUnchecked<'a, B> for &'a mut T {
type DowncastResult<D: ?Sized + 'a> = &'a mut D;
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(
self,
metadata: DynMetadata<D::Root>,
) -> &'a mut D {
// SAFETY: Safety invariants for this fn require that the provided metadata is valid for self. Since the input reference has the
// lifetime 'a and the returned reference also has lifetime 'a, this dereference does not extend the reference's lifetime
// and only serves to re-attach the metadata.
unsafe { &mut *D::ptr_from_parts(NonNull::from(self).cast(), metadata).as_ptr() }
}
}
// SAFETY: Since T is StableDeref, the results of its Deref and DerefMut implementations should meet the stability requirements and the
// table returned is simply passed through from T's GetDynDynTable<B> implementation, which is unsafe itself and can be assumed to
// be correct. The DowncastUnchecked implementation defers to the impl for &mut T::Target, so it should be correct.
unsafe impl<'a, B: ?Sized + DynDynBase, T: DynDyn<'a, B> + StableDeref + DerefMut + 'a>
GetDynDynTable<B> for DynDynRefMut<'a, B, T>
where
T::Target: Unsize<B>,
{
type DynTarget = T::Target;
fn get_dyn_dyn_table(&self) -> DynDynTable {
<T as GetDynDynTable<B>>::get_dyn_dyn_table(self.0)
}
}
impl<'a, B: ?Sized + DynDynBase, T: DynDyn<'a, B> + StableDeref + DerefMut + 'a>
DowncastUnchecked<'a, B> for DynDynRefMut<'a, B, T>
where
T::Target: Unsize<B>,
{
type DowncastResult<D: ?Sized + 'a> = &'a mut D;
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(
self,
metadata: DynMetadata<D::Root>,
) -> Self::DowncastResult<D> {
// SAFETY: Just passing through to the implementation for &'a mut T.
unsafe {
<&mut T::Target as DowncastUnchecked<B>>::downcast_unchecked(&mut **self.0, metadata)
}
}
}
cfg_if! {
if #[cfg(feature = "alloc")] {
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::rc::Rc;
// SAFETY: Box<T> meets all Deref/DerefMut stability requirements and the table is retrieved by dereferencing it, which is correct
// by definition. The DowncastUnchecked implementation is also a simple cast via converting to/from a pointer and so should
// be correct.
unsafe impl<B: ?Sized + DynDynBase, T: ?Sized + Unsize<B>> GetDynDynTable<B> for Box<T> {
type DynTarget = T;
fn get_dyn_dyn_table(&self) -> DynDynTable {
B::get_dyn_dyn_table(&**self)
}
}
impl<'a, B: ?Sized + DynDynBase, T: ?Sized + Unsize<B> + 'a> DowncastUnchecked<'a, B>
for Box<T>
{
type DowncastResult<D: ?Sized + 'a> = Box<D>;
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(self, metadata: DynMetadata<D::Root>) -> Box<D> {
// SAFETY: 1) NonNull::new_unchecked is fine since the raw pointer of a Box can never be null.
// 2) Box::from_raw is fine since the fat pointer passed in has the same data pointer as what we got from
// Box::into_raw and the metadata pointer is guaranteed to be valid by this fn's safety invariants.
unsafe {
Box::from_raw(
D::ptr_from_parts(NonNull::new_unchecked(Box::into_raw(self)).cast(), metadata)
.as_ptr(),
)
}
}
}
// SAFETY: Rc<T> meets all Deref/DerefMut stability requirements and the table is retrieved by dereferencing it, which is correct by
// definition. The DowncastUnchecked implementation is also a simple cast via converting to/from a pointer and so should be
// correct.
unsafe impl<B: ?Sized + DynDynBase, T: ?Sized + Unsize<B>> GetDynDynTable<B> for Rc<T> {
type DynTarget = T;
fn get_dyn_dyn_table(&self) -> DynDynTable {
B::get_dyn_dyn_table(&**self)
}
}
impl<'a, B: ?Sized + DynDynBase, T: ?Sized + Unsize<B> + 'a> DowncastUnchecked<'a, B>
for Rc<T>
{
type DowncastResult<D: ?Sized + 'a> = Rc<D>;
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(self, metadata: DynMetadata<D::Root>) -> Rc<D> {
// SAFETY: 1) NonNull::new_unchecked is fine since the raw pointer of a Box can never be null.
// 2) Rc::from_raw is fine since the fat pointer passed in has the same data pointer as what we got from
// Rc::into_raw and the metadata pointer is guaranteed to be valid by this fn's safety invariants.
unsafe {
Rc::from_raw(
D::ptr_from_parts(
NonNull::new_unchecked(Rc::into_raw(self) as *mut T).cast(),
metadata,
)
.as_ptr(),
)
}
}
}
// SAFETY: Arc<T> meets all Deref/DerefMut stability requirements and the table is retrieved by dereferencing it, which is correct
// by definition. The DowncastUnchecked implementation is also a simple cast via converting to/from a pointer and so should
// be correct.
unsafe impl<B: ?Sized + DynDynBase, T: ?Sized + Unsize<B>> GetDynDynTable<B> for Arc<T> {
type DynTarget = T;
fn get_dyn_dyn_table(&self) -> DynDynTable {
B::get_dyn_dyn_table(&**self)
}
}
impl<'a, B: ?Sized + DynDynBase, T: ?Sized + Unsize<B> + 'a> DowncastUnchecked<'a, B>
for Arc<T>
{
type DowncastResult<D: ?Sized + 'a> = Arc<D>;
unsafe fn downcast_unchecked<D: ?Sized + DynDynCastTarget>(self, metadata: DynMetadata<D::Root>) -> Arc<D> {
// SAFETY: 1) NonNull::new_unchecked is fine since the raw pointer of a Box can never be null.
// 2) Arc::from_raw is fine since the fat pointer passed in has the same data pointer as what we got from
// Arc::into_raw and the metadata pointer is guaranteed to be valid by this fn's safety invariants.
unsafe {
Arc::from_raw(
D::ptr_from_parts(
NonNull::new_unchecked(Arc::into_raw(self) as *mut T).cast(),
metadata,
)
.as_ptr(),
)
}
}
}
}
}