mirror of https://github.com/rwf2/Rocket.git
Add 'into_bytes' and 'into_inner' methods to Body.
This commit is contained in:
parent
62a75cdde6
commit
efbfbd1045
|
@ -36,6 +36,13 @@ impl<T> Body<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Consumes `self` and returns the inner body.
|
||||||
|
pub fn into_inner(self) -> T {
|
||||||
|
match self {
|
||||||
|
Body::Sized(b, _) | Body::Chunked(b, _) => b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `true` if `self` is a `Body::Sized`.
|
/// Returns `true` if `self` is a `Body::Sized`.
|
||||||
pub fn is_sized(&self) -> bool {
|
pub fn is_sized(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -54,20 +61,33 @@ impl<T> Body<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: io::Read> Body<T> {
|
impl<T: io::Read> Body<T> {
|
||||||
/// Attepts to read `self` into a `String` and returns it. If reading or
|
/// Attepts to read `self` into a `Vec` and returns it. If reading fails,
|
||||||
/// conversion fails, returns `None`.
|
/// returns `None`.
|
||||||
pub fn into_string(self) -> Option<String> {
|
pub fn into_bytes(self) -> Option<Vec<u8>> {
|
||||||
let (mut body, mut string) = match self {
|
let (mut body, mut vec) = match self {
|
||||||
Body::Sized(b, size) => (b, String::with_capacity(size as usize)),
|
Body::Sized(b, size) => (b, Vec::with_capacity(size as usize)),
|
||||||
Body::Chunked(b, _) => (b, String::new())
|
Body::Chunked(b, _) => (b, Vec::new())
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = body.read_to_string(&mut string) {
|
if let Err(e) = body.read_to_end(&mut vec) {
|
||||||
error_!("Error reading body: {:?}", e);
|
error_!("Error reading body: {:?}", e);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(string)
|
Some(vec)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attepts to read `self` into a `String` and returns it. If reading or
|
||||||
|
/// conversion fails, returns `None`.
|
||||||
|
pub fn into_string(self) -> Option<String> {
|
||||||
|
self.into_bytes()
|
||||||
|
.and_then(|bytes| match String::from_utf8(bytes) {
|
||||||
|
Ok(string) => Some(string),
|
||||||
|
Err(e) => {
|
||||||
|
error_!("Body is invalid UTF-8: {}", e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue