@@ -6,36 +6,34 @@ use std::{collections::VecDeque, io::Read};
6
6
/// # Example
7
7
///
8
8
/// ```
9
- /// use std::{
10
- /// collections::VecDeque,
11
- /// io::{copy, stdout, Read},
12
- /// };
9
+ /// use std::io::{copy, stdout};
13
10
/// use lib::io::MultiReader;
14
11
///
15
12
/// fn main() -> std::io::Result<()> {
16
13
/// let header = "---- HEADER ----\n".as_bytes();
17
14
/// let content = "Example of MultiReader\n".as_bytes();
18
15
/// let footer = "---- FOOTER ----\n".as_bytes();
19
- /// let mut multi_reader = MultiReader::new(VecDeque::from(vec![
20
- /// Box::new(header) as Box<dyn Read>,
21
- /// Box::new(content) as Box<dyn Read>,
22
- /// Box::new(footer) as Box<dyn Read>,
23
- /// ]));
16
+ /// let mut multi_reader =
17
+ /// MultiReader::new(vec![Box::new(header), Box::new(content), Box::new(footer)]);
24
18
/// copy(&mut multi_reader, &mut stdout())?;
25
19
/// Ok(())
26
20
/// }
27
21
/// ```
28
22
pub struct MultiReader {
29
23
readers : VecDeque < Box < dyn Read > > ,
30
- /// Points to where we read right now .
24
+ /// Points to current element while reading .
31
25
current : Option < Box < dyn Read > > ,
32
26
}
33
27
34
28
impl MultiReader {
35
- /// Creates `MultiReader`. `pos` is set to 0 by default.
36
- pub fn new ( mut readers : VecDeque < Box < dyn Read > > ) -> Self {
37
- let current = readers. pop_front ( ) ;
38
- Self { readers, current }
29
+ /// Constructs `MultiReader`. `current` is set to the first element that is popped out from `VecDeque`.
30
+ pub fn new ( readers : Vec < Box < dyn Read > > ) -> Self {
31
+ let mut deque = VecDeque :: from ( readers) ;
32
+ let current = deque. pop_front ( ) ;
33
+ Self {
34
+ readers : deque,
35
+ current,
36
+ }
39
37
}
40
38
}
41
39
0 commit comments