From ad200740856d097ac37fdd406692a6df0e0fcae2 Mon Sep 17 00:00:00 2001 From: Patrick Reisert Date: Sat, 18 May 2019 11:56:45 +0200 Subject: [PATCH] Use more #[repr(transparent)] Fixes #46 --- README.md | 2 +- src/cominterfaces.rs | 3 ++- src/hstring.rs | 4 ++++ src/rt/mod.rs | 16 +++++++++------- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9136bce..8a32665 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This library is still subject to breaking changes, but it is already possible to Creating custom WinRT classes using inheritance is not yet supported, so it is currently not possible to create user interfaces using *XAML*. ## Prerequisites -Using this crate requires at least Rust 1.25. +Using this crate requires at least Rust 1.28. Additional nightly features (e.g. using specialization) can be enabled with the `nightly` Cargo feature. ## Design diff --git a/src/cominterfaces.rs b/src/cominterfaces.rs index 0548763..f304df2 100644 --- a/src/cominterfaces.rs +++ b/src/cominterfaces.rs @@ -37,7 +37,8 @@ DEFINE_IID!(IID_IAgileObject, 0x94EA2B94, 0xE9CC, 0x49E0, 0xC0, 0xFF, 0xEE, 0x64 /// Interface that marks an object as agile. /// It inherits from `IUnknown` and does not have additional members. -#[repr(C)] #[derive(Debug)] +#[repr(transparent)] +#[derive(Debug)] pub struct IAgileObject { lpVtbl: *const IUnknownVtbl // IAgileObject has no methods besides what IUnknown has } diff --git a/src/hstring.rs b/src/hstring.rs index 6dd38d6..8e6a65f 100644 --- a/src/hstring.rs +++ b/src/hstring.rs @@ -47,6 +47,7 @@ fn zero_header() -> HSTRING_HEADER { /// A reference to either an `HString`, a `FastHString`, or a raw null-terminated UTF-16 buffer. #[derive(Copy, Clone)] +#[repr(transparent)] pub struct HStringReference<'a>(HSTRING_HEADER, PhantomData<&'a ()>); impl<'a> HStringReference<'a> { @@ -164,6 +165,7 @@ impl<'a> Deref for HStringReference<'a> { /// functions. Creating a new `FastHString` is faster than creating an instance of `HString` /// because it eliminates an additional allocation. Furthermore, obtaining a `HStringArg` from /// a `FastHString` is basically free, which is not the case for `HString`. +#[repr(transparent)] pub struct FastHString(HSTRING_HEADER); impl FastHString { @@ -341,6 +343,7 @@ impl<'a> Deref for FastHString { /// References of this type are passed to WinRT functions. You can not create a value of /// this type, only references can exist and are obtained via (automatic) dereferencing of /// `FastHString` or `HStringReference`. +#[repr(transparent)] pub struct HStringArg(HSTRING_HEADER); impl HStringArg { @@ -358,6 +361,7 @@ impl HStringArg { /// the containing `HSTRING` might be null (empty string), and null references /// are not allowed. In order to obtain an `&HStringArg` from an `HString`, /// first create an `HStringReference` using `make_reference()`. +#[repr(transparent)] pub struct HString(HSTRING); impl HString { diff --git a/src/rt/mod.rs b/src/rt/mod.rs index c2f6165..ed391a6 100644 --- a/src/rt/mod.rs +++ b/src/rt/mod.rs @@ -20,7 +20,8 @@ use self::gen::windows::foundation::collections::{ }; /// Represents a single UTF-16 character. This is the standard character type in WinRT. -#[derive(Clone, Copy)] #[repr(C)] +#[derive(Clone, Copy)] +#[repr(transparent)] pub struct Char(pub ::w::ctypes::wchar_t); // TODO: deref to u16 /// Marker trait for all Windows Runtime interfaces. They must inherit from `IInspectable`. @@ -276,11 +277,11 @@ macro_rules! RT_INTERFACE { ($(#[$attr:meta])* basic $interface:ident ($vtbl:ident) : $pinterface:ident ($pvtbl:ty) [$iid:ident] {} ) => { - #[repr(C)] #[allow(missing_copy_implementations)] #[doc(hidden)] + #[repr(transparent)] #[allow(missing_copy_implementations)] #[doc(hidden)] pub struct $vtbl { pub parent: $pvtbl } - $(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)] + $(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)] pub struct $interface { lpVtbl: *const $vtbl } @@ -329,7 +330,7 @@ macro_rules! RT_INTERFACE { $(,$p: $t)* ) -> $rtr)+ } - $(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)] + $(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)] pub struct $interface { lpVtbl: *const $vtbl } @@ -378,7 +379,7 @@ macro_rules! RT_INTERFACE { $(,$p: $t)* ) -> $rtr)+ } - $(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)] + $(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)] pub struct $interface<$t1> where $t1: RtType { lpVtbl: *const $vtbl<$t1>, } @@ -423,7 +424,7 @@ macro_rules! RT_INTERFACE { $(,$p: $t)* ) -> $rtr)+ } - $(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)] + $(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)] pub struct $interface<$t1, $t2> where $t1: RtType, $t2: RtType { lpVtbl: *const $vtbl<$t1, $t2>, } @@ -649,7 +650,8 @@ macro_rules! DEFINE_CLSID { macro_rules! RT_ENUM { {enum $name:ident : $t:ty { $($variant:ident = $value:expr,)+ }} => { - #[repr(C)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] + #[repr(transparent)] + #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[allow(non_upper_case_globals)] pub struct $name(pub $t);