|
|
|
@ -20,7 +20,7 @@ macro_rules! impl_trait_object {
|
|
|
|
|
fn into_multitrait(self) -> $crate::MultitraitObject {
|
|
|
|
|
let mut mto = $crate::MultitraitObject::new(self);
|
|
|
|
|
$(
|
|
|
|
|
$crate::register_traits!(mto, $obj, $trt);
|
|
|
|
|
mto._register::<$trt>($crate::__fat_pointer!($obj as $trt).vptr);
|
|
|
|
|
)*
|
|
|
|
|
|
|
|
|
|
mto
|
|
|
|
@ -29,32 +29,43 @@ macro_rules! impl_trait_object {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! __fat_pointer {
|
|
|
|
|
($v:ty as $t:ty) => {{
|
|
|
|
|
let x = ::std::ptr::null::<$v>() as *const $v as *const $t;
|
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
|
unsafe {
|
|
|
|
|
std::mem::transmute::<_, $crate::FatPointer>(x)
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Registers multiple trait_impl on a multitrait object
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use multi_trait_object::*;
|
|
|
|
|
/// use std::fmt::{Debug, Display};
|
|
|
|
|
///
|
|
|
|
|
/// let value = String::new();
|
|
|
|
|
/// let mut mto = MultitraitObject::new(value);
|
|
|
|
|
/// register_traits!(mto, String, dyn Debug, dyn Display);
|
|
|
|
|
/// let mto = create_object!(String::new(), dyn Debug, dyn Display);
|
|
|
|
|
/// ```
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! register_traits {
|
|
|
|
|
($r:expr, $v:ty, $($t:ty), +) => {
|
|
|
|
|
macro_rules! create_object {
|
|
|
|
|
($v:expr, $($t:ty), +) => {
|
|
|
|
|
{
|
|
|
|
|
let null_ptr = unsafe {
|
|
|
|
|
// SAFETY: We're never accessing the null value
|
|
|
|
|
$crate::null_ptr(&$v)
|
|
|
|
|
};
|
|
|
|
|
let mut mto = $crate::MultitraitObject::new($v);
|
|
|
|
|
$(
|
|
|
|
|
$r._register::<$t>($crate::__fat_pointer!($v as $t).vptr);
|
|
|
|
|
let vptr = unsafe {
|
|
|
|
|
// SAFETY: We're never accessing the null value
|
|
|
|
|
let ptr = $crate::null_ptr(&*null_ptr) as *const $t;
|
|
|
|
|
std::mem::transmute::<_, $crate::FatPointer>(ptr).vptr
|
|
|
|
|
};
|
|
|
|
|
mto._register::<$t>(vptr);
|
|
|
|
|
)+
|
|
|
|
|
mto
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! __fat_pointer {
|
|
|
|
|
($v:ty as $t:ty) => {{
|
|
|
|
|
let x = ::std::ptr::null::<$v>() as *const $v as *const $t;
|
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
|
unsafe {
|
|
|
|
|
std::mem::transmute::<_, $crate::FatPointer>(x)
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
}
|