Skip to content

Commit 354d4fd

Browse files
alcejen20
authored andcommitted
fix(examples): Remove use of VecDeque as a placeholder type (#143)
1 parent f9502df commit 354d4fd

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

tonic-examples/src/authentication/server.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ pub mod pb {
22
tonic::include_proto!("grpc.examples.echo");
33
}
44

5+
use futures::Stream;
56
use pb::{EchoRequest, EchoResponse};
6-
use std::collections::VecDeque;
7+
use std::pin::Pin;
78
use tonic::{body::BoxBody, transport::Server, Request, Response, Status, Streaming};
89
use tower::Service;
910

1011
type EchoResult<T> = Result<Response<T>, Status>;
11-
type Stream = VecDeque<Result<EchoResponse, Status>>;
12+
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
1213

1314
#[derive(Default)]
1415
pub struct EchoServer;
@@ -20,7 +21,7 @@ impl pb::server::Echo for EchoServer {
2021
Ok(Response::new(EchoResponse { message }))
2122
}
2223

23-
type ServerStreamingEchoStream = Stream;
24+
type ServerStreamingEchoStream = ResponseStream;
2425

2526
async fn server_streaming_echo(
2627
&self,
@@ -36,7 +37,7 @@ impl pb::server::Echo for EchoServer {
3637
Err(Status::unimplemented("not implemented"))
3738
}
3839

39-
type BidirectionalStreamingEchoStream = Stream;
40+
type BidirectionalStreamingEchoStream = ResponseStream;
4041

4142
async fn bidirectional_streaming_echo(
4243
&self,

tonic-examples/src/load_balance/server.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ pub mod pb {
22
tonic::include_proto!("grpc.examples.echo");
33
}
44

5-
use pb::{EchoRequest, EchoResponse};
6-
use std::{collections::VecDeque, net::SocketAddr};
5+
use futures::Stream;
6+
use std::net::SocketAddr;
7+
use std::pin::Pin;
78
use tokio::sync::mpsc;
89
use tonic::{transport::Server, Request, Response, Status, Streaming};
910

11+
use pb::{EchoRequest, EchoResponse};
12+
1013
type EchoResult<T> = Result<Response<T>, Status>;
11-
type Stream = VecDeque<Result<EchoResponse, Status>>;
14+
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
1215

1316
#[derive(Debug)]
1417
pub struct EchoServer {
@@ -23,7 +26,7 @@ impl pb::server::Echo for EchoServer {
2326
Ok(Response::new(EchoResponse { message }))
2427
}
2528

26-
type ServerStreamingEchoStream = Stream;
29+
type ServerStreamingEchoStream = ResponseStream;
2730

2831
async fn server_streaming_echo(
2932
&self,
@@ -39,7 +42,7 @@ impl pb::server::Echo for EchoServer {
3942
Err(Status::unimplemented("not implemented"))
4043
}
4144

42-
type BidirectionalStreamingEchoStream = Stream;
45+
type BidirectionalStreamingEchoStream = ResponseStream;
4346

4447
async fn bidirectional_streaming_echo(
4548
&self,

tonic-examples/src/multiplex/server.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::collections::VecDeque;
1+
use futures::Stream;
2+
use std::pin::Pin;
23
use tonic::{transport::Server, Request, Response, Status};
34

45
pub mod hello_world {
@@ -19,6 +20,8 @@ use echo::{
1920
EchoRequest, EchoResponse,
2021
};
2122

23+
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
24+
2225
#[tokio::main]
2326
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2427
let addr = "[::1]:50051".parse().unwrap();
@@ -64,6 +67,6 @@ impl Echo for MyEcho {
6467
Ok(Response::new(EchoResponse { message }))
6568
}
6669

67-
type ServerStreamingEchoStream = VecDeque<Result<EchoResponse, Status>>;
68-
type BidirectionalStreamingEchoStream = VecDeque<Result<EchoResponse, Status>>;
70+
type ServerStreamingEchoStream = ResponseStream;
71+
type BidirectionalStreamingEchoStream = ResponseStream;
6972
}

tonic-examples/src/tls/server.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ pub mod pb {
22
tonic::include_proto!("/grpc.examples.echo");
33
}
44

5+
use futures::Stream;
56
use pb::{EchoRequest, EchoResponse};
6-
use std::collections::VecDeque;
7+
use std::pin::Pin;
78
use tonic::{
89
transport::{Identity, Server, ServerTlsConfig},
910
Request, Response, Status, Streaming,
1011
};
1112

1213
type EchoResult<T> = Result<Response<T>, Status>;
13-
type Stream = VecDeque<Result<EchoResponse, Status>>;
14+
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
1415

1516
#[derive(Default)]
1617
pub struct EchoServer;
@@ -22,7 +23,7 @@ impl pb::server::Echo for EchoServer {
2223
Ok(Response::new(EchoResponse { message }))
2324
}
2425

25-
type ServerStreamingEchoStream = Stream;
26+
type ServerStreamingEchoStream = ResponseStream;
2627

2728
async fn server_streaming_echo(
2829
&self,
@@ -38,7 +39,7 @@ impl pb::server::Echo for EchoServer {
3839
Err(Status::unimplemented("not implemented"))
3940
}
4041

41-
type BidirectionalStreamingEchoStream = Stream;
42+
type BidirectionalStreamingEchoStream = ResponseStream;
4243

4344
async fn bidirectional_streaming_echo(
4445
&self,

tonic-examples/src/tls_client_auth/server.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ pub mod pb {
22
tonic::include_proto!("grpc.examples.echo");
33
}
44

5-
use std::collections::VecDeque;
6-
5+
use futures::Stream;
76
use pb::{EchoRequest, EchoResponse};
7+
use std::pin::Pin;
88
use tonic::transport::{Certificate, Identity, Server, ServerTlsConfig};
99
use tonic::{Request, Response, Status};
1010

1111
type EchoResult<T> = Result<Response<T>, Status>;
12-
type Stream = VecDeque<Result<EchoResponse, Status>>;
12+
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
1313

1414
#[derive(Default)]
1515
pub struct EchoServer;
@@ -21,8 +21,8 @@ impl pb::server::Echo for EchoServer {
2121
Ok(Response::new(EchoResponse { message }))
2222
}
2323

24-
type ServerStreamingEchoStream = Stream;
25-
type BidirectionalStreamingEchoStream = Stream;
24+
type ServerStreamingEchoStream = ResponseStream;
25+
type BidirectionalStreamingEchoStream = ResponseStream;
2626
}
2727

2828
#[tokio::main]

0 commit comments

Comments
 (0)