impls.rs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. use cmp;
  11. use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind};
  12. use fmt;
  13. use mem;
  14. // =============================================================================
  15. // Forwarding implementations
  16. #[stable(feature = "rust1", since = "1.0.0")]
  17. impl<'a, R: Read + ?Sized> Read for &'a mut R {
  18. #[inline]
  19. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  20. (**self).read(buf)
  21. }
  22. #[inline]
  23. fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
  24. (**self).read_to_end(buf)
  25. }
  26. #[inline]
  27. fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
  28. (**self).read_to_string(buf)
  29. }
  30. #[inline]
  31. fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
  32. (**self).read_exact(buf)
  33. }
  34. }
  35. #[stable(feature = "rust1", since = "1.0.0")]
  36. impl<'a, W: Write + ?Sized> Write for &'a mut W {
  37. #[inline]
  38. fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
  39. #[inline]
  40. fn flush(&mut self) -> io::Result<()> { (**self).flush() }
  41. #[inline]
  42. fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
  43. (**self).write_all(buf)
  44. }
  45. #[inline]
  46. fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
  47. (**self).write_fmt(fmt)
  48. }
  49. }
  50. #[stable(feature = "rust1", since = "1.0.0")]
  51. impl<'a, S: Seek + ?Sized> Seek for &'a mut S {
  52. #[inline]
  53. fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
  54. }
  55. #[stable(feature = "rust1", since = "1.0.0")]
  56. impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B {
  57. #[inline]
  58. fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
  59. #[inline]
  60. fn consume(&mut self, amt: usize) { (**self).consume(amt) }
  61. #[inline]
  62. fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
  63. (**self).read_until(byte, buf)
  64. }
  65. #[inline]
  66. fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
  67. (**self).read_line(buf)
  68. }
  69. }
  70. #[stable(feature = "rust1", since = "1.0.0")]
  71. impl<R: Read + ?Sized> Read for Box<R> {
  72. #[inline]
  73. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  74. (**self).read(buf)
  75. }
  76. #[inline]
  77. fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
  78. (**self).read_to_end(buf)
  79. }
  80. #[inline]
  81. fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
  82. (**self).read_to_string(buf)
  83. }
  84. #[inline]
  85. fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
  86. (**self).read_exact(buf)
  87. }
  88. }
  89. #[stable(feature = "rust1", since = "1.0.0")]
  90. impl<W: Write + ?Sized> Write for Box<W> {
  91. #[inline]
  92. fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
  93. #[inline]
  94. fn flush(&mut self) -> io::Result<()> { (**self).flush() }
  95. #[inline]
  96. fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
  97. (**self).write_all(buf)
  98. }
  99. #[inline]
  100. fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
  101. (**self).write_fmt(fmt)
  102. }
  103. }
  104. #[stable(feature = "rust1", since = "1.0.0")]
  105. impl<S: Seek + ?Sized> Seek for Box<S> {
  106. #[inline]
  107. fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
  108. }
  109. #[stable(feature = "rust1", since = "1.0.0")]
  110. impl<B: BufRead + ?Sized> BufRead for Box<B> {
  111. #[inline]
  112. fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
  113. #[inline]
  114. fn consume(&mut self, amt: usize) { (**self).consume(amt) }
  115. #[inline]
  116. fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
  117. (**self).read_until(byte, buf)
  118. }
  119. #[inline]
  120. fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
  121. (**self).read_line(buf)
  122. }
  123. }
  124. // =============================================================================
  125. // In-memory buffer implementations
  126. #[stable(feature = "rust1", since = "1.0.0")]
  127. impl<'a> Read for &'a [u8] {
  128. #[inline]
  129. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  130. let amt = cmp::min(buf.len(), self.len());
  131. let (a, b) = self.split_at(amt);
  132. buf[..amt].copy_from_slice(a);
  133. *self = b;
  134. Ok(amt)
  135. }
  136. #[inline]
  137. fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
  138. if buf.len() > self.len() {
  139. return Err(Error::new(ErrorKind::UnexpectedEof,
  140. "failed to fill whole buffer"));
  141. }
  142. let (a, b) = self.split_at(buf.len());
  143. buf.copy_from_slice(a);
  144. *self = b;
  145. Ok(())
  146. }
  147. }
  148. #[stable(feature = "rust1", since = "1.0.0")]
  149. impl<'a> BufRead for &'a [u8] {
  150. #[inline]
  151. fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) }
  152. #[inline]
  153. fn consume(&mut self, amt: usize) { *self = &self[amt..]; }
  154. }
  155. #[stable(feature = "rust1", since = "1.0.0")]
  156. impl<'a> Write for &'a mut [u8] {
  157. #[inline]
  158. fn write(&mut self, data: &[u8]) -> io::Result<usize> {
  159. let amt = cmp::min(data.len(), self.len());
  160. let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
  161. a.copy_from_slice(&data[..amt]);
  162. *self = b;
  163. Ok(amt)
  164. }
  165. #[inline]
  166. fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
  167. if self.write(data)? == data.len() {
  168. Ok(())
  169. } else {
  170. Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"))
  171. }
  172. }
  173. #[inline]
  174. fn flush(&mut self) -> io::Result<()> { Ok(()) }
  175. }
  176. #[stable(feature = "rust1", since = "1.0.0")]
  177. impl Write for Vec<u8> {
  178. #[inline]
  179. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  180. self.extend_from_slice(buf);
  181. Ok(buf.len())
  182. }
  183. #[inline]
  184. fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
  185. self.extend_from_slice(buf);
  186. Ok(())
  187. }
  188. #[inline]
  189. fn flush(&mut self) -> io::Result<()> { Ok(()) }
  190. }
  191. #[cfg(test)]
  192. mod tests {
  193. use io::prelude::*;
  194. use test;
  195. #[bench]
  196. fn bench_read_slice(b: &mut test::Bencher) {
  197. let buf = [5; 1024];
  198. let mut dst = [0; 128];
  199. b.iter(|| {
  200. let mut rd = &buf[..];
  201. for _ in 0..8 {
  202. let _ = rd.read(&mut dst);
  203. test::black_box(&dst);
  204. }
  205. })
  206. }
  207. #[bench]
  208. fn bench_write_slice(b: &mut test::Bencher) {
  209. let mut buf = [0; 1024];
  210. let src = [5; 128];
  211. b.iter(|| {
  212. let mut wr = &mut buf[..];
  213. for _ in 0..8 {
  214. let _ = wr.write_all(&src);
  215. test::black_box(&wr);
  216. }
  217. })
  218. }
  219. #[bench]
  220. fn bench_read_vec(b: &mut test::Bencher) {
  221. let buf = vec![5; 1024];
  222. let mut dst = [0; 128];
  223. b.iter(|| {
  224. let mut rd = &buf[..];
  225. for _ in 0..8 {
  226. let _ = rd.read(&mut dst);
  227. test::black_box(&dst);
  228. }
  229. })
  230. }
  231. #[bench]
  232. fn bench_write_vec(b: &mut test::Bencher) {
  233. let mut buf = Vec::with_capacity(1024);
  234. let src = [5; 128];
  235. b.iter(|| {
  236. let mut wr = &mut buf[..];
  237. for _ in 0..8 {
  238. let _ = wr.write_all(&src);
  239. test::black_box(&wr);
  240. }
  241. })
  242. }
  243. }