ergo-pin
is an experiment in providing a more ergonomic stack-pinning API. The primary current use-case for this is in async fn
, but it is also useful for other usecases of the std::pin::Pin
API (e.g. usage of the unstable generator feature).
With ergo-pin
you simply apply an attribute on an item, and within that item you can use the pin!
macro to pin any expression:
fn get_reader() -> impl futures::io::AsyncRead + !Unpin { ... }
fn get_stream() -> impl futures::stream::Stream<Item = u32> + !Unpin { ... }
#[ergo_pin]
async fn foo() -> io::Result<[u8; 4]> {
let mut buffer = [0; 4];
pin!(get_reader()).read_exact(&mut buffer).await?;
buffer
}
#[ergo_pin]
async fn bar() {
let mut stream = pin!(get_stream());
while let Some(value) = stream.next().await {
dbg!(value);
}
}