I'm trying to get data using crates_io_api
.
I attempted to get data from a stream, but
I can not get it to work.
AsyncClient::all_crates
returns an impl Stream
. How do I get data from it? It would be helpful if you provide code.
I checked out the async book but it didn't work. Thank you.
Here's my current code.
use crates_io_api::{AsyncClient, Error};
use futures::stream::StreamExt;
async fn get_all(query: Option<String>) -> Result<crates_io_api::Crate, Error> {
// Instantiate the client.
let client = AsyncClient::new(
"test ([email protected])",
std::time::Duration::from_millis(10000),
)?;
let stream = client.all_crates(query);
// what should I do after?
// ERROR: `impl Stream cannot be unpinned`
while let Some(item) = stream.next().await {
// ...
}
}
This looks like a mistake on the side of
crates_io_api
. Getting thenext
element of aStream
requires that theStream
isUnpin
:Because
Next
stores a reference toSelf
, you must guarantee thatSelf
is not moved during the process, or risk pointer invalidation. This is what theUnpin
marker trait represents.crates_io_api
does not provide this guarantee (although they could, and should be), so you must make it yourself. To convert a!Unpin
type to aUnpin
type, you can pin it to a heap allocation:Or you can pin it to the stack with the
pin_mut!
/pin!
macro:Alternatively, you could use a combinator that does not require
Unpin
such asfor_each
: