|
15 | 15 | use cmp;
|
16 | 16 | use fmt;
|
17 | 17 | use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
|
18 |
| -use iter::{IteratorExt, ExactSizeIterator}; |
| 18 | +use iter::{IteratorExt, ExactSizeIterator, repeat}; |
19 | 19 | use ops::Drop;
|
20 | 20 | use option::Option;
|
21 | 21 | use option::Option::{Some, None};
|
@@ -62,17 +62,11 @@ impl<R> fmt::Show for BufferedReader<R> where R: fmt::Show {
|
62 | 62 | impl<R: Reader> BufferedReader<R> {
|
63 | 63 | /// Creates a new `BufferedReader` with the specified buffer capacity
|
64 | 64 | pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
|
65 |
| - // It's *much* faster to create an uninitialized buffer than it is to |
66 |
| - // fill everything in with 0. This buffer is entirely an implementation |
67 |
| - // detail and is never exposed, so we're safe to not initialize |
68 |
| - // everything up-front. This allows creation of BufferedReader instances |
69 |
| - // to be very cheap (large mallocs are not nearly as expensive as large |
70 |
| - // callocs). |
71 |
| - let mut buf = Vec::with_capacity(cap); |
72 |
| - unsafe { buf.set_len(cap); } |
73 | 65 | BufferedReader {
|
74 | 66 | inner: inner,
|
75 |
| - buf: buf, |
| 67 | + // We can't use the same trick here as we do for BufferedWriter, |
| 68 | + // since this memory is visible to the inner Reader. |
| 69 | + buf: repeat(0).take(cap).collect(), |
76 | 70 | pos: 0,
|
77 | 71 | cap: 0,
|
78 | 72 | }
|
@@ -166,7 +160,12 @@ impl<W> fmt::Show for BufferedWriter<W> where W: fmt::Show {
|
166 | 160 | impl<W: Writer> BufferedWriter<W> {
|
167 | 161 | /// Creates a new `BufferedWriter` with the specified buffer capacity
|
168 | 162 | pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
|
169 |
| - // See comments in BufferedReader for why this uses unsafe code. |
| 163 | + // It's *much* faster to create an uninitialized buffer than it is to |
| 164 | + // fill everything in with 0. This buffer is entirely an implementation |
| 165 | + // detail and is never exposed, so we're safe to not initialize |
| 166 | + // everything up-front. This allows creation of BufferedWriter instances |
| 167 | + // to be very cheap (large mallocs are not nearly as expensive as large |
| 168 | + // callocs). |
170 | 169 | let mut buf = Vec::with_capacity(cap);
|
171 | 170 | unsafe { buf.set_len(cap); }
|
172 | 171 | BufferedWriter {
|
|
0 commit comments