(index<- ) ./libstd/rt/io/mock.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 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use option::{Option, None};
12 use rt::io::{Reader, Writer};
13
14 pub struct MockReader {
15 read: ~fn(buf: &mut [u8]) -> Option<uint>,
16 eof: ~fn() -> bool
17 }
18
19 impl MockReader {
20 pub fn new() -> MockReader {
21 MockReader {
22 read: |_| None,
23 eof: || false
24 }
25 }
26 }
27
28 impl Reader for MockReader {
29 fn read(&mut self, buf: &mut [u8]) -> Option<uint> { (self.read)(buf) }
30 fn eof(&mut self) -> bool { (self.eof)() }
31 }
32
33 pub struct MockWriter {
34 write: ~fn(buf: &[u8]),
35 flush: ~fn()
36 }
37
38 impl MockWriter {
39 pub fn new() -> MockWriter {
40 MockWriter {
41 write: |_| (),
42 flush: || ()
43 }
44 }
45 }
46
47 impl Writer for MockWriter {
48 fn write(&mut self, buf: &[u8]) { (self.write)(buf) }
49 fn flush(&mut self) { (self.flush)() }
50 }