|
15 | 15 |
|
16 | 16 | use uint;
|
17 | 17 | use int;
|
| 18 | +use iterator::Iterator; |
18 | 19 | use vec;
|
19 |
| -use rt::io::{Reader, Writer}; |
| 20 | +use rt::io::{Reader, Writer, Decorator}; |
20 | 21 | use rt::io::{read_error, standard_error, EndOfFile, DEFAULT_BUF_SIZE};
|
21 | 22 | use option::{Option, Some, None};
|
22 | 23 | use unstable::finally::Finally;
|
@@ -62,6 +63,16 @@ pub trait ReaderUtil {
|
62 | 63 | /// Raises the same conditions as the `read` method.
|
63 | 64 | fn read_to_end(&mut self) -> ~[u8];
|
64 | 65 |
|
| 66 | + /// Create an iterator that reads a single byte on |
| 67 | + /// each iteration, until EOF. |
| 68 | + /// |
| 69 | + /// # Failure |
| 70 | + /// |
| 71 | + /// Raises the same conditions as the `read` method, for |
| 72 | + /// each call to its `.next()` method. |
| 73 | + /// Ends the iteration if the condition is handled. |
| 74 | + fn bytes(self) -> ByteIterator<Self>; |
| 75 | + |
65 | 76 | }
|
66 | 77 |
|
67 | 78 | pub trait ReaderByteConversions {
|
@@ -337,6 +348,41 @@ impl<T: Reader> ReaderUtil for T {
|
337 | 348 | }
|
338 | 349 | return buf;
|
339 | 350 | }
|
| 351 | + |
| 352 | + fn bytes(self) -> ByteIterator<T> { |
| 353 | + ByteIterator{reader: self} |
| 354 | + } |
| 355 | +} |
| 356 | + |
| 357 | +/// An iterator that reads a single byte on each iteration, |
| 358 | +/// until `.read_byte()` returns `None`. |
| 359 | +/// |
| 360 | +/// # Notes about the Iteration Protocol |
| 361 | +/// |
| 362 | +/// The `ByteIterator` may yield `None` and thus terminate |
| 363 | +/// an iteration, but continue to yield elements if iteration |
| 364 | +/// is attempted again. |
| 365 | +/// |
| 366 | +/// # Failure |
| 367 | +/// |
| 368 | +/// Raises the same conditions as the `read` method, for |
| 369 | +/// each call to its `.next()` method. |
| 370 | +/// Yields `None` if the condition is handled. |
| 371 | +pub struct ByteIterator<T> { |
| 372 | + priv reader: T, |
| 373 | +} |
| 374 | + |
| 375 | +impl<R> Decorator<R> for ByteIterator<R> { |
| 376 | + fn inner(self) -> R { self.reader } |
| 377 | + fn inner_ref<'a>(&'a self) -> &'a R { &self.reader } |
| 378 | + fn inner_mut_ref<'a>(&'a mut self) -> &'a mut R { &mut self.reader } |
| 379 | +} |
| 380 | + |
| 381 | +impl<'self, R: Reader> Iterator<u8> for ByteIterator<R> { |
| 382 | + #[inline] |
| 383 | + fn next(&mut self) -> Option<u8> { |
| 384 | + self.reader.read_byte() |
| 385 | + } |
340 | 386 | }
|
341 | 387 |
|
342 | 388 | impl<T: Reader> ReaderByteConversions for T {
|
@@ -646,6 +692,48 @@ mod test {
|
646 | 692 | }
|
647 | 693 | }
|
648 | 694 |
|
| 695 | + #[test] |
| 696 | + fn bytes_0_bytes() { |
| 697 | + let mut reader = MockReader::new(); |
| 698 | + let count = Cell::new(0); |
| 699 | + reader.read = |buf| { |
| 700 | + do count.with_mut_ref |count| { |
| 701 | + if *count == 0 { |
| 702 | + *count = 1; |
| 703 | + Some(0) |
| 704 | + } else { |
| 705 | + buf[0] = 10; |
| 706 | + Some(1) |
| 707 | + } |
| 708 | + } |
| 709 | + }; |
| 710 | + let byte = reader.bytes().next(); |
| 711 | + assert!(byte == Some(10)); |
| 712 | + } |
| 713 | + |
| 714 | + #[test] |
| 715 | + fn bytes_eof() { |
| 716 | + let mut reader = MockReader::new(); |
| 717 | + reader.read = |_| None; |
| 718 | + let byte = reader.bytes().next(); |
| 719 | + assert!(byte == None); |
| 720 | + } |
| 721 | + |
| 722 | + #[test] |
| 723 | + fn bytes_error() { |
| 724 | + let mut reader = MockReader::new(); |
| 725 | + reader.read = |_| { |
| 726 | + read_error::cond.raise(placeholder_error()); |
| 727 | + None |
| 728 | + }; |
| 729 | + let mut it = reader.bytes(); |
| 730 | + do read_error::cond.trap(|_| ()).inside { |
| 731 | + let byte = it.next(); |
| 732 | + assert!(byte == None); |
| 733 | + } |
| 734 | + } |
| 735 | + |
| 736 | + |
649 | 737 | #[test]
|
650 | 738 | fn read_bytes() {
|
651 | 739 | let mut reader = MemReader::new(~[10, 11, 12, 13]);
|
|
0 commit comments