1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 08:52:42 +01:00
actix-web/src/client/connection.rs

80 lines
1.8 KiB
Rust
Raw Normal View History

2018-11-12 08:12:54 +01:00
use std::{fmt, io, time};
use futures::Poll;
use tokio_io::{AsyncRead, AsyncWrite};
use super::pool::Acquired;
/// HTTP client connection
2018-11-14 07:53:30 +01:00
pub struct Connection<T> {
2018-11-12 08:12:54 +01:00
io: T,
created: time::Instant,
pool: Option<Acquired<T>>,
}
impl<T> fmt::Debug for Connection<T>
where
2018-11-14 07:53:30 +01:00
T: fmt::Debug,
2018-11-12 08:12:54 +01:00
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Connection {:?}", self.io)
}
}
impl<T: AsyncRead + AsyncWrite + 'static> Connection<T> {
pub(crate) fn new(io: T, created: time::Instant, pool: Acquired<T>) -> Self {
Connection {
io,
created,
pool: Some(pool),
}
}
/// Raw IO stream
pub fn get_mut(&mut self) -> &mut T {
&mut self.io
}
/// Close connection
pub fn close(mut self) {
if let Some(mut pool) = self.pool.take() {
pool.close(self)
}
}
/// Release this connection to the connection pool
pub fn release(mut self) {
if let Some(mut pool) = self.pool.take() {
pool.release(self)
}
}
pub(crate) fn into_inner(self) -> (T, time::Instant) {
(self.io, self.created)
}
}
impl<T: AsyncRead + AsyncWrite + 'static> io::Read for Connection<T> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.io.read(buf)
}
}
impl<T: AsyncRead + AsyncWrite + 'static> AsyncRead for Connection<T> {}
impl<T: AsyncRead + AsyncWrite + 'static> io::Write for Connection<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.io.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.io.flush()
}
}
impl<T: AsyncRead + AsyncWrite + 'static> AsyncWrite for Connection<T> {
fn shutdown(&mut self) -> Poll<(), io::Error> {
self.io.shutdown()
}
}