Use From instead of custom ReclaimStream.

This commit is contained in:
Drake Tetreault
2020-01-16 17:39:39 -08:00
committed by Martin Algesten
parent af6491cd59
commit 069775d3e0
3 changed files with 17 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::io::{Read, Result as IoResult};
use crate::stream::{ReclaimStream, Stream};
use crate::stream::Stream;
use crate::unit::Unit;
use url::Url;
@@ -74,14 +74,14 @@ impl PoolKey {
/// read is exhausted (reached a 0).
///
/// *Internal API*
pub(crate) struct PoolReturnRead<R: Read + Sized + ReclaimStream> {
pub(crate) struct PoolReturnRead<R: Read + Sized + Into<Stream>> {
// unit that contains the agent where we want to return the reader.
unit: Option<Unit>,
// wrapped reader around the same stream
reader: Option<R>,
}
impl<R: Read + Sized + ReclaimStream> PoolReturnRead<R> {
impl<R: Read + Sized + Into<Stream>> PoolReturnRead<R> {
pub fn new(unit: Option<Unit>, reader: R) -> Self {
PoolReturnRead {
unit,
@@ -94,7 +94,7 @@ impl<R: Read + Sized + ReclaimStream> PoolReturnRead<R> {
if let (Some(unit), Some(reader)) = (self.unit.take(), self.reader.take()) {
let state = &mut unit.agent.lock().unwrap();
// bring back stream here to either go into pool or dealloc
let stream = reader.reclaim_stream();
let stream = reader.into();
if let Some(agent) = state.as_mut() {
if !stream.is_poolable() {
// just let it deallocate
@@ -115,7 +115,7 @@ impl<R: Read + Sized + ReclaimStream> PoolReturnRead<R> {
}
}
impl<R: Read + Sized + ReclaimStream> Read for PoolReturnRead<R> {
impl<R: Read + Sized + Into<Stream>> Read for PoolReturnRead<R> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
let amount = self.do_read(buf)?;
// only if the underlying reader is exhausted can we send a new
@@ -127,7 +127,7 @@ impl<R: Read + Sized + ReclaimStream> Read for PoolReturnRead<R> {
}
}
impl<R: Read + Sized + ReclaimStream> Drop for PoolReturnRead<R> {
impl<R: Read + Sized + Into<Stream>> Drop for PoolReturnRead<R> {
fn drop(&mut self) {
self.return_connection();
}

View File

@@ -6,7 +6,7 @@ use chunked_transfer::Decoder as ChunkDecoder;
use crate::error::Error;
use crate::header::Header;
use crate::pool::PoolReturnRead;
use crate::stream::{ReclaimStream, Stream};
use crate::stream::Stream;
use crate::unit::Unit;
#[cfg(feature = "json")]
@@ -617,9 +617,12 @@ impl<R: Read> Read for LimitedRead<R> {
}
}
impl<R: ReclaimStream> ReclaimStream for LimitedRead<R> {
fn reclaim_stream(self) -> Stream {
self.reader.reclaim_stream()
impl<R> From<LimitedRead<R>> for Stream
where
Stream: From<R>,
{
fn from(limited_read: LimitedRead<R>) -> Stream {
limited_read.reader.into()
}
}

View File

@@ -120,23 +120,13 @@ impl Read for Stream {
}
}
#[cfg(all(feature = "tls", not(feature = "native-tls")))]
pub(crate) trait ReclaimStream {
fn reclaim_stream(self) -> Stream;
}
impl ReclaimStream for Stream {
fn reclaim_stream(self) -> Stream {
self
}
}
impl<R: ReclaimStream> ReclaimStream for ChunkDecoder<R>
impl<R> From<ChunkDecoder<R>> for Stream
where
R: Read,
Stream: From<R>,
{
fn reclaim_stream(self) -> Stream {
self.into_inner().reclaim_stream()
fn from(chunk_decoder: ChunkDecoder<R>) -> Stream {
chunk_decoder.into_inner().into()
}
}