tarpc icon indicating copy to clipboard operation
tarpc copied to clipboard

Can't compile readme.rs

Open Praying opened this issue 6 years ago • 11 comments

here is the code

// Copyright 2018 Google LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

#![feature(async_await)]

use futures::{
    future::{self, Ready},
    prelude::*,
};
use rpc::{
    client, context,
    server::{BaseChannel, Channel},
};
use std::io;

/// This is the service definition. It looks a lot like a trait definition.
/// It defines one RPC, hello, which takes one arg, name, and returns a String.
#[tarpc::service]
pub trait World {
    async fn hello(name: String) -> String;
}

/// This is the type that implements the generated World trait. It is the business logic
/// and is used to start the server.
#[derive(Clone)]
struct HelloServer;

impl World for HelloServer {
    // Each defined rpc generates two items in the trait, a fn that serves the RPC, and
    // an associated type representing the future output by the fn.

    type HelloFut = Ready<String>;

    fn hello(self, _: context::Context, name: String) -> Self::HelloFut {
        future::ready(format!("Hello, {}!", name))
    }
}

#[tokio::main]
async fn main() -> io::Result<()> {
    // bincode_transport is provided by the associated crate bincode-transport. It makes it easy
    // to start up a serde-powered bincode serialization strategy over TCP.
    let mut transport = bincode_transport::listen(&"0.0.0.0:0".parse().unwrap())?;
    let addr = transport.local_addr();

    let server = async move {
        // For this example, we're just going to wait for one connection.
        let client = transport.next().await.unwrap().unwrap();

        // `Channel` is a trait representing a server-side connection. It is a trait to allow
        // for some channels to be instrumented: for example, to track the number of open connections.
        // BaseChannel is the most basic channel, simply wrapping a transport with no added
        // functionality.
        BaseChannel::with_defaults(client)
            // serve_world is generated by the tarpc::service attribute. It takes as input any type
            // implementing the generated World trait.
            .respond_with(HelloServer.serve())
            .execute()
            .await;
    };
    tokio::spawn(server);

    let transport = bincode_transport::connect(&addr).await?;

    // WorldClient is generated by the tarpc::service attribute. It has a constructor `new` that
    // takes a config and any Transport as input.
    let mut client = WorldClient::new(client::Config::default(), transport).spawn()?;

    // The client has an RPC method for each RPC defined in the annotated trait. It takes the same
    // args as defined, with the addition of a Context, which is always the first arg. The Context
    // specifies a deadline and trace information which can be helpful in debugging requests.
    let hello = client.hello(context::current(), "Stim".to_string()).await?;

    eprintln!("{}", hello);

    Ok(())
}

and here is the error:

   Compiling tarpc-demo v0.1.0 (/home/quran/SourceCode/tarpc-demo)
error[E0706]: trait fns cannot be declared `async`
  --> src/main.rs:23:5
   |
23 |     async fn hello(name: String) -> String;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0433]: failed to resolve: use of undeclared type or module `rpc`
  --> src/main.rs:13:5
   |
13 | use rpc::{
   |     ^^^ use of undeclared type or module `rpc`

error[E0432]: unresolved import `rpc`
  --> src/main.rs:13:5
   |
13 | use rpc::{
   |     ^^^ use of undeclared type or module `rpc`

error[E0433]: failed to resolve: use of undeclared type or module `tarpc`
  --> src/main.rs:21:3
   |
21 | #[tarpc::service]
   |   ^^^^^ use of undeclared type or module `tarpc`

error[E0437]: type `HelloFut` is not a member of trait `World`
  --> src/main.rs:35:5
   |
35 |     type HelloFut = Ready<String>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `World`

error[E0433]: failed to resolve: use of undeclared type or module `bincode_transport`
  --> src/main.rs:46:25
   |
46 |     let mut transport = bincode_transport::listen(&"0.0.0.0:0".parse().unwrap())?;
   |                         ^^^^^^^^^^^^^^^^^ use of undeclared type or module `bincode_transport`

error[E0433]: failed to resolve: use of undeclared type or module `BaseChannel`
  --> src/main.rs:57:9
   |
57 |         BaseChannel::with_defaults(client)
   |         ^^^^^^^^^^^ use of undeclared type or module `BaseChannel`

error[E0433]: failed to resolve: use of undeclared type or module `bincode_transport`
  --> src/main.rs:66:21
   |
66 |     let transport = bincode_transport::connect(&addr).await?;
   |                     ^^^^^^^^^^^^^^^^^ use of undeclared type or module `bincode_transport`

error[E0433]: failed to resolve: use of undeclared type or module `WorldClient`
  --> src/main.rs:70:22
   |
70 |     let mut client = WorldClient::new(client::Config::default(), transport).spawn()?;
   |                      ^^^^^^^^^^^ use of undeclared type or module `WorldClient`

warning: unused imports: `BaseChannel`, `Channel`
  --> src/main.rs:15:14
   |
15 |     server::{BaseChannel, Channel},
   |              ^^^^^^^^^^^  ^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error[E0599]: no method named `serve` found for type `HelloServer` in the current scope
  --> src/main.rs:60:39
   |
29 | struct HelloServer;
   | ------------------- method `serve` not found for this
...
60 |             .respond_with(HelloServer.serve())
   |                                       ^^^^^

error[E0220]: associated type `HelloFut` not found for `Self`
  --> src/main.rs:37:58
   |
37 |     fn hello(self, _: context::Context, name: String) -> Self::HelloFut {
   |                                                          ^^^^^^^^^^^^^^ associated type `HelloFut` not found

error: aborting due to 11 previous errors

Some errors have detailed explanations: E0220, E0432, E0433, E0437, E0599.
For more information about an error, try `rustc --explain E0220`.
error: Could not compile `tarpc-demo`.

To learn more, run the command again with --verbose.

Praying avatar Aug 09 '19 07:08 Praying

I try it in nightly-gnu and nightly-mingw, the same error,, please fix it

Praying avatar Aug 09 '19 07:08 Praying

Hi, thanks for the report :) I see a few errors that indicate to me that you have not set up your Cargo.toml properly. Can you show me what it looks like?

failed to resolve: use of undeclared type or module rpc

Try changing these imports to tarpc:: instead of rpc::. rpc is an internal crate used by the tarpc example. I've now changed this example to use a less confusing import in https://github.com/google/tarpc/commit/d560ac619782bb42046ea13606c85989e178d52a.

use of undeclared type or module tarpc

This indicates that you have not added tarpc to your Cargo.toml.

use of undeclared type or module bincode_transport

This is an internal name used by the tarpc package; on crates.io it's tarpc-bincode-transport, so in paths it would be tarpc_bincode_transport. I just updated the examples to use the real crate name in https://github.com/google/tarpc/commit/f974533bf76c66dbe914be6470463771c893e95b.

tikue avatar Aug 09 '19 08:08 tikue

@Praying have you had a chance to look at this?

tikue avatar Aug 13 '19 03:08 tikue

@tikue Sorry, I forget it, I just write those in my toml, and the code has been delete....

futures-preview = "0.3.0-alpha.17"
tokio = "0.2.0-alpha.1"

and I just want to write a simple example to learn how to use it, But it seems very difficult to use it. I am looking a rust rpc to implement raft. I hope more example and toturials

Praying avatar Aug 13 '19 11:08 Praying

@Praying thanks for the feedback! Anything specific you would change to make it easier (other than fix the examples, which I believe are fixed now)?

tikue avatar Aug 13 '19 15:08 tikue

@tikue emming, It seems that I still didn't compile the readme.rs successfully. could you give me a complete example that I can use compile?

Praying avatar Aug 14 '19 11:08 Praying

example-service is a fully contained example. It shows you how you set up your Cargo.toml.

tikue avatar Aug 14 '19 15:08 tikue

@Praying did you eventually get things working?

tikue avatar Nov 16 '19 01:11 tikue

I copied the example-service and it doesn't work for me:

error[E0433]: failed to resolve: could not find `tokio_serde` in `tarpc`
 --> src/client.rs:9:30
  |
9 | use tarpc::{client, context, tokio_serde::formats::Json};
  |                              ^^^^^^^^^^^ could not find `tokio_serde` in `tarpc`

The service was removed from the source so I'm guessing it's no longer valid.

edit: The reason is that it's not updated for master, you'd need to use the master branch of tarpc to go along with that example

lewisdiamond avatar Dec 08 '20 03:12 lewisdiamond

Sorry about that! I need to publish a new version.

tikue avatar Dec 08 '20 04:12 tikue

A new version was published today. Please let me know if it fixes this issue.

tikue avatar Dec 29 '20 00:12 tikue

I think this was fixed, but if not, please feel free to reopen, thanks!

tikue avatar Feb 04 '24 08:02 tikue