Added a from_read() method to RWops

The ownership is a little squirrelly but this seems the best way.
This commit is contained in:
Simon Heath
2016-09-20 15:12:31 -04:00
parent f0a32f4415
commit 3c62e22269
+15
View File
@@ -59,6 +59,21 @@ impl<'a> RWops<'a> {
}
}
/// Reads a `Read` object into a buffer and then passes it to `RWops.from_bytes`.
///
/// The buffer must be provided to this function and must live as long as the
/// `RWops`, but the `RWops` does not take ownership of it.
pub fn from_read<T>(r: &mut T, buffer: &'a mut Vec<u8>) -> Result<RWops<'a>, String>
where T: io::Read + Sized {
match r.read_to_end(buffer) {
Ok(_size) => RWops::from_bytes(buffer),
Err(ioerror) => {
let msg = format!("IO error: {}", ioerror);
Err(msg)
}
}
}
/// Prepares a read-write memory buffer for use with `RWops`.
///
/// This method can only fail if the buffer size is zero.