-
Notifications
You must be signed in to change notification settings - Fork 486
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
Add first-class async authoring support #540
Comments
Also note: the |
WinRT provides four "special" interfaces for modeling async operations. In theory, you could provide an implementation of struct Resolver : implements<Resolver, IUriToStreamResolver>
{
IAsyncOperation<IInputStream> UriToStreamAsync(Uri const& uri)
{
auto stream = co_await <something> // load stuff asynchronously...
// apply any transformation...
co_return stream;
}
}; This depends on #81 (support for implementing generic interfaces is still outstanding) and will likely use async funtions like this: windows-rs/examples/ocr/src/main.rs Lines 12 to 27 in d82a523
I'll rename this issue to track the addition of first-class async support. |
windows::web::IUriToStreamResolver
So there's no way to implement the URI to stream resolver with the Rust API currently? |
You can implement the |
Yeah we really considered using cpp to do it, and it might be the path we follow. |
But Noah just did something crazy and it might work: impl MyResolver {
pub fn uri_to_stream_async<'a, T0__: Into<Param<'a, Uri>>>(
&self,
uri: T0__
) -> windows::Result<IAsyncOperation<IInputStream>> {
let file = StorageFile::get_file_from_path_async("test.html").unwrap();
loop {
if file.status().unwrap() != AsyncStatus::Completed {
continue
}
break Ok(file.get().unwrap().open_sequential_read_async().unwrap())
}
}
} |
Ha 😉 - that's basically what I was suggesting - use another implementation. This specifically is cheating slightly, but hey if it works for you that's fine. I'd just tweak it slightly: pub fn uri_to_stream_async(&self, _: &Option<Uri>) -> Result<IAsyncOperation<IInputStream>> {
StorageFile::get_file_from_path_async("test.html")?
.get()?
.open_sequential_read_async()
} The caveat is that obviously its not that async since |
I believe we can use an synchronous approach until first-class support is available here :) we're just playing around with the libraries and just by having a URI resolver would be great :) Thanks for your help! |
|
Okay, I've implemented your suggestion like so: #[implement(windows::web::IUriToStreamResolver)]
#[derive(Clone)]
struct CustomResolver();
impl CustomResolver {
pub fn uri_to_stream_async<'a, T0__: Into<Param<'a, Uri>>>(
&self,
uri: T0__,
) -> windows::Result<IAsyncOperation<IInputStream>> {
StorageFile::get_file_from_path_async("index.html")?
.get()?
.open_sequential_read_async()
}
}
impl<'a> Into<windows::Param<'a,windows::web::IUriToStreamResolver >> for CustomResolver {
fn into(self) -> windows::Param<'a,windows::web::IUriToStreamResolver > {
::windows::Param::Owned(::std::convert::Into::into(::std::clone::Clone::clone(&self)))
}
} However, when I use it: let res = CustomResolver();
Ok(self.webview.navigate_to_local_stream_uri(Uri::create_uri(url)?, res)?) It compiles, but I get this error at runtime:
Any ideas? Thanks for your help! |
You just created the Uri the wrong way :) your code is working. Now we just need to know how to use the resolver with a byte array instead of a file. This is the code that works: let res = CustomResolver();
self.webview.navigate_to_local_stream_uri(self.webview.build_local_stream_uri("scheme", "index.html")?, res)? |
It doesn't seem like there is a way to create a |
I will just say again that there is no need for |
Just leaving a comment here to share this good write-up by @jonwis: https://jonwis.github.io/notes/winrt-design/async_is_hazardous.html I think I'll provide basic async implementation support in Rust for compatibility with existing APIs for example: let async : IAsyncAction = async(move || {
// some work here
})?; But it does not seem worthwhile to add more comprehensive support such as what I provided for C++/WinRT via coroutines. |
Closing this old issue - doesn't seem to be much demand for such API support. Let me know if you have thoughts to the contrary. 😊 |
Full support for async implementations is now here: #3235 |
Hello! First of all thanks for this project :) I've been writing a Webview app with this crate and it works great!
I'm trying to write a
IUriToStreamResolver
implementation and after a few hours researching and attempting to do it, I found theinterface
API:But now I have no idea how to get an
IAsyncOperation<IInputStream>
to return. All I could do was return aIAsyncOperation<IRandomAccessStream>
with theFileRandomAccessStream
struct. Can you help me? Do I need to implemement another interface?Thanks :)
The text was updated successfully, but these errors were encountered: