Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HANDLE types should support Sync trait #396

Closed
lzybkr opened this issue Mar 8, 2017 · 3 comments
Closed

HANDLE types should support Sync trait #396

lzybkr opened this issue Mar 8, 2017 · 3 comments

Comments

@lzybkr
Copy link
Contributor

lzybkr commented Mar 8, 2017

I'm new to Rust, so if my understanding is misguided, do let me know.

I'm trying to use a handle - HMODULE in a static HashMap. This is not allowed because the trait std::marker::Sync is not satisfied on *mut winapi::HINSTANCE__.

I think DECLARE_HANDLE should define a type that supports the Sync trait.
Handles are not pointers even though they're defined that way in winnt.h.

@retep998
Copy link
Owner

retep998 commented Mar 8, 2017

You should be wrapping your handles in a newtype so that you can do stuff like automatically closing the handle when dropped. One of the benefits of such a wrapper is you can assert that it is thread safe like so:

struct Handle(HANDLE);
unsafe impl Sync for Handle {}

Related issue: #360

@retep998 retep998 closed this as completed Mar 8, 2017
@lzybkr
Copy link
Contributor Author

lzybkr commented Mar 8, 2017

I see.

Does it make sense for winapi to define a wrapper type for at least some handle types? So for example, you could have a Raw type that is the pointer, and the wrapper type?

This would also introduce a level of type safety that isn't present in C/C++ with Windows handles.

@retep998
Copy link
Owner

retep998 commented Mar 8, 2017

winapi is strictly raw bindings. It does not attempt to provide wrappers (aside from the minimal amount for COM interfaces) or make anything safe. Anyone is free to create their own library on top of winapi which provides thread safe wrappers.

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

No branches or pull requests

2 participants