Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't compile the http server example #1765

Closed
dotSlashLu opened this issue Nov 12, 2019 · 4 comments
Closed

Can't compile the http server example #1765

dotSlashLu opened this issue Nov 12, 2019 · 4 comments
Labels
T-docs Topic: documentation

Comments

@dotSlashLu
Copy link

dotSlashLu commented Nov 12, 2019

Version

rustc 1.39.0 (4560ea788 2019-11-04)

└── tokio v0.2.0-alpha.6
    ├── tokio-codec v0.2.0-alpha.6
    │   └── tokio-io v0.2.0-alpha.6
    ├── tokio-executor v0.2.0-alpha.6
    │   ├── tokio-sync v0.2.0-alpha.6
    ├── tokio-fs v0.2.0-alpha.6
    │   ├── tokio-executor v0.2.0-alpha.6 (*)
    │   ├── tokio-io v0.2.0-alpha.6 (*)
    │   └── tokio-sync v0.2.0-alpha.6 (*)
    ├── tokio-io v0.2.0-alpha.6 (*)
    ├── tokio-macros v0.2.0-alpha.6
    ├── tokio-net v0.2.0-alpha.6
    │   ├── tokio-codec v0.2.0-alpha.6 (*)
    │   ├── tokio-executor v0.2.0-alpha.6 (*)
    │   ├── tokio-io v0.2.0-alpha.6 (*)
    │   ├── tokio-sync v0.2.0-alpha.6 (*)
    ├── tokio-sync v0.2.0-alpha.6 (*)
    ├── tokio-timer v0.3.0-alpha.6
    │   ├── tokio-executor v0.2.0-alpha.6 (*)
    │   └── tokio-sync v0.2.0-alpha.6 (*)

Platform

Linux jarvis.local 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Description

I tried to compile the http server example on the documentation index, but I can't get it compiled with type annotation error and mainly this no method named unwrap found for type impl std::future::Future in the current scope error I don't know how to solve since this is my first try to update to tokio-0.2.x and std future.

error[E0599]: no method named `unwrap` found for type `impl std::future::Future` in the current scope
  --> src/main.rs:13:49
   |
13 |     let mut listener = TcpListener::bind(&addr).unwrap();
   |                                                 ^^^^^^ method not found in `impl std::future::Future`

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:25:17
   |
25 |             let mut buf = [0; 1024];
   |                 ^^^^^^^ cannot infer type for `{integer}`
   |
note: the type is part of the `async` object because of this `await`
  --> src/main.rs:55:33
   |
55 |                 if let Err(e) = socket.write_all(&buf[0..n]).await {
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:33:48
   |
33 |                 let n = match socket.read(&mut buf).await {
   |                                                ^^^ cannot infer type for `{integer}`
   |
note: the type is part of the `async` object because of this `await`
  --> src/main.rs:33:31
   |
33 |                 let n = match socket.read(&mut buf).await {
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:33:43
   |
33 |                 let n = match socket.read(&mut buf).await {
   |                                           ^^^^^^^^ cannot infer type for `{integer}`
   |
note: the type is part of the `async` object because of this `await`
  --> src/main.rs:33:31
   |
33 |                 let n = match socket.read(&mut buf).await {
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:55:51
   |
55 |                 if let Err(e) = socket.write_all(&buf[0..n]).await {
   |                                                   ^^^ cannot infer type for `{integer}`
   |
note: the type is part of the `async` object because of this `await`
  --> src/main.rs:55:33
   |
55 |                 if let Err(e) = socket.write_all(&buf[0..n]).await {
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:55:51
   |
55 |                 if let Err(e) = socket.write_all(&buf[0..n]).await {
   |                                                   ^^^^^^^^^ cannot infer type for `{integer}`
   |
note: the type is part of the `async` object because of this `await`
  --> src/main.rs:55:33
   |
55 |                 if let Err(e) = socket.write_all(&buf[0..n]).await {
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0698]: type inside `async` object must be known in this context
  --> src/main.rs:55:50
   |
55 |                 if let Err(e) = socket.write_all(&buf[0..n]).await {
   |                                                  ^^^^^^^^^^ cannot infer type for `{integer}`
   |
note: the type is part of the `async` object because of this `await`
  --> src/main.rs:55:33
   |
55 |                 if let Err(e) = socket.write_all(&buf[0..n]).await {
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors
@ghost
Copy link

ghost commented Nov 12, 2019

Hi @dotSlashLu example is working for me after couple of changes mentioned as a comment in the following code:

use tokio::net::TcpListener;
use tokio::prelude::*;
use std::net::SocketAddr; // Added `SocketAddr` in scope

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr: SocketAddr  = "127.0.0.1:8080".parse()?; // explicitly specified `SocketAddr`
    let mut listener = TcpListener::bind(&addr).await.unwrap(); // unwrap after await

    loop {
        let (mut socket, _) = listener.accept().await?;

        tokio::spawn(async move {
            let mut buf = [0u8; 1024]; // explicitly specified type `u8`

            // In a loop, read data from the socket and write the data back.
            loop {
                let n = match socket.read(&mut buf).await {
                    // socket closed
                    Ok(n) if n == 0 => return,
                    Ok(n) => n,
                    Err(e) => {
                        println!("failed to read from socket; err = {:?}", e);
                        return;
                    }
                };

                // Write the data back
                if let Err(e) = socket.write_all(&buf[0..n]).await {
                    println!("failed to write to socket; err = {:?}", e);
                    return;
                }
            }
        });
    }
}

@dotSlashLu
Copy link
Author

Thanks @abdul-rehman0 , the unwrap error makes sense to me now. Seems the example needs to be corrected then.

@ghost
Copy link

ghost commented Nov 12, 2019

@dotSlashLu if you check out the main page of this repository, you will find the correct working example. The documentation on the crate seems to be outdated.

@hawkw hawkw added the T-docs Topic: documentation label Nov 15, 2019
@carllerche
Copy link
Member

The docs found on docs.rs will be fixed as soon as the crate is published 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-docs Topic: documentation
Projects
None yet
Development

No branches or pull requests

3 participants