-
Notifications
You must be signed in to change notification settings - Fork 515
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
Support fixed-size array parameters #1941
Conversation
I used BCrypt to validate a lot of this work. https://github.com/microsoft/windows-rs/blob/fixed-arrays/crates/tests/bcrypt/tests/tests.rs It's now a lot easier to reason about and use such APIs from Rust thanks to the awareness of optional and array parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a nice change!
} | ||
quote! { #name.as_ptr() } | ||
}; | ||
quote! { ::core::mem::transmute(#map), } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we know this is a pointer does as _
work instead? Would be nice to avoid another use of transmute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not just yet - it still requires a bit more untangling before that will work. There are a few cases where the underlying types aren't simply the pointer equivalent of the slice. But I'd love to get there.
pub const fn trim_null_end(mut bytes: &[u8]) -> &[u8] { | ||
while let [rest @ .., last] = bytes { | ||
if *last == 0 { | ||
bytes = rest; | ||
} else { | ||
break; | ||
} | ||
} | ||
bytes | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this is const?
Could be rewritten as:
fn trim_null_end(bytes: &[u8]) -> &[u8] {
let end = bytes
.iter()
.rposition(|&s| s != 0)
.map(|i| i + 1)
.unwrap_or(bytes.len());
&bytes[..end]
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't but the iterator approach seems overly complicated in comparison.
Following on #1939 this update adds support for Win32 parameters that are declared as fixed-size arrays. This avoid any confusion around treating these arrays as references. It also more consistently applies
Option
to optional parameters rather than trying to transform empty slices into null pointers under the hood.Unfortunately, it also highlighted a few more metadata bugs:
microsoft/win32metadata#1008
microsoft/win32metadata#1010
microsoft/win32metadata#1011
microsoft/win32metadata#1012
microsoft/win32metadata#1014
Fixes #1938