Skip to content

Clean up Stream wrapper #21

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

Merged
1 commit merged into from
Nov 21, 2019
Merged
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
67 changes: 22 additions & 45 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use std::pin::Pin;
use std::sync::Arc;

use async_h1::client;
use async_std::{io, net, task};
use async_std::io::{self, Read, Write};
use async_std::net::{self, TcpStream};
use async_std::task::{self, Context, Poll};
use http_types::{Method, Request, Url};

fn main() -> Result<(), async_h1::Exception> {
task::block_on(async {
let tcp_stream = net::TcpStream::connect("127.0.0.1:8080").await?;
println!("connecting to {}", tcp_stream.peer_addr()?);
let stream = net::TcpStream::connect("127.0.0.1:8080").await?;
println!("connecting to {}", stream.peer_addr()?);

// TODO: Delete this line when we implement `Clone` for `TcpStream`.
let stream = Stream(Arc::new(stream));

let stream = Stream::new(tcp_stream);
for i in 0usize..2 {
println!("making request {}/2", i + 1);

Expand All @@ -23,59 +30,29 @@ fn main() -> Result<(), async_h1::Exception> {
})
}

use async_std::{
io::{Read, Write},
net::TcpStream,
task::{Context, Poll},
};
use std::{
pin::Pin,
sync::{Arc, Mutex},
};

struct Stream {
internal: Arc<Mutex<TcpStream>>,
}

impl Stream {
fn new(internal: TcpStream) -> Self {
Stream {
internal: Arc::new(Mutex::new(internal)),
}
}
}

impl Clone for Stream {
fn clone(&self) -> Self {
Stream {
internal: self.internal.clone(),
}
}
}
#[derive(Clone)]
struct Stream(Arc<TcpStream>);

impl Read for Stream {
fn poll_read(
mut self: Pin<&mut Self>,
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
<TcpStream as Read>::poll_read(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
Pin::new(&mut &*self.0).poll_read(cx, buf)
}
}

impl Write for Stream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8],
) -> Poll<io::Result<usize>> {
<TcpStream as Write>::poll_write(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
Pin::new(&mut &*self.0).poll_write(cx, buf)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
<TcpStream as Write>::poll_flush(Pin::new(&mut self.internal.lock().unwrap()), cx)
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
Pin::new(&mut &*self.0).poll_flush(cx)
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
<TcpStream as Write>::poll_close(Pin::new(&mut self.internal.lock().unwrap()), cx)
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
Pin::new(&mut &*self.0).poll_close(cx)
}
}
64 changes: 20 additions & 44 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::pin::Pin;
use std::sync::Arc;

use async_h1::server;
use async_std::net;
use async_std::io::{self, Read, Write};
use async_std::net::{self, TcpStream};
use async_std::prelude::*;
use async_std::task;
use async_std::task::{self, Context, Poll};
use http_types::{Response, StatusCode};

fn main() -> Result<(), async_h1::Exception> {
Expand All @@ -15,7 +19,9 @@ fn main() -> Result<(), async_h1::Exception> {
let stream = stream?;
println!("starting new connection from {}", stream.peer_addr()?);

let stream = Stream::new(stream);
// TODO: Delete this line when we implement `Clone` for `TcpStream`.
let stream = Stream(Arc::new(stream));

server::connect(stream.clone(), stream, |_| {
async { Ok(Response::new(StatusCode::Ok)) }
})
Expand All @@ -26,59 +32,29 @@ fn main() -> Result<(), async_h1::Exception> {
})
}

use async_std::{
io::{self, Read, Write},
net::TcpStream,
task::{Context, Poll},
};
use std::{
pin::Pin,
sync::{Arc, Mutex},
};

struct Stream {
internal: Arc<Mutex<TcpStream>>,
}

impl Stream {
fn new(internal: TcpStream) -> Self {
Stream {
internal: Arc::new(Mutex::new(internal)),
}
}
}

impl Clone for Stream {
fn clone(&self) -> Self {
Stream {
internal: self.internal.clone(),
}
}
}
#[derive(Clone)]
struct Stream(Arc<TcpStream>);

impl Read for Stream {
fn poll_read(
mut self: Pin<&mut Self>,
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
<TcpStream as Read>::poll_read(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
Pin::new(&mut &*self.0).poll_read(cx, buf)
}
}

impl Write for Stream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8],
) -> Poll<io::Result<usize>> {
<TcpStream as Write>::poll_write(Pin::new(&mut self.internal.lock().unwrap()), cx, buf)
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
Pin::new(&mut &*self.0).poll_write(cx, buf)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
<TcpStream as Write>::poll_flush(Pin::new(&mut self.internal.lock().unwrap()), cx)
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
Pin::new(&mut &*self.0).poll_flush(cx)
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
<TcpStream as Write>::poll_close(Pin::new(&mut self.internal.lock().unwrap()), cx)
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
Pin::new(&mut &*self.0).poll_close(cx)
}
}