Skip to content

Commit c576215

Browse files
authored
Merge pull request ohkami-rs#31 from ohkami-rs/change/remove-default-type-for-C
change: remove default type for `C: UnderlyingConnection`, always requiring user's explicit specifying
2 parents d6803ad + d039398 commit c576215

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
* Minimal and Efficient : minimal codebase to provide efficient, memory-safe WebSocket handling.
1717

18-
* Multi Environment : `tokio`, `smol`, `glommio` are supported as async runtime ( by feature flags `rt_{name}` ).
18+
* Multi-Environment : `tokio`, `smol`, `glommio` are supported as async runtime ( by feature flags `rt_{name}` ).
1919

2020
## Note
2121

@@ -33,10 +33,11 @@ mews = { version = "0.2", features = ["rt_tokio"] }
3333
tokio = { version = "1", features = ["rt"] }
3434
# ...
3535
```
36-
*( with pseudo Request & Response )*
37-
```rust
36+
**( with pseudo Request & Response )**
37+
```rust,ignore
3838
/* server */
3939
40+
use tokio::net::TcpStream;
4041
use mews::{WebSocketContext, Connection, Message};
4142
4243
async fn handle_websocket(
@@ -48,7 +49,7 @@ async fn handle_websocket(
4849
);
4950
5051
let (sign, ws) = ctx.on_upgrade(
51-
|mut conn: Connection| async move {
52+
|mut conn: Connection<TcpStream>| async move {
5253
while let Ok(Some(Message::Text(text))) = conn.recv().await {
5354
conn.send(text).await
5455
.expect("failed to send message");
@@ -67,9 +68,11 @@ async fn handle_websocket(
6768
ws.manage(tcp);
6869
}
6970
```
70-
```rust
71+
```rust,ignore
7172
/* client */
7273
74+
use tokio::net::TcpStream;
75+
7376
async fn start_websocket(
7477
mut tcp: TcpStream
7578
) {
@@ -80,7 +83,7 @@ async fn start_websocket(
8083
);
8184
8285
let (sign, ws) = ctx.on_upgrade(
83-
|mut conn: Connection| async move {
86+
|mut conn: Connection<TcpStream>| async move {
8487
conn.send("Hello!").await.expect("failed to send message");
8588
while let Ok(Some(Message::Text(text))) = conn.recv().await {
8689
println!("got: `{text}`")

src/connection.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{sync::Arc, io::Error};
55
pub trait UnderlyingConnection: AsyncRead + AsyncWrite + Unpin + 'static {}
66
impl<T: AsyncRead + AsyncWrite + Unpin + 'static> UnderlyingConnection for T {}
77

8-
pub struct Connection<C: UnderlyingConnection = crate::runtime::TcpStream> {
8+
pub struct Connection<C: UnderlyingConnection> {
99
__closed__: Arc<RwLock<bool>>,
1010

1111
conn: Arc<std::cell::UnsafeCell<C>>,
@@ -343,14 +343,6 @@ pub mod split {
343343
<tokio::net::TcpStream>::split(self)
344344
}
345345
}
346-
#[cfg(feature="rt_nio")]
347-
impl<'split> Splitable<'split> for nio::net::TcpStream {
348-
type ReadHalf = nio::net::tcp::ReadHalf <'split>;
349-
type WriteHalf = nio::net::tcp::WriteHalf<'split>;
350-
fn split(&'split mut self) -> (Self::ReadHalf, Self::WriteHalf) {
351-
<nio::net::TcpStream>::split(self)
352-
}
353-
}
354346
#[cfg(feature="rt_glommio")]
355347
impl<'split, T: AsyncRead + AsyncWrite + Unpin + 'split> Splitable<'split> for T {
356348
type ReadHalf = futures_util::io::ReadHalf <&'split mut T>;
@@ -371,7 +363,7 @@ pub mod split {
371363
}
372364
};
373365

374-
pub struct ReadHalf<C: AsyncRead + Unpin = <crate::runtime::TcpStream as Splitable<'static>>::ReadHalf> {
366+
pub struct ReadHalf<C: AsyncRead + Unpin> {
375367
__closed__: Arc<RwLock<bool>>,
376368
conn: C,
377369
config: Config,
@@ -389,7 +381,7 @@ pub mod split {
389381
}
390382
}
391383

392-
pub struct WriteHalf<C: AsyncWrite + Unpin = <crate::runtime::TcpStream as Splitable<'static>>::WriteHalf> {
384+
pub struct WriteHalf<C: AsyncWrite + Unpin> {
393385
__closed__: Arc<RwLock<bool>>,
394386
conn: C,
395387
config: Config,

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ compile_error! {"More than one runtime feature flags can't be activated"}
3636
mod runtime {
3737
#[cfg(feature="rt_tokio")]
3838
pub use {
39-
tokio::net::TcpStream,
4039
tokio::io::AsyncReadExt as AsyncRead,
4140
tokio::io::AsyncWriteExt as AsyncWrite,
4241
tokio::sync::RwLock,
@@ -45,7 +44,6 @@ mod runtime {
4544

4645
#[cfg(feature="rt_smol")]
4746
pub use {
48-
smol::net::TcpStream,
4947
smol::io::AsyncReadExt as AsyncRead,
5048
smol::io::AsyncWriteExt as AsyncWrite,
5149
smol::lock::RwLock,
@@ -57,7 +55,6 @@ mod runtime {
5755

5856
#[cfg(feature="rt_glommio")]
5957
pub use {
60-
glommio::net::TcpStream,
6158
futures_util::AsyncReadExt as AsyncRead,
6259
futures_util::AsyncWriteExt as AsyncWrite,
6360
glommio::sync::RwLock,

src/websocket.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::runtime;
21
use crate::message::{CloseFrame, CloseCode};
32
use crate::connection::{UnderlyingConnection, Connection};
43

@@ -47,7 +46,7 @@ pub type Handler<C> = Box<dyn
4746
/// );
4847
///
4948
/// let (sign, ws) = ctx.on_upgrade(
50-
/// |mut conn: Connection| async move {
49+
/// |mut conn: Connection<TcpStream>| async move {
5150
/// while let Ok(Some(Message::Text(text))) = conn.recv().await {
5251
/// conn.send(text).await
5352
/// .expect("failed to send message");
@@ -130,7 +129,7 @@ const _: () = {
130129
/// tcp: TcpStream
131130
/// ) -> Response {
132131
/// let (sign, ws) = ctx.on_upgrade(
133-
/// |mut conn: Connection| async move {
132+
/// |mut conn: Connection<TcpStream>| async move {
134133
/// while let Ok(Some(Message::Text(text))) = conn.recv().await {
135134
/// conn.send(text).await
136135
/// .expect("failed to send message");
@@ -145,7 +144,7 @@ const _: () = {
145144
/// }
146145
/// ```
147146
#[must_use = "`WebSocket` does nothing unless `.manage()` or `.manage_with_timeout()` is called"]
148-
pub struct WebSocket<C: UnderlyingConnection = runtime::TcpStream> {
147+
pub struct WebSocket<C: UnderlyingConnection> {
149148
config: Config,
150149
handler: Handler<C>,
151150
}
@@ -185,7 +184,7 @@ impl<C: UnderlyingConnection> WebSocket<C> {
185184
const _: () = {
186185
impl<C: UnderlyingConnection> PartialEq for WebSocket<C>
187186
where
188-
dyn FnOnce(Connection<C>) -> std::pin::Pin<Box<(dyn std::future::Future<Output = ()> + Send + 'static)>> + Send + Sync
187+
dyn FnOnce(Connection<C>) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'static>> + Send + Sync
189188
: PartialEq
190189
{
191190
fn eq(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)