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
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use prelude::*;
use comm::{Port, Chan};
use cmp;
use io;
use option::{None, Option, Some};
use super::{Reader, Writer};
use vec::{bytes, CopyableVector, MutableVector, ImmutableVector};
/// Allows reading from a port.
///
/// # Example
///
/// ```
/// let reader = PortReader::new(port);
///
/// let mut buf = ~[0u8, ..100];
/// match reader.read(buf) {
/// Some(nread) => println!("Read {} bytes", nread),
/// None => println!("At the end of the stream!")
/// }
/// ```
pub struct PortReader {
priv buf: Option<~[u8]>, // A buffer of bytes received but not consumed.
priv pos: uint, // How many of the buffered bytes have already be consumed.
priv port: Port<~[u8]>, // The port to pull data from.
priv closed: bool, // Whether the pipe this port connects to has been closed.
}
impl PortReader {
pub fn new(port: Port<~[u8]>) -> PortReader {
PortReader {
buf: None,
pos: 0,
port: port,
closed: false,
}
}
}
impl Reader for PortReader {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
let mut num_read = 0;
loop {
match self.buf {
Some(ref prev) => {
let dst = buf.mut_slice_from(num_read);
let src = prev.slice_from(self.pos);
let count = cmp::min(dst.len(), src.len());
bytes::copy_memory(dst, src.slice_to(count));
num_read += count;
self.pos += count;
},
None => (),
};
if num_read == buf.len() || self.closed {
break;
}
self.pos = 0;
self.buf = self.port.recv_opt();
self.closed = self.buf.is_none();
}
if self.closed && num_read == 0 {
io::io_error::cond.raise(io::standard_error(io::EndOfFile));
None
} else {
Some(num_read)
}
}
fn eof(&mut self) -> bool { self.closed }
}
/// Allows writing to a chan.
///
/// # Example
///
/// ```
/// let writer = ChanWriter::new(chan);
/// writer.write("hello, world".as_bytes());
/// ```
pub struct ChanWriter {
chan: Chan<~[u8]>,
}
impl ChanWriter {
pub fn new(chan: Chan<~[u8]>) -> ChanWriter {
ChanWriter { chan: chan }
}
}
impl Writer for ChanWriter {
fn write(&mut self, buf: &[u8]) {
if !self.chan.try_send(buf.to_owned()) {
io::io_error::cond.raise(io::IoError {
kind: io::BrokenPipe,
desc: "Pipe closed",
detail: None
});
}
}
}
#[cfg(test)]
mod test {
use prelude::*;
use super::*;
use io;
use task;
#[test]
fn test_port_reader() {
let (port, chan) = Chan::new();
do task::spawn {
chan.send(~[1u8, 2u8]);
chan.send(~[]);
chan.send(~[3u8, 4u8]);
chan.send(~[5u8, 6u8]);
chan.send(~[7u8, 8u8]);
}
let mut reader = PortReader::new(port);
let mut buf = ~[0u8, ..3];
assert_eq!(false, reader.eof());
assert_eq!(Some(0), reader.read([]));
assert_eq!(false, reader.eof());
assert_eq!(Some(3), reader.read(buf));
assert_eq!(false, reader.eof());
assert_eq!(~[1,2,3], buf);
assert_eq!(Some(3), reader.read(buf));
assert_eq!(false, reader.eof());
assert_eq!(~[4,5,6], buf);
assert_eq!(Some(2), reader.read(buf));
assert_eq!(~[7,8,6], buf);
assert_eq!(true, reader.eof());
let mut err = None;
let result = io::io_error::cond.trap(|io::standard_error(k, _, _)| {
err = Some(k)
}).inside(|| {
reader.read(buf)
});
assert_eq!(Some(io::EndOfFile), err);
assert_eq!(None, result);
assert_eq!(true, reader.eof());
assert_eq!(~[7,8,6], buf);
// Ensure it continues to fail in the same way.
err = None;
let result = io::io_error::cond.trap(|io::standard_error(k, _, _)| {
err = Some(k)
}).inside(|| {
reader.read(buf)
});
assert_eq!(Some(io::EndOfFile), err);
assert_eq!(None, result);
assert_eq!(true, reader.eof());
assert_eq!(~[7,8,6], buf);
}
#[test]
fn test_chan_writer() {
let (port, chan) = Chan::new();
let mut writer = ChanWriter::new(chan);
writer.write_be_u32(42);
let wanted = ~[0u8, 0u8, 0u8, 42u8];
let got = do task::try { port.recv() }.unwrap();
assert_eq!(wanted, got);
let mut err = None;
io::io_error::cond.trap(|io::IoError { kind, .. } | {
err = Some(kind)
}).inside(|| {
writer.write_u8(1)
});
assert_eq!(Some(io::BrokenPipe), err);
}
}
|