Skip to content

Commit

Permalink
Use more #[repr(transparent)]
Browse files Browse the repository at this point in the history
Fixes #46
  • Loading branch information
Boddlnagg committed May 18, 2019
1 parent 185c7d9 commit ad20074
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/cominterfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions src/hstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit ad20074

Please sign in to comment.