-
Notifications
You must be signed in to change notification settings - Fork 532
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
Provide wrappers for APIs which return variable-length strings #3475
Labels
question
Further information is requested
Comments
There's already good support for paths and environment variables in the Rust Standard Library so I'm not sure this is generally useful for most Rust developers to warrant dedicated support here. |
It's not specifically for env vars, but generally for APIs which return
variable-length strings.
Examples:
* QueryFullProcessImageName
* GetModuleFileName
* GetWindowsDirectory
* GetLongPathName
For a specific example that I encountered, take a look at the following
code in the rust compiler:
https://github.com/rust-lang/rust/blob/279604832887abeaea0cb31c20590c43dea497ae/compiler/rustc_session/src/filesearch.rs#L119-L152
They're using GetModuleFileNameW with a buffer of 1024, and hope for the
best. It's wasteful in most cases, and can still fail for really long
paths. Ideally, there'd be a wrapper which will start with 260 bytes on the
stack (avoiding heap most of the time), and then reallocate if needed, just
like WIL does. In order to not have everybody reinvent the wheel, having
windows-rs do it (like WIL does) will help have more robust, less buggy
software for Windows in rust.
Another example is GetLongPathName which I needed recently. I found
the normpath crate which wraps it nicely. Look at its implementation:
https://github.com/dylni/normpath/blob/d65453fdb39ee4091846732477975fb665b1a7dd/src/windows/mod.rs#L153
It uses the "winapi_path", "winapi_buffered" functions to handle the
variable-length returned path. It's 100 lines of subtle, unsafe code. I
hope they did it right, but I don't trust any crate author (or myself) to
get it right. Ideally, it should be part of a Microsoft crate, either here
or in a more high-level crate.
I hope it makes sense. Thanks.
…On Fri, Feb 7, 2025 at 4:34 AM Kenny Kerr ***@***.***> wrote:
There's already good support for paths and environment variables in the
Rust Standard Library so I'm not sure this is generally useful for most
Rust developers to warrant dedicated support here.
—
Reply to this email directly, view it on GitHub
<#3475 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABMDRPGV5CZJP2YY63WXDST2OQLSVAVCNFSM6AAAAABWUR2IVGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNBRG44DGOBSG4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggestion
Similar to WIL:
https://github.com/microsoft/wil/wiki/Win32-helpers#predefined-adapters-for-functions-that-return-variable-length-strings
The text was updated successfully, but these errors were encountered: