move Read impl of Body to private function
This commit is contained in:
		
							
								
								
									
										41
									
								
								src/body.rs
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								src/body.rs
									
									
									
									
									
								
							| @@ -1,5 +1,4 @@ | ||||
| use std::io::{Cursor, Read}; | ||||
| use std::io; | ||||
| use std::io::Read; | ||||
| use std::fs::File; | ||||
| use std::fmt; | ||||
|  | ||||
| @@ -28,10 +27,18 @@ impl Body { | ||||
|     */ | ||||
| } | ||||
|  | ||||
| impl Read for Body { | ||||
|     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||||
|         self.reader.read(buf) | ||||
|     } | ||||
| // useful for tests, but not publicly exposed | ||||
| #[cfg(test)] | ||||
| pub fn read_to_string(mut body: Body) -> ::std::io::Result<String> { | ||||
|     let mut s = String::new(); | ||||
|     match body.reader { | ||||
|         Kind::Reader(ref mut reader, _) => { | ||||
|             reader.read_to_string(&mut s) | ||||
|         } | ||||
|         Kind::Bytes(ref mut bytes) => { | ||||
|             (&**bytes).read_to_string(&mut s) | ||||
|         } | ||||
|     }.map(|_| s) | ||||
| } | ||||
|  | ||||
| enum Kind { | ||||
| @@ -39,28 +46,6 @@ enum Kind { | ||||
|     Bytes(Vec<u8>), | ||||
| } | ||||
|  | ||||
| impl Read for Kind { | ||||
|     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||||
|         match *self { | ||||
|             Kind::Reader(ref mut reader, _) => { | ||||
|                 reader.read(buf) | ||||
|             } | ||||
|             Kind::Bytes(ref mut bytes) => { | ||||
|                 // To make sure the bytes are removed properly when you read | ||||
|                 // them, you need to use `drain()` | ||||
|                 // FIXME: this will probably have poor performance for larger | ||||
|                 // bodies due to allocating more than necessary. | ||||
|                 let drained_bytes: Vec<u8> = bytes.drain(..).collect(); | ||||
|  | ||||
|                 // then you need a Cursor because a standard Vec doesn't implement | ||||
|                 // Read | ||||
|                 let mut cursor = Cursor::new(drained_bytes); | ||||
|                 cursor.read(buf) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl From<Vec<u8>> for Body { | ||||
|     #[inline] | ||||
|     fn from(v: Vec<u8>) -> Body { | ||||
|   | ||||
| @@ -291,11 +291,11 @@ impl Read for Response { | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use super::*; | ||||
|     use ::body; | ||||
|     use hyper::method::Method; | ||||
|     use hyper::Url; | ||||
|     use hyper::header::{Host, Headers, ContentType}; | ||||
|     use std::collections::HashMap; | ||||
|     use std::io::Read; | ||||
|     use serde_urlencoded; | ||||
|     use serde_json; | ||||
|  | ||||
| @@ -378,8 +378,7 @@ mod tests { | ||||
|  | ||||
|         r = r.body(body); | ||||
|  | ||||
|         let mut buf = String::new(); | ||||
|         r.body.unwrap().unwrap().read_to_string(&mut buf).unwrap(); | ||||
|         let buf = body::read_to_string(r.body.unwrap().unwrap()).unwrap(); | ||||
|  | ||||
|         assert_eq!(buf, body); | ||||
|     } | ||||
| @@ -398,8 +397,7 @@ mod tests { | ||||
|         // Make sure the content type was set | ||||
|         assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::form_url_encoded())); | ||||
|  | ||||
|         let mut buf = String::new(); | ||||
|         r.body.unwrap().unwrap().read_to_string(&mut buf).unwrap(); | ||||
|         let buf = body::read_to_string(r.body.unwrap().unwrap()).unwrap(); | ||||
|  | ||||
|         let body_should_be = serde_urlencoded::to_string(&form_data).unwrap(); | ||||
|         assert_eq!(buf, body_should_be); | ||||
| @@ -419,8 +417,7 @@ mod tests { | ||||
|         // Make sure the content type was set | ||||
|         assert_eq!(r.headers.get::<ContentType>(), Some(&ContentType::json())); | ||||
|  | ||||
|         let mut buf = String::new(); | ||||
|         r.body.unwrap().unwrap().read_to_string(&mut buf).unwrap(); | ||||
|         let buf = body::read_to_string(r.body.unwrap().unwrap()).unwrap(); | ||||
|  | ||||
|         let body_should_be = serde_json::to_string(&json_data).unwrap(); | ||||
|         assert_eq!(buf, body_should_be); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user