Skip to content

Commit ad20074

Browse files
committed
Use more #[repr(transparent)]
Fixes #46
1 parent 185c7d9 commit ad20074

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This library is still subject to breaking changes, but it is already possible to
88
Creating custom WinRT classes using inheritance is not yet supported, so it is currently not possible to create user interfaces using *XAML*.
99

1010
## Prerequisites
11-
Using this crate requires at least Rust 1.25.
11+
Using this crate requires at least Rust 1.28.
1212
Additional nightly features (e.g. using specialization) can be enabled with the `nightly` Cargo feature.
1313

1414
## Design

src/cominterfaces.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ DEFINE_IID!(IID_IAgileObject, 0x94EA2B94, 0xE9CC, 0x49E0, 0xC0, 0xFF, 0xEE, 0x64
3737

3838
/// Interface that marks an object as agile.
3939
/// It inherits from `IUnknown` and does not have additional members.
40-
#[repr(C)] #[derive(Debug)]
40+
#[repr(transparent)]
41+
#[derive(Debug)]
4142
pub struct IAgileObject {
4243
lpVtbl: *const IUnknownVtbl // IAgileObject has no methods besides what IUnknown has
4344
}

src/hstring.rs

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn zero_header() -> HSTRING_HEADER {
4747

4848
/// A reference to either an `HString`, a `FastHString`, or a raw null-terminated UTF-16 buffer.
4949
#[derive(Copy, Clone)]
50+
#[repr(transparent)]
5051
pub struct HStringReference<'a>(HSTRING_HEADER, PhantomData<&'a ()>);
5152

5253
impl<'a> HStringReference<'a> {
@@ -164,6 +165,7 @@ impl<'a> Deref for HStringReference<'a> {
164165
/// functions. Creating a new `FastHString` is faster than creating an instance of `HString`
165166
/// because it eliminates an additional allocation. Furthermore, obtaining a `HStringArg` from
166167
/// a `FastHString` is basically free, which is not the case for `HString`.
168+
#[repr(transparent)]
167169
pub struct FastHString(HSTRING_HEADER);
168170

169171
impl FastHString {
@@ -341,6 +343,7 @@ impl<'a> Deref for FastHString {
341343
/// References of this type are passed to WinRT functions. You can not create a value of
342344
/// this type, only references can exist and are obtained via (automatic) dereferencing of
343345
/// `FastHString` or `HStringReference`.
346+
#[repr(transparent)]
344347
pub struct HStringArg(HSTRING_HEADER);
345348

346349
impl HStringArg {
@@ -358,6 +361,7 @@ impl HStringArg {
358361
/// the containing `HSTRING` might be null (empty string), and null references
359362
/// are not allowed. In order to obtain an `&HStringArg` from an `HString`,
360363
/// first create an `HStringReference` using `make_reference()`.
364+
#[repr(transparent)]
361365
pub struct HString(HSTRING);
362366

363367
impl HString {

src/rt/mod.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use self::gen::windows::foundation::collections::{
2020
};
2121

2222
/// Represents a single UTF-16 character. This is the standard character type in WinRT.
23-
#[derive(Clone, Copy)] #[repr(C)]
23+
#[derive(Clone, Copy)]
24+
#[repr(transparent)]
2425
pub struct Char(pub ::w::ctypes::wchar_t); // TODO: deref to u16
2526

2627
/// Marker trait for all Windows Runtime interfaces. They must inherit from `IInspectable`.
@@ -276,11 +277,11 @@ macro_rules! RT_INTERFACE {
276277
($(#[$attr:meta])* basic $interface:ident ($vtbl:ident) : $pinterface:ident ($pvtbl:ty) [$iid:ident]
277278
{}
278279
) => {
279-
#[repr(C)] #[allow(missing_copy_implementations)] #[doc(hidden)]
280+
#[repr(transparent)] #[allow(missing_copy_implementations)] #[doc(hidden)]
280281
pub struct $vtbl {
281282
pub parent: $pvtbl
282283
}
283-
$(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)]
284+
$(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)]
284285
pub struct $interface {
285286
lpVtbl: *const $vtbl
286287
}
@@ -329,7 +330,7 @@ macro_rules! RT_INTERFACE {
329330
$(,$p: $t)*
330331
) -> $rtr)+
331332
}
332-
$(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)]
333+
$(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)]
333334
pub struct $interface {
334335
lpVtbl: *const $vtbl
335336
}
@@ -378,7 +379,7 @@ macro_rules! RT_INTERFACE {
378379
$(,$p: $t)*
379380
) -> $rtr)+
380381
}
381-
$(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)]
382+
$(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)]
382383
pub struct $interface<$t1> where $t1: RtType {
383384
lpVtbl: *const $vtbl<$t1>,
384385
}
@@ -423,7 +424,7 @@ macro_rules! RT_INTERFACE {
423424
$(,$p: $t)*
424425
) -> $rtr)+
425426
}
426-
$(#[$attr])* #[repr(C)] #[allow(missing_copy_implementations)]
427+
$(#[$attr])* #[repr(transparent)] #[allow(missing_copy_implementations)]
427428
pub struct $interface<$t1, $t2> where $t1: RtType, $t2: RtType {
428429
lpVtbl: *const $vtbl<$t1, $t2>,
429430
}
@@ -649,7 +650,8 @@ macro_rules! DEFINE_CLSID {
649650

650651
macro_rules! RT_ENUM {
651652
{enum $name:ident : $t:ty { $($variant:ident = $value:expr,)+ }} => {
652-
#[repr(C)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
653+
#[repr(transparent)]
654+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
653655
#[allow(non_upper_case_globals)]
654656
pub struct $name(pub $t);
655657

0 commit comments

Comments
 (0)