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

Support fixed-size array parameters #1941

Merged
merged 4 commits into from Jul 28, 2022
Merged

Support fixed-size array parameters #1941

merged 4 commits into from Jul 28, 2022

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Jul 27, 2022

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

@kennykerr
Copy link
Collaborator Author

kennykerr commented Jul 27, 2022

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.

Copy link
Contributor

@rylev rylev left a 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), }
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Comment on lines +55 to +64
pub const fn trim_null_end(mut bytes: &[u8]) -> &[u8] {
while let [rest @ .., last] = bytes {
if *last == 0 {
bytes = rest;
} else {
break;
}
}
bytes
}
Copy link
Contributor

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]
}

Copy link
Collaborator Author

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.

@kennykerr kennykerr merged commit d40a518 into master Jul 28, 2022
@kennykerr kennykerr deleted the fixed-arrays branch July 28, 2022 16:31
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.

Add slice support for MemorySize attribute
2 participants