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
Is there support for the std::io::Read
trait somehow?
#1952
Comments
That would actually require a trait with an |
I'm not sure if we really need async in this case, since However, there is an argument to be made that Proof of concept implementation// for demonstration
#![feature(io_read_to_string)]
use std::io;
use windows::{
core::Result,
w,
Foundation::Uri,
Storage::Streams::{DataReader, IInputStream, InputStreamOptions},
Web::Http::{HttpClient, IHttpContent},
};
struct DataReaderWrap(DataReader);
impl io::Read for DataReaderWrap {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut bytes =
self.0
.LoadAsync(buf.len() as u32)
.map_err(|e| io::Error::from_raw_os_error(e.code().0))?
.get()
.map_err(|e| io::Error::from_raw_os_error(e.code().0))? as usize;
bytes = bytes.min(buf.len());
self.0
.ReadBytes(&mut buf[0..bytes])
.map_err(|e| io::Error::from_raw_os_error(e.code().0))
.map(|_| bytes)
}
}
fn main() -> Result<()> {
let uri = Uri::CreateUri(w!("https://www.rust-lang.org/"))?;
let http: HttpClient = HttpClient::new()?;
let content: IHttpContent = http.GetAsync(&uri)?.get()?.Content()?;
let is: IInputStream = content.ReadAsInputStreamAsync()?.get()?;
let reader = DataReader::CreateDataReader(&is)?;
// not needed but this might improve performance
reader.SetInputStreamOptions(InputStreamOptions::ReadAhead)?;
println!("{:?}", io::read_to_string(DataReaderWrap(reader)));
Ok(())
} |
@Nerixyz Fantastic, that piece of code solved my problem completely, thanks so much! And agreed, it would be nice if that was included by default in the crate. |
Neat, thanks for the example @Nerixyz! I'll think about whether this trait can offered automatically. |
std::io::Read
trait somehow?
I'm trying to use
windows::Web::Http::HttpClient
to download a file. I'm usingHttpClient::GetInputStreamAsync
to get anIInputStream
and would like to somehow pass that to a function that expects something that implements thestd::io::Read
trait. Is that somehow possible? Or am I entirely on the wrong track here? It seems to me that somehow on some very general level I'm getting a stream from the Windows API here, and it would be nice if that composed nicely with the standard trait for reading from streams in Rust :)The text was updated successfully, but these errors were encountered: