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

Use cstr literals where possible #311

Merged
merged 3 commits into from
Aug 29, 2024
Merged

Use cstr literals where possible #311

merged 3 commits into from
Aug 29, 2024

Conversation

jschwe
Copy link
Contributor

@jschwe jschwe commented Aug 28, 2024

Convert usage of byte str and normal str with a \0 to cstr literals where possible

@mrobinson
Copy link
Member

This seems fine though it seems the build is failing on Windows.

@jdm
Copy link
Member

jdm commented Aug 28, 2024

I suspect c strings are i8 on windows? It's a common cross-platform error.

@jschwe jschwe force-pushed the jschwender/c_str branch 4 times, most recently from c1e2639 to ada53a2 Compare August 29, 2024 01:39
Convert usage of byte str and normal str with a `\0`
to cstr literals where possible.
Since CStr::as_ptr() returns `*const c_char` this also allows
removing a nice amount of pointer casts.

On Windows this also allows removing casts to [`LPCSTR`], which
is defined as a `*const CHAR`, where `CHAR` is defined as `c_char`
and `c_char` is taken from the std library if the `std` feature is
enabled.

[`LPCSTR`]: https://docs.rs/winapi/latest/winapi/um/winnt/type.LPCSTR.html

Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>
Fixes pedantic clippy lint `ptr_as_ptr`.

Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>
@jschwe jschwe force-pushed the jschwender/c_str branch 2 times, most recently from ad7ebbd to ab6cd30 Compare August 29, 2024 01:58
Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>
@jschwe
Copy link
Contributor Author

jschwe commented Aug 29, 2024

I suspect c strings are i8 on windows? It's a common cross-platform error.

The nice thing about CStr is that the as_ptr() method documents that it [returns *const c_char](https://doc.rust-lang.org/stable/std/ffi/struct.CStr.html#method.as_ptr. This allows us to remove a lot of the casts in the window specific code.

While removing those needless casts, I stumbled a bit over CreateWindowExA accepting a u16 encoded in a pointer type and added a comment in the source code explaining it.

Copy link
Member

@jdm jdm left a comment

Choose a reason for hiding this comment

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

These changes are great! Thanks for cleaning up the code.

@jdm jdm added this pull request to the merge queue Aug 29, 2024
Merged via the queue into servo:main with commit 1da9c17 Aug 29, 2024
17 checks passed
@jschwe jschwe deleted the jschwender/c_str branch August 29, 2024 03:13
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.

3 participants