├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | 3 | matrix: 4 | include: 5 | - rust: stable 6 | - rust: beta 7 | - rust: nightly 8 | - name: "master doc to gh-pages" 9 | rust: nightly 10 | script: 11 | - cargo doc --no-deps 12 | - curl -O https://raw.githubusercontent.com/alexcrichton/rust-travis-deploy/master/travis-deploy.rs 13 | - rustc travis-deploy.rs 14 | - (cd target/doc && ../../travis-deploy) 15 | 16 | script: 17 | - cargo build --verbose 18 | - cargo test --verbose 19 | - cargo test --verbose --features tokio 20 | 21 | notifications: 22 | email: 23 | on_success: never 24 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bufstream" 3 | version = "0.1.4" 4 | authors = ["The Rust Project Developers"] 5 | license = "MIT/Apache-2.0" 6 | repository = "https://github.com/alexcrichton/bufstream" 7 | homepage = "https://github.com/alexcrichton/bufstream" 8 | documentation = "https://docs.rs/bufstream" 9 | description = """ 10 | Buffered I/O for streams where each read/write half is separately buffered 11 | """ 12 | 13 | [dependencies.futures] 14 | optional = true 15 | version = "0.1.13" 16 | 17 | [dependencies.tokio-io] 18 | optional = true 19 | version = "0.1.1" 20 | 21 | [features] 22 | default = [] 23 | tokio = ["futures", "tokio-io"] 24 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 The Rust Project Developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bufstream 2 | ========= 3 | 4 | Buffered I/O streams for reading/writing 5 | 6 | [![Build Status](https://travis-ci.org/alexcrichton/bufstream.svg?branch=master)](https://travis-ci.org/alexcrichton/bufstream) 7 | 8 | [Documentation](https://docs.rs/bufstream/) 9 | 10 | ## Usage 11 | 12 | ```toml 13 | [dependencies] 14 | bufstream = "0.1" 15 | ``` 16 | 17 | ## Tokio 18 | 19 | There is support for tokio's `AsyncRead` + `AsyncWrite` traits through the `tokio` 20 | feature. When using this crate with asynchronous IO, make sure to properly flush 21 | the stream before dropping it since IO during drop may cause panics. For the same 22 | reason you should stay away from `BufStream::into_inner`. 23 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 or the MIT license 7 | // , at your 8 | // option. This file may not be copied, modified, or distributed 9 | // except according to those terms. 10 | 11 | //! A crate for separately buffered streams. 12 | //! 13 | //! This crate provides a `BufStream` type which provides buffering of both the 14 | //! reading and writing halves of a `Read + Write` type. Each half is completely 15 | //! independently buffered of the other, which may not always be desired. For 16 | //! example `BufStream` may have surprising semantics. 17 | //! 18 | //! # Usage 19 | //! 20 | //! ```toml 21 | //! [dependencies] 22 | //! bufstream = "0.1" 23 | //! ``` 24 | //! 25 | //! ```no_run 26 | //! use std::io::prelude::*; 27 | //! use std::net::TcpStream; 28 | //! use bufstream::BufStream; 29 | //! 30 | //! 31 | //! let stream = TcpStream::connect("localhost:4000").unwrap(); 32 | //! let mut buf = BufStream::new(stream); 33 | //! buf.read(&mut [0; 1024]).unwrap(); 34 | //! buf.write(&[0; 1024]).unwrap(); 35 | //! ``` 36 | //! 37 | //! # Async I/O 38 | //! 39 | //! This crate optionally can support async I/O streams with the [Tokio stack] via 40 | //! the `tokio` feature of this crate: 41 | //! 42 | //! [Tokio stack]: https://tokio.rs/ 43 | //! 44 | //! ```toml 45 | //! bufstream = { version = "0.2", features = ["tokio"] } 46 | //! ``` 47 | //! 48 | //! All methods are internally capable of working with streams that may return 49 | //! [`ErrorKind::WouldBlock`] when they're not ready to perform the particular 50 | //! operation. 51 | //! 52 | //! [`ErrorKind::WouldBlock`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html 53 | //! 54 | //! Note that care needs to be taken when using these objects, however. The 55 | //! Tokio runtime, in particular, requires that data is fully flushed before 56 | //! dropping streams. For compatibility with blocking streams all streams are 57 | //! flushed/written when they are dropped, and this is not always a suitable 58 | //! time to perform I/O. If I/O streams are flushed before drop, however, then 59 | //! these operations will be a noop. 60 | 61 | #[cfg(feature = "tokio")] extern crate futures; 62 | #[cfg(feature = "tokio")] #[macro_use] extern crate tokio_io; 63 | 64 | use std::fmt; 65 | use std::io::prelude::*; 66 | use std::io::{self, BufReader, BufWriter, Seek, SeekFrom}; 67 | use std::error; 68 | 69 | #[cfg(feature = "tokio")] use futures::{Async, Poll}; 70 | #[cfg(feature = "tokio")] use tokio_io::{AsyncRead, AsyncWrite}; 71 | 72 | const DEFAULT_BUF_SIZE: usize = 8 * 1024; 73 | 74 | /// Wraps a Stream and buffers input and output to and from it. 75 | /// 76 | /// It can be excessively inefficient to work directly with a `Read+Write`. For 77 | /// example, every call to `read` or `write` on `TcpStream` results in a system 78 | /// call. A `BufStream` keeps in memory buffers of data, making large, 79 | /// infrequent calls to `read` and `write` on the underlying `Read+Write`. 80 | /// 81 | /// The output buffer will be written out when this stream is dropped. 82 | #[derive(Debug)] 83 | pub struct BufStream { 84 | inner: BufReader> 85 | } 86 | 87 | /// An error returned by `into_inner` which combines an error that 88 | /// happened while writing out the buffer, and the buffered writer object 89 | /// which may be used to recover from the condition. 90 | #[derive(Debug)] 91 | pub struct IntoInnerError(W, io::Error); 92 | 93 | impl IntoInnerError { 94 | /// Returns the error which caused the call to `into_inner()` to fail. 95 | /// 96 | /// This error was returned when attempting to write the internal buffer. 97 | pub fn error(&self) -> &io::Error { &self.1 } 98 | /// Returns the buffered writer instance which generated the error. 99 | /// 100 | /// The returned object can be used for error recovery, such as 101 | /// re-inspecting the buffer. 102 | pub fn into_inner(self) -> W { self.0 } 103 | } 104 | 105 | impl From> for io::Error { 106 | fn from(iie: IntoInnerError) -> io::Error { iie.1 } 107 | } 108 | 109 | impl error::Error for IntoInnerError { 110 | fn description(&self) -> &str { 111 | error::Error::description(self.error()) 112 | } 113 | } 114 | 115 | impl fmt::Display for IntoInnerError { 116 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 117 | self.error().fmt(f) 118 | } 119 | } 120 | 121 | struct InternalBufWriter(Option>); 122 | 123 | impl InternalBufWriter { 124 | fn get_ref(&self) -> &BufWriter { 125 | self.0.as_ref().unwrap() 126 | } 127 | 128 | fn get_mut(&mut self) -> &mut BufWriter { 129 | self.0.as_mut().unwrap() 130 | } 131 | } 132 | 133 | impl Read for InternalBufWriter { 134 | fn read(&mut self, buf: &mut [u8]) -> io::Result { 135 | self.get_mut().get_mut().read(buf) 136 | } 137 | } 138 | 139 | impl fmt::Debug for InternalBufWriter { 140 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 141 | self.get_ref().fmt(f) 142 | } 143 | } 144 | 145 | impl Seek for InternalBufWriter { 146 | fn seek(&mut self, pos: SeekFrom) -> io::Result { 147 | self.get_mut().get_mut().seek(pos) 148 | } 149 | } 150 | 151 | impl BufStream { 152 | /// Creates a new buffered stream with explicitly listed capacities for the 153 | /// reader/writer buffer. 154 | pub fn with_capacities(reader_cap: usize, writer_cap: usize, inner: S) 155 | -> BufStream { 156 | let writer = BufWriter::with_capacity(writer_cap, inner); 157 | let internal_writer = InternalBufWriter(Some(writer)); 158 | let reader = BufReader::with_capacity(reader_cap, internal_writer); 159 | BufStream { inner: reader } 160 | } 161 | 162 | /// Creates a new buffered stream with the default reader/writer buffer 163 | /// capacities. 164 | pub fn new(inner: S) -> BufStream { 165 | BufStream::with_capacities(DEFAULT_BUF_SIZE, DEFAULT_BUF_SIZE, inner) 166 | } 167 | 168 | /// Gets a reference to the underlying stream. 169 | pub fn get_ref(&self) -> &S { 170 | self.inner.get_ref().get_ref().get_ref() 171 | } 172 | 173 | /// Gets a mutable reference to the underlying stream. 174 | /// 175 | /// # Warning 176 | /// 177 | /// It is inadvisable to read directly from or write directly to the 178 | /// underlying stream. 179 | pub fn get_mut(&mut self) -> &mut S { 180 | self.inner.get_mut().get_mut().get_mut() 181 | } 182 | 183 | /// Unwraps this `BufStream`, returning the underlying stream. 184 | /// 185 | /// The internal write buffer is written out before returning the stream. 186 | /// Any leftover data in the read buffer is lost. 187 | pub fn into_inner(mut self) -> Result>> { 188 | let e = { 189 | let InternalBufWriter(ref mut w) = *self.inner.get_mut(); 190 | let (e, w2) = match w.take().unwrap().into_inner() { 191 | Ok(s) => return Ok(s), 192 | Err(err) => { 193 | (io::Error::new(err.error().kind(), err.error().to_string()), 194 | err.into_inner()) 195 | } 196 | }; 197 | *w = Some(w2); 198 | e 199 | }; 200 | Err(IntoInnerError(self, e)) 201 | } 202 | } 203 | 204 | impl BufRead for BufStream { 205 | fn fill_buf(&mut self) -> io::Result<&[u8]> { self.inner.fill_buf() } 206 | fn consume(&mut self, amt: usize) { self.inner.consume(amt) } 207 | fn read_until(&mut self, byte: u8, buf: &mut Vec) -> io::Result { 208 | self.inner.read_until(byte, buf) 209 | } 210 | fn read_line(&mut self, string: &mut String) -> io::Result { 211 | self.inner.read_line(string) 212 | } 213 | } 214 | 215 | impl Read for BufStream { 216 | fn read(&mut self, buf: &mut [u8]) -> io::Result { 217 | self.inner.read(buf) 218 | } 219 | } 220 | 221 | impl Write for BufStream { 222 | fn write(&mut self, buf: &[u8]) -> io::Result { 223 | self.inner.get_mut().0.as_mut().unwrap().write(buf) 224 | } 225 | fn flush(&mut self) -> io::Result<()> { 226 | self.inner.get_mut().0.as_mut().unwrap().flush() 227 | } 228 | } 229 | 230 | impl Seek for BufStream { 231 | fn seek(&mut self, pos: SeekFrom) -> io::Result { 232 | self.inner.seek(pos) 233 | } 234 | } 235 | 236 | #[cfg(feature = "tokio")] 237 | impl BufStream { 238 | /// Unwraps this `BufStream`, asynchronously returning the underlying stream. 239 | /// 240 | /// The internal write buffer is written out before returning the stream. 241 | /// Any leftover data in the read buffer is lost. 242 | pub fn poll_into_inner(&mut self) -> Poll { 243 | // Do the flush asynchronously so that the underlying BufWriter will 244 | // skip its blocking flush call 245 | match self.poll_flush() { 246 | Ok(Async::Ready(_)) => { 247 | let InternalBufWriter(ref mut w) = *self.inner.get_mut(); 248 | match w.take().unwrap().into_inner() { 249 | Err(err) => Err(io::Error::new(err.error().kind(), err.error().to_string())), 250 | Ok(s) => Ok(Async::Ready(s)), 251 | } 252 | } 253 | Ok(Async::NotReady) => Ok(Async::NotReady), 254 | Err(err) => Err(err), 255 | } 256 | } 257 | } 258 | 259 | #[cfg(feature = "tokio")] 260 | impl AsyncRead for BufStream {} 261 | 262 | #[cfg(feature = "tokio")] 263 | impl AsyncWrite for BufStream { 264 | fn shutdown(&mut self) -> Poll<(), io::Error> { 265 | try_nb!(self.flush()); 266 | let mut inner = self.inner.get_mut().0.as_mut().unwrap(); 267 | inner.shutdown() 268 | } 269 | } 270 | 271 | #[cfg(test)] 272 | mod tests { 273 | use std::io::prelude::*; 274 | use std::io; 275 | 276 | use super::BufStream; 277 | // This is just here to make sure that we don't infinite loop in the 278 | // newtype struct autoderef weirdness 279 | #[test] 280 | fn test_buffered_stream() { 281 | struct S; 282 | 283 | impl Write for S { 284 | fn write(&mut self, b: &[u8]) -> io::Result { Ok(b.len()) } 285 | fn flush(&mut self) -> io::Result<()> { Ok(()) } 286 | } 287 | 288 | impl Read for S { 289 | fn read(&mut self, _: &mut [u8]) -> io::Result { Ok(0) } 290 | } 291 | 292 | let mut stream = BufStream::new(S); 293 | assert_eq!(stream.read(&mut [0; 10]).unwrap(), 0); 294 | stream.write(&[0; 10]).unwrap(); 295 | stream.flush().unwrap(); 296 | } 297 | } 298 | --------------------------------------------------------------------------------