Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Reimplement stream using futures and without split #290

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 29 additions & 76 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exclude = [".github"]
[features]
default = ["stream"]
expose_stream = []
stream = ["dep:bytes", "dep:tokio", "dep:tokio-rustls"]
stream = ["dep:futures-io"]

# <Forward to imap-codec>
arbitrary = ["imap-codec/arbitrary"]
Expand All @@ -30,11 +30,9 @@ ext_metadata = ["imap-codec/ext_metadata"]
# </Forward to imap-codec>

[dependencies]
bytes = { version = "1.7.1", optional = true }
futures-io = { version = "0.3.31", optional = true }
imap-codec = { version = "2.0.0-alpha.5", features = ["quirk_crlf_relaxed"] }
thiserror = "1.0.63"
tokio = { version = "1.40.0", optional = true, features = ["io-util", "macros", "net"] }
tokio-rustls = { version = "0.26.0", optional = true, default-features = false }
tracing = "0.1.40"

[dev-dependencies]
Expand All @@ -43,6 +41,7 @@ imap-next = { path = ".", features = ["tag_generator"] }

rand = "0.8.5"
tokio = { version = "1.40.0", features = ["full"] }
tokio-util = { version = "0.7.12", features = ["compat"] }

[workspace]
resolver = "2"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ use imap_next::{
stream::Stream,
};
use tokio::net::TcpStream;
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut stream = Stream::insecure(TcpStream::connect("127.0.0.1:1143").await?);
let mut stream = Stream::new(TcpStream::connect("127.0.0.1:1143").await?.compat());
let mut client = Client::new(Options::default());

loop {
Expand Down
3 changes: 2 additions & 1 deletion examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use imap_next::{
stream::Stream,
};
use tokio::net::TcpStream;
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let stream = TcpStream::connect("127.0.0.1:12345").await.unwrap();
let mut stream = Stream::insecure(stream);
let mut stream = Stream::new(stream.compat());
let mut client = Client::new(Options::default());

let greeting = loop {
Expand Down
3 changes: 2 additions & 1 deletion examples/client_authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use imap_next::{
stream::Stream,
};
use tokio::net::TcpStream;
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let stream = TcpStream::connect("127.0.0.1:12345").await.unwrap();
let mut stream = Stream::insecure(stream);
let mut stream = Stream::new(stream.compat());
let mut client = Client::new(Options::default());

loop {
Expand Down
3 changes: 2 additions & 1 deletion examples/client_idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use imap_next::{
stream::Stream,
};
use tokio::{net::TcpStream, sync::mpsc::Receiver};
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let stream = TcpStream::connect("127.0.0.1:12345").await.unwrap();
let mut stream = Stream::insecure(stream);
let mut stream = Stream::new(stream.compat());
let mut client = Client::new(Options::default());

loop {
Expand Down
3 changes: 2 additions & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use imap_next::{
stream::Stream,
};
use tokio::net::TcpListener;
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:12345").await.unwrap();
let (stream, _) = listener.accept().await.unwrap();
let mut stream = Stream::insecure(stream);
let mut stream = Stream::new(stream.compat());
let mut server = Server::new(
Options::default(),
Greeting::ok(None, "server (example)").unwrap(),
Expand Down
3 changes: 2 additions & 1 deletion examples/server_authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use imap_next::{
types::CommandAuthenticate,
};
use tokio::net::TcpListener;
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:12345").await.unwrap();
let (stream, _) = listener.accept().await.unwrap();
let mut stream = Stream::insecure(stream);
let mut stream = Stream::new(stream.compat());
let mut server = Server::new(
Options::default(),
Greeting::ok(None, "server_idle (example)").unwrap(),
Expand Down
3 changes: 2 additions & 1 deletion examples/server_idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use imap_next::{
stream::Stream,
};
use tokio::{net::TcpListener, sync::mpsc::Receiver};
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main(flavor = "current_thread")]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:12345").await.unwrap();
let (stream, _) = listener.accept().await.unwrap();
let mut stream = Stream::insecure(stream);
let mut stream = Stream::new(stream.compat());
let mut server = Server::new(
Options::default(),
Greeting::ok(None, "server_idle (example)").unwrap(),
Expand Down
5 changes: 3 additions & 2 deletions integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ publish = false
[dependencies]
bstr = { version = "1.10.0", default-features = false }
bytes = "1.7.1"
imap-codec = { version = "2.0.0-alpha.4" }
imap-codec = { version = "2.0.0-alpha.5" }
imap-next = { path = ".." }
tokio = { version = "1.40.0", features = ["macros", "net", "rt", "time"] }
tokio = { version = "1.40.0", features = ["io-util", "macros", "net", "rt", "time"] }
tokio-util = { version = "0.7.12", features = ["compat"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

Expand Down
10 changes: 7 additions & 3 deletions integration-test/src/client_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use imap_next::{
stream::{self, Stream},
};
use tokio::net::TcpStream;
use tokio_util::compat::{Compat, TokioAsyncReadCompatExt};
use tracing::trace;

use crate::codecs::Codecs;
Expand All @@ -25,7 +26,7 @@ impl ClientTester {
) -> Self {
let stream = TcpStream::connect(server_address).await.unwrap();
trace!(?server_address, "Client is connected");
let stream = Stream::insecure(stream);
let stream = Stream::new(stream.compat());
let client = Client::new(client_options);
Self {
codecs,
Expand Down Expand Up @@ -344,13 +345,16 @@ impl ClientTester {
#[allow(clippy::large_enum_variant)]
enum ConnectionState {
/// Connection to server established.
Connected { stream: Stream, client: Client },
Connected {
stream: Stream<Compat<TcpStream>>,
client: Client,
},
/// Connection dropped.
Disconnected,
}

impl ConnectionState {
fn connected(&mut self) -> (&mut Stream, &mut Client) {
fn connected(&mut self) -> (&mut Stream<Compat<TcpStream>>, &mut Client) {
match self {
ConnectionState::Connected { stream, client } => (stream, client),
ConnectionState::Disconnected => panic!("Client is already disconnected"),
Expand Down
16 changes: 11 additions & 5 deletions integration-test/src/server_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use imap_next::{
server::{self, ResponseHandle, Server},
stream::{self, Stream},
};
use tokio::net::TcpListener;
use tokio::net::{TcpListener, TcpStream};
use tokio_util::compat::{Compat, TokioAsyncReadCompatExt};
use tracing::trace;

use crate::codecs::Codecs;
Expand All @@ -24,7 +25,7 @@ impl ServerTester {
) -> Self {
let (stream, client_address) = server_listener.accept().await.unwrap();
trace!(?client_address, "Server accepts connection");
let stream = Stream::insecure(stream);
let stream = Stream::new(stream.compat());
Self {
codecs,
server_options,
Expand Down Expand Up @@ -308,15 +309,20 @@ impl ServerTester {
#[allow(clippy::large_enum_variant)]
enum ConnectionState {
// Connection to client established.
Connected { stream: Stream },
Connected {
stream: Stream<Compat<TcpStream>>,
},
// Server greeted client.
Greeted { stream: Stream, server: Server },
Greeted {
stream: Stream<Compat<TcpStream>>,
server: Server,
},
// Connection dropped.
Disconnected,
}

impl ConnectionState {
fn greeted(&mut self) -> (&mut Stream, &mut Server) {
fn greeted(&mut self) -> (&mut Stream<Compat<TcpStream>>, &mut Server) {
match self {
ConnectionState::Connected { .. } => panic!("Server has not greeted yet"),
ConnectionState::Greeted { stream, server } => (stream, server),
Expand Down
Loading