(index<- ) ./libstd/io/result.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Wed Apr 9 17:27:02 2014
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 //! Implementations of I/O traits for the IoResult type
12 //!
13 //! I/O constructors return option types to allow errors to be handled.
14 //! These implementations allow e.g. `IoResult<File>` to be used
15 //! as a `Reader` without unwrapping the result first.
16
17 use clone::Clone;
18 use result::{Ok, Err};
19 use super::{Reader, Writer, Listener, Acceptor, Seek, SeekStyle, IoResult};
20
21 impl<W: Writer> Writer for IoResult<W> {
22 fn write(&mut self, buf: &[u8]) -> IoResult<()> {
23 match *self {
24 Ok(ref mut writer) => writer.write(buf),
25 Err(ref e) => Err((*e).clone())
26 }
27 }
28
29 fn flush(&mut self) -> IoResult<()> {
30 match *self {
31 Ok(ref mut writer) => writer.flush(),
32 Err(ref e) => Err(e.clone()),
33 }
34 }
35 }
36
37 impl<R: Reader> Reader for IoResult<R> {
38 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
39 match *self {
40 Ok(ref mut reader) => reader.read(buf),
41 Err(ref e) => Err(e.clone()),
42 }
43 }
44 }
45
46 impl<S: Seek> Seek for IoResult<S> {
47 fn tell(&self) -> IoResult<u64> {
48 match *self {
49 Ok(ref seeker) => seeker.tell(),
50 Err(ref e) => Err(e.clone()),
51 }
52 }
53 fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()> {
54 match *self {
55 Ok(ref mut seeker) => seeker.seek(pos, style),
56 Err(ref e) => Err(e.clone())
57 }
58 }
59 }
60
61 impl<T, A: Acceptor<T>, L: Listener<T, A>> Listener<T, A> for IoResult<L> {
62 fn listen(self) -> IoResult<A> {
63 match self {
64 Ok(listener) => listener.listen(),
65 Err(e) => Err(e),
66 }
67 }
68 }
69
70 impl<T, A: Acceptor<T>> Acceptor<T> for IoResult<A> {
71 fn accept(&mut self) -> IoResult<T> {
72 match *self {
73 Ok(ref mut acceptor) => acceptor.accept(),
74 Err(ref e) => Err(e.clone()),
75 }
76 }
77 }
78
79 #[cfg(test)]
80 mod test {
81 use prelude::*;
82 use super::super::mem::*;
83 use io;
84
85 #[test]
86 fn test_option_writer() {
87 let mut writer: io::IoResult<MemWriter> = Ok(MemWriter::new());
88 writer.write([0, 1, 2]).unwrap();
89 writer.flush().unwrap();
90 assert_eq!(writer.unwrap().unwrap(), vec!(0, 1, 2));
91 }
92
93 #[test]
94 fn test_option_writer_error() {
95 let mut writer: io::IoResult<MemWriter> =
96 Err(io::standard_error(io::EndOfFile));
97
98 match writer.write([0, 0, 0]) {
99 Ok(..) => fail!(),
100 Err(e) => assert_eq!(e.kind, io::EndOfFile),
101 }
102 match writer.flush() {
103 Ok(..) => fail!(),
104 Err(e) => assert_eq!(e.kind, io::EndOfFile),
105 }
106 }
107
108 #[test]
109 fn test_option_reader() {
110 let mut reader: io::IoResult<MemReader> =
111 Ok(MemReader::new(vec!(0, 1, 2, 3)));
112 let mut buf = [0, 0];
113 reader.read(buf).unwrap();
114 assert_eq!(buf.as_slice(), &[0, 1]);
115 }
116
117 #[test]
118 fn test_option_reader_error() {
119 let mut reader: io::IoResult<MemReader> =
120 Err(io::standard_error(io::EndOfFile));
121 let mut buf = [];
122
123 match reader.read(buf) {
124 Ok(..) => fail!(),
125 Err(e) => assert_eq!(e.kind, io::EndOfFile),
126 }
127 }
128 }