Skip to content

Commit

Permalink
refactor!: Reimplement stream without split
Browse files Browse the repository at this point in the history
  • Loading branch information
jakoschiko committed Sep 8, 2024
1 parent 75671ca commit 119629b
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 305 deletions.
81 changes: 0 additions & 81 deletions Cargo.lock

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

5 changes: 2 additions & 3 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:bytes", "dep:tokio"]

# <Forward to imap-codec>
arbitrary = ["imap-codec/arbitrary"]
Expand All @@ -33,8 +33,7 @@ ext_metadata = ["imap-codec/ext_metadata"]
bytes = { version = "1.7.1", 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 }
tokio = { version = "1.40.0", optional = true, features = ["io-util", "net"] }
tracing = "0.1.40"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use tokio::net::TcpStream;
#[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?);
let mut client = Client::new(Options::default());
loop {
Expand Down
2 changes: 1 addition & 1 deletion examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tokio::net::TcpStream;
#[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);
let mut client = Client::new(Options::default());

let greeting = loop {
Expand Down
2 changes: 1 addition & 1 deletion examples/client_authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::net::TcpStream;
#[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);
let mut client = Client::new(Options::default());

loop {
Expand Down
2 changes: 1 addition & 1 deletion examples/client_idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::{net::TcpStream, sync::mpsc::Receiver};
#[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);
let mut client = Client::new(Options::default());

loop {
Expand Down
2 changes: 1 addition & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tokio::net::TcpListener;
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);
let mut server = Server::new(
Options::default(),
Greeting::ok(None, "server (example)").unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion examples/server_authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::net::TcpListener;
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);
let mut server = Server::new(
Options::default(),
Greeting::ok(None, "server_idle (example)").unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion examples/server_idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tokio::{net::TcpListener, sync::mpsc::Receiver};
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);
let mut server = Server::new(
Options::default(),
Greeting::ok(None, "server_idle (example)").unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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"] }
tracing = "0.1.40"
Expand Down
9 changes: 6 additions & 3 deletions integration-test/src/client_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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);
let client = Client::new(client_options);
Self {
codecs,
Expand Down Expand Up @@ -344,13 +344,16 @@ impl ClientTester {
#[allow(clippy::large_enum_variant)]
enum ConnectionState {
/// Connection to server established.
Connected { stream: Stream, client: Client },
Connected {
stream: Stream<TcpStream>,
client: Client,
},
/// Connection dropped.
Disconnected,
}

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

use crate::codecs::Codecs;
Expand All @@ -24,7 +24,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);
Self {
codecs,
server_options,
Expand Down Expand Up @@ -308,15 +308,20 @@ impl ServerTester {
#[allow(clippy::large_enum_variant)]
enum ConnectionState {
// Connection to client established.
Connected { stream: Stream },
Connected {
stream: Stream<TcpStream>,
},
// Server greeted client.
Greeted { stream: Stream, server: Server },
Greeted {
stream: Stream<TcpStream>,
server: Server,
},
// Connection dropped.
Disconnected,
}

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

0 comments on commit 119629b

Please sign in to comment.