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
Mapping Option
to nullable types
#816
Comments
Option
to nullable string-types
Sorry for closing and then immediately reopening... had some struggles with my ability to use a computer. |
I've just noticed #292, almost definitely related? |
Option
to nullable string-typesOption
to nullable types
Hi Daniel, #292 is specifically about WinRT reference parameters. For use bindings::Windows::Win32::System::Threading::*;
fn main() {
unsafe {
CreateProcessA(
"c:\\windows\\notepad.exe",
None,
std::ptr::null_mut(),
std::ptr::null_mut(),
false,
Default::default(),
std::ptr::null_mut(),
None,
&mut STARTUPINFOA {
cb: std::mem::size_of::<STARTUPINFOA>() as _,
..Default::default()
},
&mut PROCESS_INFORMATION::default(),
);
}
} As you can see, the optional string parameters can be replaced with |
I'll keep this issue open to track the remaining usability issues. Ideally, we can write it more simply as follows: CreateProcessA(
"c:\\windows\\notepad.exe",
None,
None,
None,
false,
Default::default(),
None,
None,
&mut Default::default(),
&mut Default::default(),
); |
Thanks for the information! I was too stuck on trying to find a way to map my |
Hi @danielframpton , @kennykerr , Manually unwrapping each if let Some(base) = base {
if let Some(filter) = filter {
unsafe { ldap_searchW(..., base, filter, ....) }
} else {
unsafe { ldap_searchW(..., base, None, ....) }
}
}
else {
if let Some(filter) = filter {
unsafe { ldap_searchW(..., None, filter, ....) }
} else {
unsafe { ldap_searchW(..., None, None, ....) }
}
} Did you manage to get a more generic solution working? Or did I miss a simpler solution? I'm still new to windows-rs, but do you need some help, maybe with something non-generic just to start with |
The recent improvements to string and parameter bindings in 0.39.0 made these kinds of issues a lot less confusing. I'll close this issue for now, but feel free to open new issues for any remaining examples you come across. |
On second thought, I was inspired to deal with a lot of the remaining pointer parameters and turn them into references (#1939) - this gets us much closer to ideal. |
As far as I'm aware, it isn't possible to reflect the nullable strings that are used in certain APIs with
Option
. For example,CreateProcessA
takes many parameters that can be null to represent different configurations for creating the target process.As you would expect, the
None
type would be implicitly converted toPSTR::NULL
which I think might be useful when trying to write idiomatic code, especially libraries.I think something like this should be fairly easy to implement as some string types seem to have hand-written implementation details.Ideally, this is probably something that could be handled for every nullable type and not just
PSTR
and friends, right?The text was updated successfully, but these errors were encountered: