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

Update examples in top-level readme. #588

Merged
merged 3 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ Transport-agnostic `core` and transport servers for `http`, `ipc`, `websockets`

```rust
use jsonrpc_http_server::jsonrpc_core::{IoHandler, Value, Params};
use jsonrpc_http_server::{ServerBuilder};
use jsonrpc_http_server::ServerBuilder;

fn main() {
let mut io = IoHandler::new();
io.add_method("say_hello", |_params: Params| {
Ok(Value::String("hello".to_string()))
let mut io = IoHandler::default();
io.add_method("say_hello", |_params: Params| async {
Ok(Value::String("hello".to_owned()))
});

let server = ServerBuilder::new(io)
Expand Down Expand Up @@ -97,7 +97,6 @@ fn main() {

```rust
use jsonrpc_core_client::transports::local;
use jsonrpc_core::futures::future::{self, Future, FutureResult};
use jsonrpc_core::{Error, IoHandler, Result};
use jsonrpc_derive::rpc;

Expand Down Expand Up @@ -143,5 +142,4 @@ fn main() {
};
fut.wait().unwrap();
}

```
59 changes: 0 additions & 59 deletions core/README.md

This file was deleted.

62 changes: 62 additions & 0 deletions derive/examples/client-local.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use jsonrpc_core::{
futures::{self, FutureExt},
BoxFuture, IoHandler, Result,
};
use jsonrpc_core_client::transports::local;
use jsonrpc_derive::rpc;

/// Rpc trait
#[rpc]
pub trait Rpc {
/// Returns a protocol version
#[rpc(name = "protocolVersion")]
fn protocol_version(&self) -> Result<String>;

/// Adds two numbers and returns a result
#[rpc(name = "add", alias("callAsyncMetaAlias"))]
fn add(&self, a: u64, b: u64) -> Result<u64>;

/// Performs asynchronous operation
#[rpc(name = "callAsync")]
fn call(&self, a: u64) -> BoxFuture<Result<String>>;
}

struct RpcImpl;

impl Rpc for RpcImpl {
fn protocol_version(&self) -> Result<String> {
Ok("version1".into())
}

fn add(&self, a: u64, b: u64) -> Result<u64> {
Ok(a + b)
}

fn call(&self, _: u64) -> BoxFuture<Result<String>> {
Box::pin(futures::future::ready(Ok("OK".to_owned())))
}
}

fn main() {
futures::executor::block_on(async {
let mut io = IoHandler::new();
io.extend_with(RpcImpl.to_delegate());
println!("Starting local server");
let (client, server) = local::connect(io);
let client = use_client(client).fuse();
let server = server.fuse();

futures::pin_mut!(client);
futures::pin_mut!(server);

futures::select! {
server = server => {},
client = client => {},
}
});
}

async fn use_client(client: RpcClient) {
let res = client.add(5, 6).await.unwrap();
println!("5 + 6 = {}", res);
}
17 changes: 16 additions & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,22 @@
//! }
//! }
//!
//! # fn main() {}
//! fn main() {
//! let mut io = jsonrpc_core::MetaIoHandler::default();
//! io.extend_with(RpcImpl::default().to_delegate());
//!
//! let server_builder = jsonrpc_tcp_server::ServerBuilder::with_meta_extractor(
//! io,
//! |request: &jsonrpc_tcp_server::RequestContext| Arc::new(Session::new(request.sender.clone()))
//! );
//! let server = server_builder
//! .start(&"127.0.0.1:3030".parse().unwrap())
//! .expect("Unable to start TCP server");
//!
//! // The server spawns a separate thread. Dropping the `server` handle causes it to close.
//! // Uncomment the line below to keep the server running in your example.
//! // server.wait();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"uncommented" because the example wouldn't terminate otherwise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, is there some other way to prevent that? I guess we could mark it as "no-run"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could either mark it no_run or hide the "uncommented" line from the docs by # // server.wait();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. no_run feels inferior, since it won't detect the runtime errors
  2. I think hidden lines are still being executed (they are just not displayed in the example)

I can remove the comment completely, but I think it's easier if we make users aware they have to wait for the server. Can add an explanation comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can add an explanation comment.

Yes please. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

//! }
//! ```
//!
//! Client Example
Expand Down