use std::any::Any; use unsize::Coercion; /// Attempt to downcast a [`triomphe::Arc`][`triomphe::Arc`] to a concrete type. /// /// Checks that the downcast is safe using [`Any::is`]. /// /// Ported from [`std::sync::Arc::downcast`] to [`triomphe::Arc`]. pub fn downcast_triomphe_arc( this: triomphe::Arc, ) -> Result, triomphe::Arc> { if (*this).is::() { unsafe { Ok(unchecked_sidecast_triomphe_arc(this)) } } else { Err(this) } } /// Transmutes the contents of `Arc` to `Arc`. Updates the `Arc`'s fat /// pointer metadata. /// /// Unlike [`downcast_triomphe_arc`] this make no checks the transmute is safe. /// /// # Safety /// /// It must be [safe to transmute][transmutes] from `T` to `U`. /// /// [transmutes]: https://doc.rust-lang.org/nomicon/transmutes.html pub unsafe fn unchecked_sidecast_triomphe_arc(this: triomphe::Arc) -> triomphe::Arc where T: ?Sized, { unsafe { // Get the pointer to the offset (*const T) inside of the ArcInner. let ptr = triomphe::Arc::into_raw(this); // SAFETY: The negative offset from the data (ptr) in an Arc to the start of the // data structure is fixed regardless of type `T`. // // SAFETY: Casting from a fat pointer to a thin pointer is safe, as long as the // types are compatible (they are). triomphe::Arc::from_raw(ptr.cast()) } } /// [`Coercion::to_any`] except that it coerces to `dyn Any + Send + Sync` as /// opposed to `dyn Any`. pub fn coerce_to_any_send_sync() -> Coercion { // SAFETY: The signature of this function guarantees the coercion is valid unsafe { Coercion::new(|x| x) } }