-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::fmt; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use pin_project::pin_project; | ||
|
||
#[cfg(feature = "runtime-async-std")] | ||
use async_std::io::{Read, Write}; | ||
#[cfg(feature = "runtime-tokio")] | ||
use tokio::io::{AsyncRead as Read, AsyncWrite as Write, ReadBuf}; | ||
|
||
#[cfg(feature = "runtime-tokio")] | ||
use async_compression::tokio::bufread::DeflateDecoder; | ||
#[cfg(feature = "runtime-tokio")] | ||
use async_compression::tokio::write::DeflateEncoder; | ||
|
||
/// IMAP stream | ||
#[derive(Debug)] | ||
#[pin_project] | ||
pub struct DeflateStream<T: Read + Write + Unpin + fmt::Debug> { | ||
#[cfg(feature = "runtime-tokio")] | ||
#[pin] | ||
decoder: DeflateDecoder<tokio::io::BufReader<tokio::io::ReadHalf<T>>>, | ||
|
||
#[cfg(feature = "runtime-tokio")] | ||
#[pin] | ||
encoder: DeflateEncoder<tokio::io::WriteHalf<T>>, | ||
} | ||
|
||
#[cfg(feature = "runtime-tokio")] | ||
impl<T: Read + Write + Unpin + fmt::Debug> DeflateStream<T> { | ||
pub(crate) fn new(stream: T) -> Self { | ||
let (read_half, write_half) = tokio::io::split(stream); | ||
let read_half = tokio::io::BufReader::new(read_half); | ||
let decoder = DeflateDecoder::new(read_half); | ||
let encoder = DeflateEncoder::new(write_half); | ||
Self { decoder, encoder } | ||
} | ||
} | ||
|
||
#[cfg(feature = "runtime-tokio")] | ||
impl<T: Read + Write + Unpin + fmt::Debug> Read for DeflateStream<T> { | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut ReadBuf<'_>, | ||
) -> Poll<std::io::Result<()>> { | ||
self.project().decoder.poll_read(cx, buf) | ||
} | ||
} | ||
|
||
#[cfg(feature = "runtime-tokio")] | ||
impl<T: Read + Write + Unpin + fmt::Debug> Write for DeflateStream<T> { | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<std::io::Result<usize>> { | ||
self.project().encoder.poll_write(cx, buf) | ||
} | ||
|
||
fn poll_flush( | ||
self: Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> Poll<std::io::Result<()>> { | ||
self.project().encoder.poll_flush(cx) | ||
} | ||
|
||
fn poll_shutdown( | ||
self: Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> Poll<std::io::Result<()>> { | ||
self.project().encoder.poll_shutdown(cx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters