Polling a future in an enum variant

I'm playing around with futures and the hyper crate and I've come across a problem I just cannot get past and I don't understand why. Here is the error I get,

no method named `poll` found for struct `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, errors::Error>> + 'static)>>` in the current scope
method not found in `std::pin::Pin<std::boxed::Box<(dyn std::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, errors::Error>> + 'static)>>`rustc(E0599)

and here is the relevant code,

type Handler<R> = Box<dyn Fn(Request<Body>, Builder) -> Pin<Box<dyn Future<Output = Result<R, Error>>>> + Send + Sync>;

enum RequestFutureProgress {
    Route(Pin<Box<dyn Future<Output = Result<Response<Body>, Error>>>>)

struct RequestFuture {
    req: Request<Body>,
    progress: RequestFutureProgress,
    handler: Handler<Response<Body>>,

impl Future for RequestFuture {
    type Output = Result<Response<Body>, Error>;

    fn poll (self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        match self.progress {
            RequestFutureProgress::Received => {
                self.progress = RequestFutureProgress::Route((*self.handler)(self.req, Response::builder()));
            RequestFutureProgress::Route(response) => {
                match response.poll(cx) { /* error is at this call to poll */
                    Poll::Ready(r) => Poll::Ready(r),
                    Poll::Pending => Poll::Pending

I'm really confused. I've tried changing the type signature in the enum variant, but I cant find a way that will not produce errors.

Is this error related to the type signature in the enum? Is there a way to do this properly? What is the actual meaning of the error?

Try response.as_mut().poll(cx)

The signature for poll requires that pin has an exclusive reference to what’s inside.

That was it.

Being a bit new to Rust, sometimes these things can be tricky.

As as aside, does this mean that calling some_struct.method() is the same as SomeImpl::method(some_struct)?

If that makes sense.

Almost. If the method takes self by reference or mutable reference, the compiler will implicitly add it in the first variant.

But yes, you can certainly write method calls like that.

Also the method-call syntax some_struct.method() invokes the method look-up procedure, which involves dereferencing, auto-referencing and searching of inherent methods, trait bounds methods and other visible trait methods. The procedure is described here, here and here.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.