Skip to content

Commit

Permalink
Avoid lifetime inference errors
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Sep 5, 2020
1 parent 38db3c1 commit c447e5e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
17 changes: 3 additions & 14 deletions futures-test/src/assert_unmoved.rs
Expand Up @@ -40,7 +40,7 @@ impl<T> AssertUnmoved<T> {
}
}

fn poll_with<U>(mut self: Pin<&mut Self>, f: impl FnOnce(Pin<&mut T>) -> U) -> U {
fn poll_with<'a, U>(mut self: Pin<&'a mut Self>, f: impl FnOnce(Pin<&'a mut T>) -> U) -> U {
let cur_this = &*self as *const Self;
if self.this_ptr.is_null() {
// First time being polled
Expand Down Expand Up @@ -158,19 +158,8 @@ impl<S: AsyncSeek> AsyncSeek for AssertUnmoved<S> {
}

impl<R: AsyncBufRead> AsyncBufRead for AssertUnmoved<R> {
fn poll_fill_buf(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
// FIXME: We cannot use `poll_with` here because it causes a lifetime error.
let cur_this = &*self as *const Self;
if self.this_ptr.is_null() {
// First time being polled
*self.as_mut().project().this_ptr = cur_this;
} else {
assert_eq!(
self.this_ptr, cur_this,
"AssertUnmoved moved between poll calls"
);
}
self.project().inner.poll_fill_buf(cx)
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
self.poll_with(|r| r.poll_fill_buf(cx))
}

fn consume(self: Pin<&mut Self>, amt: usize) {
Expand Down
20 changes: 4 additions & 16 deletions futures-test/src/interleave_pending.rs
Expand Up @@ -57,10 +57,10 @@ impl<T> InterleavePending<T> {
self.inner
}

fn poll_with<U>(
self: Pin<&mut Self>,
fn poll_with<'a, U>(
self: Pin<&'a mut Self>,
cx: &mut Context<'_>,
f: impl FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<U>,
f: impl FnOnce(Pin<&'a mut T>, &mut Context<'_>) -> Poll<U>,
) -> Poll<U> {
let this = self.project();
if *this.pended {
Expand Down Expand Up @@ -185,19 +185,7 @@ impl<S: AsyncSeek> AsyncSeek for InterleavePending<S> {

impl<R: AsyncBufRead> AsyncBufRead for InterleavePending<R> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
// FIXME: We cannot use `poll_with` here because it causes a lifetime error.
let this = self.project();
if *this.pended {
let next = this.inner.poll_fill_buf(cx);
if next.is_ready() {
*this.pended = false;
}
next
} else {
cx.waker().wake_by_ref();
*this.pended = true;
Poll::Pending
}
self.poll_with(cx, R::poll_fill_buf)
}

fn consume(self: Pin<&mut Self>, amount: usize) {
Expand Down

0 comments on commit c447e5e

Please sign in to comment.