Skip to content

Remove any implied ownership semantics for Win32 structs - #2235

Merged
Kenny Kerr (kennykerr) merged 9 commits into
masterfrom
manually_drop
Dec 8, 2022
Merged

Kenny Kerr (kennykerr) merged 9 commits into
masterfrom
manually_drop

Conversation

@kennykerr

@kennykerr Kenny Kerr (kennykerr) commented Dec 7, 2022 •

Copy link
Copy Markdown
Collaborator

WinRT structs expect any non-blittable fields to be owned and dropped as necessary. Win32 structs don't provide any ownership semantics that can be gleaned from metadata and for the most part require the caller to know whether any resources are owned by the caller or callee of any particular API.

This update solidifies this by ensuring that all non-blittable field types are stored as ManuallyDrop to make that clear. A variant of ManuallyDrop is also provided and used for fields to simplify programming with the Win32 API where such fields must be accessible as references without necessarily taking ownership.

I tested the Direct3D 12 sample and the warnings that Alex Dixon (@polymonster) mentioned are no longer reported when running under a debugger.

Fixes: #1410

}

pub fn abi(&self) -> T::Abi {
unsafe { std::mem::transmute_copy(&self.abi) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Abi not constrained to be Copy? I guess we sometimes set Abi to std::mem::ManuallyDrop which would not be Copy even though semantically it usually should be.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, it can't be Copy. That would make it a lot simple for callers. Rust requires Copy for fields of unions but won't let me make a Copy type without generating considerably more code by essentially unrolling ManuallyDrop types and duplicating their memory layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to correctly release ID3D12Resource for IDXGISwapChain3::ResizeBuffers

3 participants