4 ms·
This ought to be doable automatically, at least a lot better than the current code: let event = CreateEventW( std::ptr::null_mut(), true.
by amluto 6y ago
This ought to be doable automatically, at least a lot better than the current code:
let event = CreateEventW(
std::ptr::null_mut(),
true.into(),
false.into(),
std::ptr::null(),
);
The .into() is silly but tolerable. But the first parameter is a pointer to a struct, and it’s clear enough from the signature that a reference would work. It could be mut to be on the safe side. (Yes, this involves someone making sure that the API doesn’t retain the pointer.). The last argument is, per the prototype, is a string. Admittedly, the Windows API has a truly horrible idea of what a string is, but surely they could do better than using a pointer.
- steveklabnik 6y agoBoth of these parameters can be null, and references cannot be null, so it is not possible to use references directly without losing some functionality. (They could remove the intos if they wanted to, I agree it feels kind of weird but I'm not actually sure if doing it is better or worse, I am conflicted.)
- Cu3PO42 6y agoIt could very well be Option<&mut T>, though. Personally I think having a 1:1 mapping to the C API is reasonable enough as a first target. It should make porting code easy and referencing the official documentation is probably easiest this way. No matter how many heuristics they apply to make a somewhat more idiomatic mapping, it'll never feel right until it's manually designed to fit with Rust conventions. So I'm okay with it as it is now and can hope for a much thicker abstraction in the future.
- orra 6y agoOption<T> would sensibly solve the nullability?
- steveklabnik 6y agoThere are a few different possibilities, and some of them involve Option, yes. But doing this would still possibly create an annotation burden that the parent is complaining about. My comment was trying to be pretty narrowly scoped to "why not use a reference here." You and your sibling are both right in ways!
- orra 6y agoFair enough, and gotcha. Thanks.
- johnsoft 6y agoA pointer->reference transformation is not always correct, because in C, arrays are also passed as pointers. Example: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput https://docs.microsoft.com/en-us/windows/win32/api/winuser/n...