1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use futures::Future;
use futures::future::{Either, ok};
use futures_cpupool::CpuPool;

use std::ops::DerefMut;

use std::io::{self, Read};
use std::mem;

use common::EXP_POOL;

/// Adds buffering to any reader, similar to the [standard `BufReader`], but performs non-buffer
/// reads in a thread pool.
///
/// [standard `BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
///
/// All reads are returned as futures.
///
/// This reader is most useful for wrapping readers that never block or cannot return EWOULDBLOCK,
/// but are slow. Notably, this is useful for wrapping `io::File`.
///
/// All reads must take and own the `BufReader` and the buffer being written for the duration of
/// the read.
///
/// # Examples
/// ```
/// # extern crate futures_cpupool;
/// # extern crate futures;
/// # extern crate futures_bufio;
/// #
/// # use futures::Future;
/// # use futures_cpupool::CpuPool;
/// # use futures_bufio::BufReader;
/// # use std::io;
/// # fn main() {
/// let f = io::Cursor::new(b"normally, we would open a file here here".to_vec());
/// let pool = CpuPool::new(1);
/// let reader = BufReader::with_pool_and_capacity(pool, 10, f);
///
/// let buf = vec![0; 10];
/// let (reader, buf, n) = reader.try_read_full(buf).wait().unwrap_or_else(|(_, _, e)| {
///     // in real usage, we have the option to deconstruct our BufReader or reuse buf here
///     panic!("unable to read full: {}", e);
/// });
/// assert_eq!(n, 10);
/// assert_eq!(&buf[..n], b"normally, ");
/// # }
/// ```
pub struct BufReader<R> {
    inner: R,
    buf: Box<[u8]>,
    pos: usize,
    cap: usize,
    pool: Option<CpuPool>,
}

/// Wraps `R` with the original buffer being read into and the number of bytes read.
type OkRead<R, B> = (R, B, usize);
/// Wraps `R` with the original buffer being read into and the error encountered while reading.
type ErrRead<R, B> = (R, B, io::Error);

impl<R: Read + Send + 'static> BufReader<R> {
    /// Creates and returns a new `BufReader` with an internal buffer of size `cap`.
    ///
    /// # Examples
    /// ```
    /// # extern crate futures_cpupool;
    /// # extern crate futures_bufio;
    /// #
    /// # use futures_cpupool::CpuPool;
    /// # use futures_bufio::BufReader;
    /// # use std::io;
    /// # fn main() {
    /// let f = io::Cursor::new(b"foo text".to_vec());
    /// let pool = CpuPool::new(1);
    /// let reader = BufReader::with_pool_and_capacity(pool, 4<<10, f);
    /// # }
    /// ```
    pub fn with_pool_and_capacity(pool: CpuPool, cap: usize, inner: R) -> BufReader<R> {
        let mut buf = Vec::with_capacity(cap);
        unsafe {
            buf.set_len(cap);
        }
        BufReader::with_pool_and_buf(pool, buf.into_boxed_slice(), inner)
    }

    /// Creates and returns a new `BufReader` with `buf` as the internal buffer.
    ///
    /// # Examples
    /// ```
    /// # extern crate futures_cpupool;
    /// # extern crate futures_bufio;
    /// #
    /// # use futures_cpupool::CpuPool;
    /// # use futures_bufio::BufReader;
    /// # use std::io;
    /// # fn main() {
    /// let f = io::Cursor::new(b"foo text".to_vec());
    /// let pool = CpuPool::new(1);
    /// let buf = vec![0; 4096].into_boxed_slice();
    /// let reader = BufReader::with_pool_and_buf(pool, buf, f);
    /// # }
    /// ```
    pub fn with_pool_and_buf(pool: CpuPool, buf: Box<[u8]>, inner: R) -> BufReader<R> {
        let cap = buf.len();
        BufReader {
            inner: inner,
            buf: buf,
            pos: cap,
            cap: cap,
            pool: Some(pool),
        }
    }

    /// Gets a reference to the underlying reader.
    ///
    /// It is likely invalid to read directly from the underlying reader and then use the `BufReader`
    /// again.
    pub fn get_ref(&self) -> &R {
        &self.inner
    }

    /// Gets a mutable reference to the underlying reader.
    ///
    /// It is likely invalid to read directly from the underlying reader and then use the `BufReader`
    /// again.
    pub fn get_mut(&mut self) -> &R {
        &mut self.inner
    }

    /// Sets the `BufReader`s internal buffer position to `pos`.
    ///
    ///
    /// This is _highly_ unsafe for the following reasons:
    ///
    ///   - the internal buffer may have uninitialized memory, and moving the read position back
    ///     will mean reading uninitialized memory
    ///
    ///   - the pos is not validated, meaning it is possible to move the pos past the end of the
    ///     internal buffer. This will cause a panic on the next use of `try_read_full`.
    ///
    ///   - it is possible to move _past_ the internal "capacity" end position, meaning a read may
    ///     panic due to the beginning of a read being after its end.
    ///
    /// This function should only be used for setting up a new `BufReader` with a buffer that
    /// contains known, existing contents.
    ///
    /// # Examples
    /// ```
    /// # extern crate futures_cpupool;
    /// # extern crate futures;
    /// # extern crate futures_bufio;
    /// #
    /// # use futures::Future;
    /// # use futures_cpupool::CpuPool;
    /// # use futures_bufio::BufReader;
    /// # use std::io;
    /// # fn main() {
    /// let f = io::Cursor::new(vec![]);
    /// let pool = CpuPool::new(1);
    /// let mut buf = vec![0; 4096].into_boxed_slice();
    ///
    /// let p = b"pre-existing text";
    /// let buf_len = buf.len();
    ///
    /// // copy some known text to our buffer - note it must be at the end
    /// &mut buf[buf_len-p.len()..].copy_from_slice(p);
    ///
    /// let mut reader = BufReader::with_pool_and_buf(pool, buf, f);
    ///
    /// // unsafely move the reader's position to the beginning of our known text, and read it
    /// unsafe { reader.set_pos(buf_len-p.len()); }
    /// let (_, b, _) = reader
    ///     .try_read_full(vec![0; p.len()])
    ///     .wait()
    ///     .unwrap_or_else(|(_, _, e)| {
    ///         panic!("unable to read: {}", e);
    ///     });
    ///
    /// // our read should be all of our known contents
    /// assert_eq!(&*b, p);
    /// # }
    /// ```
    pub unsafe fn set_pos(&mut self, pos: usize) {
        self.pos = pos;
    }

    /// Returns the internal components of a `BufReader`, allowing reuse. This
    /// is unsafe because it does not zero the memory of the buffer, meaning
    /// the buffer could countain uninitialized memory.
    ///
    /// # Examples
    /// ```
    /// # extern crate futures_cpupool;
    /// # extern crate futures_bufio;
    /// #
    /// # use futures_cpupool::CpuPool;
    /// # use futures_bufio::BufReader;
    /// # use std::io;
    /// # fn main() {
    /// let f = io::Cursor::new(b"foo text".to_vec());
    /// let pool = CpuPool::new(1);
    /// let reader = BufReader::with_pool_and_capacity(pool, 4<<10, f);
    ///
    /// let (f, buf, pool) = unsafe { reader.components() };
    /// assert_eq!(f.get_ref(), b"foo text");
    /// assert_eq!(buf.len(), 4<<10);
    /// # }
    /// ```
    pub unsafe fn components(mut self) -> (R, Box<[u8]>, CpuPool) {
        let r = mem::replace(&mut self.inner, mem::uninitialized());
        let buf = mem::replace(&mut self.buf, mem::uninitialized());
        let mut pool = mem::replace(&mut self.pool, mem::uninitialized());
        let pool = pool.take().expect(EXP_POOL);
        mem::forget(self);
        (r, buf, pool)
    }

    /// Reads into `buf` until `buf` is filled or the underlying reader returns a zero read (hits
    /// EOF).
    ///
    /// This returns the buffer and the number of bytes read. The buffer may need sized down on use
    /// if this returns with a short read.
    ///
    /// If used on `io::File`'s, `BufReader` could be valuable for performing page-aligned reads.
    /// In this case, once this function returns a short read, we reached EOF and any futures
    /// reads may be un-aligned.
    ///
    /// # Examples
    /// ```
    /// # extern crate futures_cpupool;
    /// # extern crate futures;
    /// # extern crate futures_bufio;
    /// #
    /// # use futures::Future;
    /// # use futures_cpupool::CpuPool;
    /// # use futures_bufio::BufReader;
    /// # use std::io;
    /// # fn main() {
    /// let f = io::Cursor::new(b"foo text".to_vec());
    /// let pool = CpuPool::new(1);
    /// let reader = BufReader::with_pool_and_capacity(pool, 10, f);
    ///
    /// let buf = vec![0; 10];
    /// let (reader, buf, n) = reader.try_read_full(buf).wait().unwrap_or_else(|(_, _, e)| {
    ///     // in real usage, we have the option to deconstruct our BufReader or reuse buf here
    ///     panic!("unable to read full: {}", e);
    /// });
    /// assert_eq!(n, 8);
    /// assert_eq!(&*buf, b"foo text\0\0");
    /// assert_eq!(&buf[..n], b"foo text");
    /// # }
    /// ```
    pub fn try_read_full<B>(
        mut self,
        mut buf: B,
    ) -> impl Future<Item = OkRead<Self, B>, Error = ErrRead<Self, B>>
    where
        B: DerefMut<Target = [u8]> + Send + 'static,
    {
        const U8READ: &str = "&[u8] reads never error";
        let mut rem = buf.len();
        let mut at = 0;

        if self.pos != self.cap {
            at = (&self.buf[self.pos..self.cap]).read(&mut buf).expect(
                U8READ,
            );
            rem -= at;
            self.pos += at;

            if rem == 0 {
                return Either::A(ok::<OkRead<Self, B>, ErrRead<Self, B>>((self, buf, at)));
            }
        }
        // self.pos == self.cap

        let pool = self.pool.take().expect(EXP_POOL);

        let block = if self.cap > 0 {
            rem - rem % self.cap
        } else {
            rem
        };

        let fut = pool.spawn_fn(move || {
            if block > 0 {
                let (block_read, err) = try_read_full(&mut self.inner, &mut buf[at..at + block]);
                if let Some(e) = err {
                    return Err((self, buf, e));
                }

                at += block_read;
                rem -= block_read;
                if rem == 0 {
                    return Ok((self, buf, at));
                }
            }

            let (buf_read, err) = try_read_full(&mut self.inner, &mut self.buf);
            match err {
                Some(e) => Err((self, buf, e)),
                None => {
                    self.cap = buf_read;
                    self.pos = (&self.buf[..self.cap]).read(&mut buf[at..]).expect(U8READ);
                    at += self.pos;
                    Ok((self, buf, at))
                }
            }
        });

        Either::B(fut.then(|res| match res {
            Ok(mut x) => {
                x.0.pool = Some(pool);
                Ok(x)
            }
            Err(mut x) => {
                x.0.pool = Some(pool);
                Err(x)
            }
        }))
    }
}

fn try_read_full<R: Read>(r: &mut R, mut buf: &mut [u8]) -> (usize, Option<io::Error>) {
    let mut nn: usize = 0;
    while !buf.is_empty() {
        match r.read(buf) {
            Ok(0) => break,
            Ok(n) => {
                let tmp = buf;
                buf = &mut tmp[n..];
                nn += n;
            }
            Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
            Err(e) => return (nn, Some(e)),
        }
    }
    (nn, None)
}

#[test]
fn test_read() {
    use std::fs;
    use std::io::Write;

    // create the test file
    fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open("bar.txt")
        .expect("unable to exclusively create foo")
        .write_all(
            "Strapped down to my bed, feet cold, eyes red. I'm out of my head. Am I alive? Am I \
             dead?"
                .as_bytes(),
        )
        .expect("unable to write all");


    // create our buffered reader
    let f = BufReader::with_pool_and_capacity(
        CpuPool::new(1),
        10,
        fs::OpenOptions::new().read(true).open("bar.txt").expect(
            "foo does not exist?",
        ),
    );
    assert_eq!(f.pos, 10);

    // disk read, no blocks
    let (f, buf, n) = f.try_read_full(vec![0; 5]).wait().unwrap_or_else(
        |(_, _, e)| {
            panic!("unable to read: {}", e)
        },
    );
    assert_eq!(n, 5);
    assert_eq!(&*buf, b"Strap");
    assert_eq!(f.pos, 5);
    assert_eq!(f.cap, 10);
    assert_eq!(&*f.buf, b"Strapped d");

    // mem read only
    let (f, buf, n) = f.try_read_full(vec![0; 2]).wait().unwrap_or_else(
        |(_, _, e)| {
            panic!("unable to read: {}", e)
        },
    );
    assert_eq!(n, 2);
    assert_eq!(&*buf, b"pe");
    assert_eq!(f.pos, 7);
    assert_eq!(f.cap, 10);
    assert_eq!(&*f.buf, b"Strapped d");

    // mem (3) + disk blocks (20) + more mem (2)
    let (f, buf, n) = f.try_read_full(vec![0; 25]).wait().unwrap_or_else(
        |(_, _, e)| {
            panic!("unable to read: {}", e)
        },
    );
    assert_eq!(n, 25);
    assert_eq!(&*buf, b"d down to my bed, feet co");
    assert_eq!(f.pos, 2);
    assert_eq!(f.cap, 10);
    assert_eq!(&*f.buf, b"cold, eyes");

    // mem (8) + disk block (10)
    let (f, buf, n) = f.try_read_full(vec![0; 18]).wait().unwrap_or_else(
        |(_, _, e)| {
            panic!("unable to read: {}", e)
        },
    );
    assert_eq!(n, 18);
    assert_eq!(&*buf, b"ld, eyes red. I'm ");
    assert_eq!(f.pos, 10);
    assert_eq!(f.cap, 10);
    assert_eq!(&*f.buf, b"cold, eyes"); // non-reset buf

    // disk block (10)
    let (f, buf, n) = f.try_read_full(vec![0; 10]).wait().unwrap_or_else(
        |(_, _, e)| {
            panic!("unable to read: {}", e)
        },
    );
    assert_eq!(n, 10);
    assert_eq!(&*buf, b"out of my ");
    assert_eq!(f.pos, 10);
    assert_eq!(f.cap, 10);
    assert_eq!(&*f.buf, b"cold, eyes");

    // disk block (20) + mem (9) (over-read by one byte)
    let (f, buf, n) = f.try_read_full(vec![0; 29]).wait().unwrap_or_else(
        |(_, _, e)| {
            panic!("unable to read: {}", e)
        },
    );
    assert_eq!(n, 28);
    assert_eq!(&*buf, b"head. Am I alive? Am I dead?\0");
    assert_eq!(f.pos, 8);
    assert_eq!(f.cap, 8);
    assert_eq!(&*f.buf, b" I dead?es");

    let (f, buf, n) = f.try_read_full(vec![0; 2]).wait().unwrap_or_else(
        |(_, _, e)| {
            panic!("unable to read: {}", e)
        },
    );
    assert_eq!(n, 0);
    assert_eq!(&*buf, b"\0\0");
    assert_eq!(f.pos, 0);
    assert_eq!(f.cap, 0);
    assert_eq!(&*f.buf, b" I dead?es");

    fs::remove_file("bar.txt").expect("expected file to be removed");
}