Skip to content

Borrowed - #1811

Merged
Kenny Kerr (kennykerr) merged 34 commits into
microsoft:masterfrom
rylev:borrowed
Jul 11, 2022
Merged

Kenny Kerr (kennykerr) merged 34 commits into
microsoft:masterfrom
rylev:borrowed

Conversation

@rylev

@rylev Ryan Levick (rylev) commented Jun 9, 2022 •

Copy link
Copy Markdown
Contributor

This implements the Borrow Type RFC which makes conversion to input parameters much more straightforward.

Changes

There are some changes in this PR from the RFC:

Changes to Borrowed

During development, it was discussed how Borrowed types are always logically optional. This PR adjusts the definition of Borrowed to be a wrapper around <T as Abi>::Abi.

Addition of InParam

Additionally, an InParam type is added which is essentially either an owned T or a Borrowed<'a, T>. This allows us to hold on params that have been converted to the correct type at the last minute dropping them at the end of the function call.

Changes to the user experience

The following are the practical changes to the UX of the windows crate after this change:

  • Most parameters need to be passed as references now instead of being able to pass ownership
  • Working with HSTRING and PCWSTR (and kin) gets worse
    • This now requires the user to do a manual conversion from a string literal to these types before passing them as parameters
  • You used to be able to pass Default::default() to a lot of places but now you need to be explicit with the type
    • For example, URI_CREATE_FLAGS::default()

To view the changes without codegen, look here

@rylev
Ryan Levick (rylev) force-pushed the borrowed branch 4 times, most recently from 7f96cc4 to 9da179c Compare June 10, 2022 14:20
@@ -156,13 +156,13 @@ impl Window {
target.SetTarget(clock);
target.Clear(std::ptr::null());
self.draw_clock()?;
target.SetTarget(previous);
target.SetTarget(previous.as_ref());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to have lost my previous comments on the other commit. Ideally this would be &previous if we can make Borrowed creatable from &Option<T>.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is not possible as Rust's type system doesn't know that &Option<T> does not implement Into<&'a T> and thus we get a conflicting impl error if I add an implementation to make &Option<T> work. It's unfortunate, but workable IMO.

Comment thread crates/samples/ocr/src/main.rs Resolved
@rylev
Ryan Levick (rylev) force-pushed the borrowed branch 3 times, most recently from 6306406 to 53f70a1 Compare June 13, 2022 09:21
@rylev

Ryan Levick (rylev) commented Jun 13, 2022 •

Copy link
Copy Markdown
Contributor Author

As far as I'm aware, the following are the things left to address before we merge this:

  • WinRT interfaces that are non-exclusive currently need to be manually converted to the expected interface before being passed to WinRT methods. It would be nice if users didn't have to do this casting.
  • Class hierarchies
  • GenericParameters must always be borrowed even if they end up being blittable. Most likely, we can't do anything about this though...
  • Instead of being able to pass None, we now require instantiating many handle types: i.e., None => HWND::default().
  • TODO

Comment thread crates/tests/handles/tests/win.rs Resolved
@rylev
Ryan Levick (rylev) force-pushed the borrowed branch 2 times, most recently from 60bfd30 to 2dd65ba Compare June 13, 2022 17:54
Comment thread crates/tests/winrt/tests/async.rs Resolved
Comment thread crates/tests/map/tests/test.rs Resolved
@@ -3,7 +3,7 @@ use windows::Foundation::{IStringable, PropertyValue, Uri};

#[test]
fn class() -> windows::core::Result<()> {
let uri = Uri::CreateUri("http://kennykerr.ca")?;
let uri = Uri::CreateUri(&windows::core::HSTRING::from("http://kennykerr.ca"))?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we just need to have a universal PCWSTR type for all wide string input parameters to avoid the complication of dealing with the various different types. Then we can specialize that one to handle all the marshaling under the hood - that should simplify and harmonize the various calling patterns for strings. Not for this PR but need to land that fairly closely along with this.

@filips123

Copy link
Copy Markdown

Are there any plans to address the situation with string parameters? Having to use HSTRING::from around every param is kinda annoying and I would expect the windows crate to take care of this automatically (as it is supposed to provide a more friendly API). Would it be possible to implement From<&str>, From<String>, etc. on PCWSTR and other string types or handle the conversion automatically in some other way?

@rylev

Copy link
Copy Markdown
Contributor Author

This is a tricky question: Automatically converting from &str or String to HSTRING is arguably too much to happen implicitly. This is because such conversions require completely new allocations since Rust string types are utf-8 while HSTRING is utf-16. I would argue that in such cases requiring the extra call to HSTRING::from is warranted as it makes this cost explicit.

In fact, this was the reason that the previous alloc feature was present as it allowed a way for these implicit conversions to happen. We could bring that feature back, but I'm not sure it's a good idea.

For string literals, the w! macro should be preferred. When not dealing with literals, you'll indeed have to manually convert between Rust types and the Windows types. You might want to look into using a third-party crate that allows string like functions on wide strings. This might allow a more convenient way to stay in a representation Windows expects.

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.

3 participants