|
|
@ -20,7 +20,7 @@ macro_rules! impl_trait_object {
|
|
|
|
fn into_multitrait(self) -> $crate::MultitraitObject {
|
|
|
|
fn into_multitrait(self) -> $crate::MultitraitObject {
|
|
|
|
let mut mto = $crate::MultitraitObject::new(self);
|
|
|
|
let mut mto = $crate::MultitraitObject::new(self);
|
|
|
|
$(
|
|
|
|
$(
|
|
|
|
$crate::register_traits!(mto, $obj, $trt);
|
|
|
|
mto._register::<$trt>($crate::__fat_pointer!($obj as $trt).vptr);
|
|
|
|
)*
|
|
|
|
)*
|
|
|
|
|
|
|
|
|
|
|
|
mto
|
|
|
|
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
|
|
|
|
/// Registers multiple trait_impl on a multitrait object
|
|
|
|
/// ```rust
|
|
|
|
/// ```rust
|
|
|
|
/// use multi_trait_object::*;
|
|
|
|
/// use multi_trait_object::*;
|
|
|
|
/// use std::fmt::{Debug, Display};
|
|
|
|
/// use std::fmt::{Debug, Display};
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// let value = String::new();
|
|
|
|
/// let mto = create_object!(String::new(), dyn Debug, dyn Display);
|
|
|
|
/// let mut mto = MultitraitObject::new(value);
|
|
|
|
|
|
|
|
/// register_traits!(mto, String, dyn Debug, dyn Display);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! register_traits {
|
|
|
|
macro_rules! create_object {
|
|
|
|
($r:expr, $v:ty, $($t:ty), +) => {
|
|
|
|
($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)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|