2
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;
use std::time::Duration;

// pyO3 module
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

use std::future::Future;

#[pyfunction]
pub fn start_server() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    let pool = ThreadPool::new(4);

    for stream in listener.incoming() {
        let stream = stream.unwrap();

        pool.execute(|| {
            let rt = tokio::runtime::Runtime::new().unwrap();
            handle_connection(stream, rt, &test_helper);
        });
    }
}

#[pymodule]
pub fn roadrunner(_: Python<'_>, m: &PyModule) -> PyResult<()> {
    m.add_wrapped(wrap_pyfunction!(start_server))?;
    Ok(())
}

async fn read_file(filename: String) -> String {
    let con = tokio::fs::read_to_string(filename).await;
    con.unwrap()
}

async fn test_helper(contents: &mut String, filename: String) {
    // this function will accept custom function and return
    *contents = tokio::task::spawn(read_file(filename.clone()))
        .await
        .unwrap();
}

pub fn handle_connection(
    mut stream: TcpStream,
    runtime: tokio::runtime::Runtime,
    test: &dyn Fn(&mut String, String) -> (dyn Future<Output = ()> + 'static),
) {
    let mut buffer = [0; 1024];
    stream.read(&mut buffer).unwrap();

    let get = b"GET / HTTP/1.1\r\n";
    let sleep = b"GET /sleep HTTP/1.1\r\n";

    let (status_line, filename) = if buffer.starts_with(get) {
        ("HTTP/1.1 200 OK", "hello.html")
    } else if buffer.starts_with(sleep) {
        thread::sleep(Duration::from_secs(5));
        ("HTTP/1.1 200 OK", "hello.html")
    } else {
        ("HTTP/1.1 404 NOT FOUND", "404.html")
    };

    let mut contents = String::new();
    let future = test_helper(&mut contents, String::from(filename));
    runtime.block_on(future);

    let response = format!(
        "{}\r\nContent-Length: {}\r\n\r\n{}",
        status_line,
        contents.len(),
        contents
    );

    stream.write(response.as_bytes()).unwrap();
    stream.flush().unwrap();
}

I am trying to create a module where I need to pass an async function as an argument. I have passed the element but I am unable to deduce what should I do from the error message. It is telling me that there is some mismatch in type inference.

Here is the error message I am getting on cargo check

error[E0271]: type mismatch resolving `for<'r> <for<'_> fn(&mut String, String) -> impl Future {test_helper} as FnOnce<(&'r mut String, String)>>::Output == (dyn Future<Output = ()> + 'static)`
   --> src/lib.rs:124:43
    |
124 |             handle_connection(stream, rt, &test_helper);
    |                                           ^^^^^^^^^^^^ expected trait object `dyn Future`, found opaque type
...
140 | async fn test_helper(contents: &mut String, filename: String) {
    |                                                               - checked the `Output` of this `async fn`, found opaque type
    |
    = note: while checking the return type of the `async fn`
    = note: expected trait object `(dyn Future<Output = ()> + 'static)`
                found opaque type `impl Future`
    = note: required for the cast to the object type `dyn for<'r> Fn(&'r mut String, String) -> (dyn Future<Output = ()> + 'static)`

error: aborting due to previous error

Please let me know what change should be made here. Thanks in advance.

Sanskar Jethi
  • 544
  • 5
  • 17

1 Answers1

2

You are writing a function type that returns a dyn type, not a reference to it, but the unsized type itself, that is not possible. Every time you want to write something like this, try using a generic instead:

pub fn handle_connection<F>(
    mut stream: TcpStream,
    runtime: tokio::runtime::Runtime,
    test: &dyn Fn(&mut String, String) -> F,
)
where F: Future<Output = ()> + 'static

This now fails with this weird error:

error[E0308]: mismatched types
  --> src/lib.rs:19:43
   |
19 |             handle_connection(stream, rt, &test_helper);
   |                                           ^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected associated type `<for<'_> fn(&mut String, String) -> impl Future {test_helper} as FnOnce<(&mut String, String)>>::Output`
              found associated type `<for<'_> fn(&mut String, String) -> impl Future {test_helper} as FnOnce<(&mut String, String)>>::Output`

But this is expected too, your future is holding a reference to that &mut String you are passing, so it is not 'static anymore. The solution is just to add a lifetime generic parameter:

pub fn handle_connection<'a, F>(
    mut stream: TcpStream,
    runtime: tokio::runtime::Runtime,
    test: &dyn Fn(&'a mut String, String) -> F,
)
where F: Future<Output = ()> + 'a

And now it should compile.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Hi @rodrigo. Thank you for the explanation. But I don't understand what difference does it make to use a generic instead of a single responsibility return type? How does a generic with the specifier work and not the exact return type? – Sanskar Jethi May 25 '21 at 06:54
  • 2
    @SanskarJethi: Because a `dyn T` type is not an exact type, it is an _unsized type_, you'd have to use some kind of reference or `Box` to address it. The actual return type of an `async fn` is unnamed and cannot be typed directly, but using a generic you can let the compiler do the deduction and return the exact type. – rodrigo May 25 '21 at 07:44