From ea4719ecaf966bd757332b6f85df6242f8b00b18 Mon Sep 17 00:00:00 2001 From: Bas Zalmstra Date: Fri, 27 Mar 2020 17:46:40 +0100 Subject: [PATCH] misc: removed lots of trait impls into macros --- crates/mun_runtime/src/lib.rs | 7 +- crates/mun_runtime/src/reflection.rs | 577 +++------------------------ 2 files changed, 65 insertions(+), 519 deletions(-) diff --git a/crates/mun_runtime/src/lib.rs b/crates/mun_runtime/src/lib.rs index f28052f01..b9a1228db 100644 --- a/crates/mun_runtime/src/lib.rs +++ b/crates/mun_runtime/src/lib.rs @@ -6,17 +6,14 @@ mod assembly; mod function; -#[macro_use] -mod macros; +#[macro_use] mod macros; +#[macro_use] mod type_info; mod garbage_collector; mod marshal; mod reflection; mod static_type_map; mod r#struct; -#[macro_use] -mod type_info; - #[cfg(test)] mod test; diff --git a/crates/mun_runtime/src/reflection.rs b/crates/mun_runtime/src/reflection.rs index 113300a64..4ec8b9c54 100644 --- a/crates/mun_runtime/src/reflection.rs +++ b/crates/mun_runtime/src/reflection.rs @@ -1,4 +1,4 @@ -use crate::type_info::HasStaticTypeInfo; +use crate::type_info::{HasStaticTypeInfo}; use crate::{marshal::Marshal, StructRef}; use md5; @@ -68,213 +68,46 @@ pub trait ArgumentReflection: Sized { fn marshal(self) -> Self::Marshalled; } -impl ArgumentReflection for f64 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for f32 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for isize { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for usize { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for i64 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for i32 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } +macro_rules! impl_primitive_type { + ($($ty:ty),+) => { + $( + impl ArgumentReflection for $ty { + type Marshalled = Self; - fn marshal(self) -> Self::Marshalled { - self - } + fn type_guid(&self) -> abi::Guid { + Self::type_info().guid + } - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} + fn type_name(&self) -> &str { + Self::type_info().name() + } -impl ArgumentReflection for i16 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for i8 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for u64 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for u32 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ArgumentReflection for u16 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} + fn marshal(self) -> Self::Marshalled { + self + } + } -impl ArgumentReflection for u8 { - type Marshalled = Self; + impl ReturnTypeReflection for $ty { + type Marshalled = Self; - fn type_name(&self) -> &str { - ::type_name() - } + fn type_name() -> &'static str { + Self::type_info().name() + } - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() + fn type_guid() -> abi::Guid { + Self::type_info().guid + } + } + )+ } } -impl ArgumentReflection for bool { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} +impl_primitive_type!( + i8,i16,i32,i64,isize, + u8,u16,u32,u64,usize, + f32,f64, + bool +); impl ArgumentReflection for () { type Marshalled = Self; @@ -288,362 +121,78 @@ impl ArgumentReflection for () { } } -impl ArgumentReflection for *const u8 { - type Marshalled = Self; - - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } +impl ReturnTypeReflection for () { + type Marshalled = (); - fn type_guid(&self) -> abi::Guid { - ::type_guid() + fn type_name() -> &'static str { + "core::empty" } } -impl ArgumentReflection for *mut u8 { +impl ArgumentReflection for *const T +where + *const T: HasStaticTypeInfo +{ type Marshalled = Self; - fn type_name(&self) -> &str { - ::type_name() - } - - fn marshal(self) -> Self::Marshalled { - self - } - fn type_guid(&self) -> abi::Guid { - ::type_guid() + Self::type_info().guid } -} - -impl ArgumentReflection for *const abi::TypeInfo { - type Marshalled = Self; fn type_name(&self) -> &str { - "*const TypeInfo" + Self::type_info().name() } fn marshal(self) -> Self::Marshalled { self } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } } -impl ArgumentReflection for *const std::ffi::c_void { +impl ReturnTypeReflection for *const T + where + *const T: HasStaticTypeInfo +{ type Marshalled = Self; - fn type_name(&self) -> &str { - "*const core::void" - } - - fn marshal(self) -> Self::Marshalled { - self + fn type_guid() -> abi::Guid { + Self::type_info().guid } - fn type_guid(&self) -> abi::Guid { - ::type_guid() + fn type_name() -> &'static str { + Self::type_info().name() } } -impl ArgumentReflection for *mut std::ffi::c_void { +impl ArgumentReflection for *mut T + where + *mut T: HasStaticTypeInfo +{ type Marshalled = Self; - fn type_name(&self) -> &str { - "*mut core::void" - } - - fn marshal(self) -> Self::Marshalled { - self - } - fn type_guid(&self) -> abi::Guid { - ::type_guid() + Self::type_info().guid } -} - -impl ArgumentReflection for *const *mut std::ffi::c_void { - type Marshalled = Self; fn type_name(&self) -> &str { - "*const *mut core::void" + Self::type_info().name() } fn marshal(self) -> Self::Marshalled { self } - - fn type_guid(&self) -> abi::Guid { - ::type_guid() - } -} - -impl ReturnTypeReflection for *const abi::TypeInfo { - type Marshalled = Self; - - fn type_name() -> &'static str { - "*const TypeInfo" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for *const std::ffi::c_void { - type Marshalled = Self; - - fn type_name() -> &'static str { - "*const core::void" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for *mut std::ffi::c_void { - type Marshalled = Self; - - fn type_name() -> &'static str { - "*mut core::void" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for *const *mut std::ffi::c_void { - type Marshalled = Self; - - fn type_name() -> &'static str { - "*const *mut core::void" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for f64 { - type Marshalled = f64; - - fn type_name() -> &'static str { - "core::f64" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for f32 { - type Marshalled = f32; - - fn type_name() -> &'static str { - "core::f32" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -#[cfg(target_pointer_width = "64")] -impl ReturnTypeReflection for isize { - type Marshalled = isize; - - fn type_name() -> &'static str { - "core::i64" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -#[cfg(target_pointer_width = "32")] -impl ReturnTypeReflection for isize { - type Marshalled = isize; - - fn type_name() -> &'static str { - "core::i32" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for i64 { - type Marshalled = i64; - - fn type_name() -> &'static str { - "core::i64" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for i32 { - type Marshalled = i32; - - fn type_name() -> &'static str { - "core::i32" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for i16 { - type Marshalled = i16; - - fn type_name() -> &'static str { - "core::i16" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for i8 { - type Marshalled = i8; - - fn type_name() -> &'static str { - "core::i8" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } } -#[cfg(target_pointer_width = "64")] -impl ReturnTypeReflection for usize { - type Marshalled = usize; - - fn type_name() -> &'static str { - "core::u64" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -#[cfg(target_pointer_width = "32")] -impl ReturnTypeReflection for usize { - type Marshalled = usize; - - fn type_name() -> &'static str { - "core::u32" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for u64 { - type Marshalled = u64; - - fn type_name() -> &'static str { - "core::u64" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for u32 { - type Marshalled = u32; - - fn type_name() -> &'static str { - "core::u32" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for u16 { - type Marshalled = u16; - - fn type_name() -> &'static str { - "core::u16" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for u8 { - type Marshalled = u8; - - fn type_name() -> &'static str { - "core::u8" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for bool { - type Marshalled = bool; - - fn type_name() -> &'static str { - "core::bool" - } - - fn type_guid() -> abi::Guid { - ::type_info().guid - } -} - -impl ReturnTypeReflection for () { - type Marshalled = (); - - fn type_name() -> &'static str { - "core::empty" - } -} - -impl ReturnTypeReflection for *const u8 { +impl ReturnTypeReflection for *mut T +where + *mut T: HasStaticTypeInfo +{ type Marshalled = Self; - fn type_name() -> &'static str { - "*const core::u8" - } - fn type_guid() -> abi::Guid { - ::type_info().guid + Self::type_info().guid } -} - -impl ReturnTypeReflection for *mut u8 { - type Marshalled = Self; fn type_name() -> &'static str { - "*mut core::u8" - } - - fn type_guid() -> abi::Guid { - <*mut u8>::type_info().guid + Self::type_info().name() } -} +} \ No newline at end of file