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

Unnecessary *mut constraint in LPCTSTR #549

Closed
cr0sh opened this issue Feb 16, 2021 · 6 comments
Closed

Unnecessary *mut constraint in LPCTSTR #549

cr0sh opened this issue Feb 16, 2021 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@cr0sh
Copy link

cr0sh commented Feb 16, 2021

MSDN documentation of WNDCLASSEXW,
states the field lpszClassName has type LPCTSTR which is a pointer to constant string.

However, the generated bindings on WNDCLASSEXW defines the field as *mut 16, which seems overly constrained for constant (t-)strings.

Current workaround is just casting *const pointers into *mut, but it could be more simple to define them as *const pointers.

@kennykerr
Copy link
Collaborator

Yes, this is something we can improve. The struct field in this case includes a "const" attribute in metadata so we just need to ensure that this translates into a *const rather than *mut field.

@kennykerr kennykerr added the enhancement New feature or request label Feb 16, 2021
@kennykerr kennykerr self-assigned this Feb 26, 2021
@kennykerr
Copy link
Collaborator

This is now a little better thanks to the introduction of the PWSTR and PSTR types. Here's an example:

https://github.com/microsoft/windows-rs/blob/master/examples/create_window/src/main.rs

Beyond that, I'd like to introduce const functions or macros that can produce a PWSTR/PSTR from a literal. That's tracked by #581.

@agausmann
Copy link

PWSTR and PSTR still just wrap *mut u8 and *mut u16 respectively. The example you have there has the expression PSTR(b"window\0".as_ptr() as _), which, after type inference, is PSTR(b"window\0".as_ptr() as *mut u8). It's still converting *const u8 to *mut u8, still UB.

@kennykerr
Copy link
Collaborator

Agreed its not yet ideal or safe, but why is it UB? The memory is not actually modified.

@agausmann
Copy link

I was mistaken, it's not directly UB, I believe I was thinking of transmute<&T, &mut T> which is not the same at all. But it can definitely be unsound if the interface asks for *mut u8 and you're casting a *const u8.

The struct field in this case includes a "const" attribute in metadata so we just need to ensure that this translates into a *const rather than *mut field.

If they're considered to be const pointers on the other side of the FFI boundary, I guess it's okay.

@kennykerr
Copy link
Collaborator

These are now const thanks to #1468

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants