Skip to content

Commit

Permalink
Merge pull request #25 from e00E/tokio-upgrade
Browse files Browse the repository at this point in the history
Upgrade to tokio 1.0
  • Loading branch information
flub committed Feb 24, 2021
2 parents 3779a7d + 674503b commit b5b5562
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
toolchain: stable
override: true
components: rustfmt

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ categories = ["asynchronous", "cryptography", "network-programming"]
native-tls = "0.2.3"
thiserror = "1.0.9"
futures-util = { version = "0.3.1", features = ["io"], optional = true }
tokio = { version = "0.2.9", default-features = false, features = ["io-util"], optional = true }
tokio = { version = "1.0", default-features = false, features = ["io-util"], optional = true }
url = "2.1.1"

[features]
Expand All @@ -33,7 +33,7 @@ runtime-tokio = ["tokio"]
[dev-dependencies]
env_logger = "0.7.1"
async-std = { version = "1.6.0", features = ["attributes"] }
tokio = { version = "0.2.9", features = ["full"] }
tokio = { version = "1.0", features = ["full"] }
cfg-if = "0.1.10"
futures = "0.3.1"

Expand Down
15 changes: 15 additions & 0 deletions src/std_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ where
}
}

#[cfg(feature = "runtime-async-std")]
impl<S> Read for StdAdapter<S>
where
S: AsyncRead + Unpin,
Expand All @@ -43,6 +44,20 @@ where
}
}

#[cfg(feature = "runtime-tokio")]
impl<S> Read for StdAdapter<S>
where
S: AsyncRead + Unpin,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut buf = tokio::io::ReadBuf::new(buf);
match self.with_context(|ctx, stream| stream.poll_read(ctx, &mut buf)) {
Poll::Ready(r) => r.map(|_| buf.filled().len()),
Poll::Pending => Err(io::Error::from(io::ErrorKind::WouldBlock)),
}
}
}

impl<S> Write for StdAdapter<S>
where
S: AsyncWrite + Unpin,
Expand Down
22 changes: 22 additions & 0 deletions src/tls_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl<S> TlsStream<S> {
}
}

#[cfg(feature = "runtime-async-std")]
impl<S> AsyncRead for TlsStream<S>
where
S: AsyncRead + AsyncWrite + Unpin,
Expand All @@ -87,6 +88,27 @@ where
}
}

#[cfg(feature = "runtime-tokio")]
impl<S> AsyncRead for TlsStream<S>
where
S: AsyncRead + AsyncWrite + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.with_context(ctx, |s| cvt(s.read(buf.initialize_unfilled()))) {
Poll::Ready(Ok(len)) => {
buf.advance(len);
Poll::Ready(Ok(()))
}
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
Poll::Pending => Poll::Pending,
}
}
}

impl<S> AsyncWrite for TlsStream<S>
where
S: AsyncRead + AsyncWrite + Unpin,
Expand Down

0 comments on commit b5b5562

Please sign in to comment.