#![no_std]
#![doc(html_logo_url = "https://raw.githubusercontent.com/Wandalen/wTools/master/asset/img/logo_v3_trans_square.png")]
#![doc(
html_favicon_url = "https://raw.githubusercontent.com/Wandalen/wTools/alpha/asset/img/logo_v3_trans_square_icon_small_v2.ico"
)]
#![doc(html_root_url = "https://docs.rs/clone_dyn_types/latest/clone_dyn_types/")]
#![ cfg_attr( doc, doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "readme.md" ) ) ) ]
#![ cfg_attr( not( doc ), doc = "Clone trait object types" ) ]
#[ cfg( feature = "enabled" ) ]
pub mod dependency
{
}
#[ cfg( feature = "enabled" ) ]
mod private
{
extern crate alloc;
use alloc::boxed::Box;
pub trait CloneDyn: Sealed {
#[ doc( hidden ) ]
fn __clone_dyn(&self, _ : DontCallMe) -> *mut ();
}
impl< T > CloneDyn for T
where
T: Clone,
{
#[ inline ]
#[ allow( clippy::implicit_return, clippy::as_conversions, clippy::ptr_as_ptr ) ]
fn __clone_dyn(&self, _ : DontCallMe) -> *mut ()
{
Box::< T >::into_raw(Box::new(self.clone())) as *mut ()
}
}
impl< T > CloneDyn for [T]
where
T: Clone,
{
#[ inline ]
#[ allow( clippy::implicit_return, clippy::as_conversions, clippy::ptr_as_ptr ) ]
fn __clone_dyn(&self, _ : DontCallMe) -> *mut ()
{
Box::< [T] >::into_raw(self.iter().cloned().collect()) as *mut ()
}
}
impl CloneDyn for str
{
#[ inline ]
#[ allow( clippy ::as_conversions, clippy ::ptr_as_ptr, clippy ::implicit_return ) ]
fn __clone_dyn(&self, _: DontCallMe) -> *mut ()
{
Box::< str >::into_raw(Box::from(self)) as *mut ()
}
}
#[ inline ]
pub fn clone< T >(src : &T) -> T
where
T: CloneDyn,
{
#[ allow( unsafe_code,
clippy::as_conversions,
clippy::ptr_as_ptr,
clippy::implicit_return,
clippy::undocumented_unsafe_blocks ) ]
unsafe
{
*Box::from_raw(< T as CloneDyn >::__clone_dyn(src, DontCallMe) as *mut T)
}
}
#[ inline ]
pub fn clone_into_box< T >(ref_dyn : &T) -> Box< T >
where
T: ?Sized + CloneDyn,
{
#[ allow( unsafe_code,
clippy::implicit_return,
clippy::as_conversions,
clippy::ptr_cast_constness,
clippy::ptr_as_ptr,
clippy::multiple_unsafe_ops_per_block,
clippy::undocumented_unsafe_blocks,
clippy::ref_as_ptr,
clippy::borrow_as_ptr ) ]
unsafe
{
let mut ptr = ref_dyn as *const T;
#[ allow( clippy::borrow_as_ptr ) ]
let data_ptr = &mut ptr as *mut *const T as *mut *mut (); *data_ptr = < T as CloneDyn >::__clone_dyn(ref_dyn, DontCallMe);
Box::from_raw(ptr as *mut T)
}
}
#[ doc( hidden ) ]
mod sealed
{
#[ doc( hidden ) ]
#[ allow( missing_debug_implementations ) ]
pub struct DontCallMe;
#[ doc( hidden ) ]
pub trait Sealed {}
impl< T: Clone > Sealed for T {}
impl< T: Clone > Sealed for [T] {}
impl Sealed for str {}
}
use sealed :: { DontCallMe, Sealed };
}
#[ cfg( feature = "enabled" ) ]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
#[ allow( clippy ::pub_use ) ]
pub use own :: *;
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod own
{
use super ::orphan;
#[ doc( inline ) ]
#[ allow( clippy ::useless_attribute, clippy ::pub_use ) ]
pub use orphan :: *;
}
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod orphan
{
use super ::exposed;
#[ doc( inline ) ]
#[ allow( clippy ::useless_attribute, clippy ::pub_use ) ]
pub use exposed :: *;
}
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod exposed
{
use super ::prelude;
#[ doc( inline ) ]
#[ allow( clippy ::useless_attribute, clippy ::pub_use ) ]
pub use prelude :: *;
}
#[ cfg( feature = "enabled" ) ]
#[ allow( unused_imports ) ]
pub mod prelude
{
use super ::private;
#[ doc( inline ) ]
#[ allow( clippy ::useless_attribute, clippy ::pub_use ) ]
pub use private :: { CloneDyn, clone_into_box, clone };
}