@@ -67,14 +67,14 @@ You should see some logging output flying past really quickly on both terminal w
67
67
shell where you ran the client binary, you should see the output of the bidirectional streaming rpc,
68
68
printing 1 line per second:
69
69
70
- NOTE = RouteNote { location: Some(Point { latitude: 409146139, longitude: -746188906 }), message: "at 1.000319208s" }
70
+ ```
71
+ NOTE = RouteNote { location: Some(Point { latitude: 409146139, longitude: -746188906 }), message: "at 1.000319208s" }
72
+ ```
71
73
72
74
If you scroll up you should see the output of the other 3 request types: simple rpc, server-side
73
75
streaming and client-side streaming.
74
76
75
77
76
- [ readme ] : https://github.com/hyperium/tonic#getting-started
77
-
78
78
## Project setup
79
79
80
80
We will develop our example from scratch in a new crate:
@@ -259,7 +259,7 @@ pub mod routeguide {
259
259
tonic :: include_proto! (" routeguide" );
260
260
}
261
261
262
- use routeguide :: route_guide_server :: RouteGuide ;
262
+ use routeguide :: route_guide_server :: { RouteGuide , RouteGuideServer } ;
263
263
use routeguide :: {Feature , Point , Rectangle , RouteNote , RouteSummary };
264
264
```
265
265
@@ -269,16 +269,16 @@ the package declared in in our `.proto` file, not a filename, e.g "routeguide.rs
269
269
With this in place, we can stub out our service implementation:
270
270
271
271
``` rust
272
- use std :: pin :: Pin ;
273
-
274
272
use futures_core :: Stream ;
273
+ use std :: pin :: Pin ;
274
+ use std :: sync :: Arc ;
275
275
use tokio :: sync :: mpsc;
276
276
use tonic :: {Request , Response , Status };
277
277
```
278
278
279
279
``` rust
280
280
#[tonic:: async_trait]
281
- impl server :: RouteGuide for RouteGuide {
281
+ impl RouteGuide for RouteGuideService {
282
282
async fn get_feature (& self , _request : Request <Point >) -> Result <Response <Feature >, Status > {
283
283
unimplemented! ()
284
284
}
@@ -370,7 +370,7 @@ when performing feature lookups. You can find them in
370
370
371
371
[ route-guide-db ] : https://github.com/hyperium/tonic/blob/master/examples/data/route_guide_db.json
372
372
[ data-module ] : https://github.com/hyperium/tonic/blob/master/examples/src/routeguide/data.rs
373
- [ in-range-fn ] : https://github.com/hyperium/tonic/blob/master/examples/src/routeguide/server.rs#L177
373
+ [ in-range-fn ] : https://github.com/hyperium/tonic/blob/master/examples/src/routeguide/server.rs#L174
374
374
375
375
#### Request and Response types
376
376
All our service methods receive a ` tonic::Request<T> ` and return a
@@ -449,7 +449,7 @@ async fn record_route(
449
449
request : Request <tonic :: Streaming <Point >>,
450
450
) -> Result <Response <RouteSummary >, Status > {
451
451
let stream = request . into_inner ();
452
- futures :: pin_mut! (stream );
452
+ futures_util :: pin_mut! (stream );
453
453
454
454
let mut summary = RouteSummary :: default ();
455
455
let mut last_point = None ;
@@ -488,7 +488,13 @@ Finally, let's look at our bidirectional streaming RPC `route_chat`, which recei
488
488
of ` RouteNote ` s and returns a stream of ` RouteNote ` s.
489
489
490
490
``` rust
491
- type RouteChatStream = Pin <Box <dyn Stream <Item = Result <RouteNote , Status >> + Send + 'static >>;
491
+ use std :: collections :: HashMap ;
492
+ ```
493
+
494
+ ``` rust
495
+ type RouteChatStream =
496
+ Pin <Box <dyn Stream <Item = Result <RouteNote , Status >> + Send + Sync + 'static >>;
497
+
492
498
493
499
async fn route_chat (
494
500
& self ,
@@ -498,7 +504,7 @@ async fn route_chat(
498
504
let stream = request . into_inner ();
499
505
500
506
let output = async_stream :: try_stream! {
501
- futures :: pin_mut! (stream );
507
+ futures_util :: pin_mut! (stream );
502
508
503
509
while let Some (note ) = stream . next (). await {
504
510
let note = note ? ;
@@ -547,7 +553,7 @@ use tonic::transport::Server;
547
553
async fn main () -> Result <(), Box <dyn std :: error :: Error >> {
548
554
let addr = " [::1]:10000" . parse (). unwrap ();
549
555
550
- let route_guide = RouteGuide {
556
+ let route_guide = RouteGuideService {
551
557
features : Arc :: new (data :: load ()),
552
558
};
553
559
@@ -561,14 +567,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
561
567
562
568
To handle requests, ` Tonic ` uses [ Tower] and [ hyper] internally. What this means,
563
569
among other things, is that we have a flexible and composable stack we can build on top of. We can,
564
- for example, add an [ interceptor] [ authentication-example ] or implement [ routing ] [ router-example ] .
565
- In the future, Tonic may include higher level support for routing and interceptors .
570
+ for example, add an [ interceptor] [ authentication-example ] to process requests before they reach our service
571
+ methods .
566
572
567
573
568
574
[ Tower ] : https://github.com/tower-rs
569
575
[ hyper ] : https://github.com/hyperium/hyper
570
576
[ authentication-example ] : https://github.com/hyperium/tonic/blob/master/examples/src/authentication/server.rs#L56
571
- [ router-example ] : https://github.com/hyperium/tonic/blob/master/interop/src/bin/server.rs#L73
572
577
573
578
<a name =" client " ></a >
574
579
## Creating the client
@@ -596,7 +601,8 @@ $ mv src/main.rs src/server.rs
596
601
$ touch src/client.rs
597
602
```
598
603
599
- To call service methods, we first need to create a gRPC * client* to communicate with the server.
604
+ To call service methods, we first need to create a gRPC * client* to communicate with the server. Like in the server
605
+ case, we'll start by bringing the generated code into scope:
600
606
601
607
``` rust
602
608
pub mod routeguide {
@@ -755,7 +761,7 @@ async fn run_route_chat(client: &mut RouteGuideClient<Channel>) -> Result<(), Bo
755
761
let outbound = async_stream :: stream! {
756
762
let mut interval = time :: interval (Duration :: from_secs (1 ));
757
763
758
- while let Some ( time ) = interval . next (). await {
764
+ while let time = interval . tick (). await {
759
765
let elapsed = time . duration_since (start );
760
766
let note = RouteNote {
761
767
location : Some (Point {
0 commit comments