Skip to content

Commit 6510fd9

Browse files
committed
auto merge of #5960 : brson/rust/io, r=pcwalton
r? This pull request is a grab bag of work on the new scheduler. The most important commit here is where I [outline](https://github.com/brson/rust/blob/io/src/libcore/rt/io/mod.rs) a fairly complete I/O API, based on `Reader` and `Writer` types, as in the current `core::io` module. I've organized this version into a number of modules with declarations for Files, TCP, UDP, Unix sockets, blocking/non-blocking implementations, memory buffers, compression adapters. I'm trying to get this into shape to present on the mailing list. This branch also wires up `spawn` to the new scheduler, and simplifies the core scheduler operations.
2 parents bffe23b + 7270fad commit 6510fd9

25 files changed

+1903
-217
lines changed

src/libcore/rt/io/comm_adapters.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 prelude::*;
12+
use super::{Reader, Writer};
13+
14+
struct PortReader<P>;
15+
16+
impl<P: GenericPort<~[u8]>> PortReader<P> {
17+
pub fn new(_port: P) -> PortReader<P> { fail!() }
18+
}
19+
20+
impl<P: GenericPort<~[u8]>> Reader for PortReader<P> {
21+
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }
22+
23+
fn eof(&mut self) -> bool { fail!() }
24+
}
25+
26+
struct ChanWriter<C>;
27+
28+
impl<C: GenericChan<~[u8]>> ChanWriter<C> {
29+
pub fn new(_chan: C) -> ChanWriter<C> { fail!() }
30+
}
31+
32+
impl<C: GenericChan<~[u8]>> Writer for ChanWriter<C> {
33+
pub fn write(&mut self, _buf: &[u8]) { fail!() }
34+
35+
pub fn flush(&mut self) { fail!() }
36+
}
37+
38+
struct ReaderPort<R>;
39+
40+
impl<R: Reader> ReaderPort<R> {
41+
pub fn new(_reader: R) -> ReaderPort<R> { fail!() }
42+
}
43+
44+
impl<R: Reader> GenericPort<~[u8]> for ReaderPort<R> {
45+
fn recv(&self) -> ~[u8] { fail!() }
46+
47+
fn try_recv(&self) -> Option<~[u8]> { fail!() }
48+
}
49+
50+
struct WriterChan<W>;
51+
52+
impl<W: Writer> WriterChan<W> {
53+
pub fn new(_writer: W) -> WriterChan<W> { fail!() }
54+
}
55+
56+
impl<W: Writer> GenericChan<~[u8]> for WriterChan<W> {
57+
fn send(&self, _x: ~[u8]) { fail!() }
58+
}
59+

src/libcore/rt/io/file.rs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,79 @@
99
// except according to those terms.
1010

1111
use prelude::*;
12-
use super::Stream;
12+
use super::misc::PathLike;
13+
use super::{Reader, Writer, Seek, Close};
14+
use super::{IoError, SeekStyle};
15+
16+
/// Open a file with the default FileMode and FileAccess
17+
/// # XXX are there sane defaults here?
18+
pub fn open_file<P: PathLike>(_path: &P) -> FileStream { fail!() }
19+
20+
/// # XXX
21+
/// * Ugh, this is ridiculous. What is the best way to represent these options?
22+
enum FileMode {
23+
/// Opens an existing file. IoError if file does not exist.
24+
Open,
25+
/// Creates a file. IoError if file exists.
26+
Create,
27+
/// Opens an existing file or creates a new one.
28+
OpenOrCreate,
29+
/// Opens an existing file or creates a new one, positioned at EOF.
30+
Append,
31+
/// Opens an existing file, truncating it to 0 bytes.
32+
Truncate,
33+
/// Opens an existing file or creates a new one, truncating it to 0 bytes.
34+
CreateOrTruncate,
35+
}
36+
37+
enum FileAccess {
38+
Read,
39+
Write,
40+
ReadWrite
41+
}
1342

1443
pub struct FileStream;
1544

16-
pub impl FileStream {
17-
fn new(_path: Path) -> FileStream {
45+
impl FileStream {
46+
pub fn open<P: PathLike>(_path: &P,
47+
_mode: FileMode,
48+
_access: FileAccess
49+
) -> Result<FileStream, IoError> {
1850
fail!()
1951
}
2052
}
2153

22-
impl Stream for FileStream {
23-
fn read(&mut self, _buf: &mut [u8]) -> uint {
54+
impl Reader for FileStream {
55+
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> {
2456
fail!()
2557
}
2658

2759
fn eof(&mut self) -> bool {
2860
fail!()
2961
}
62+
}
3063

31-
fn write(&mut self, _v: &const [u8]) {
32-
fail!()
33-
}
64+
impl Writer for FileStream {
65+
fn write(&mut self, _v: &[u8]) { fail!() }
66+
67+
fn flush(&mut self) { fail!() }
68+
}
69+
70+
impl Seek for FileStream {
71+
fn tell(&self) -> u64 { fail!() }
72+
73+
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
74+
}
75+
76+
impl Close for FileStream {
77+
fn close(&mut self) { fail!() }
3478
}
3579

3680
#[test]
3781
#[ignore]
3882
fn super_simple_smoke_test_lets_go_read_some_files_and_have_a_good_time() {
3983
let message = "it's alright. have a good time";
40-
let filename = Path("test.txt");
41-
let mut outstream = FileStream::new(filename);
84+
let filename = &Path("test.txt");
85+
let mut outstream = FileStream::open(filename, Create, Read).unwrap();
4286
outstream.write(message.to_bytes());
4387
}

src/libcore/rt/io/flate.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
//! Some various other I/O types
12+
13+
// NOTE: These ultimately belong somewhere else
14+
15+
use prelude::*;
16+
use super::*;
17+
18+
/// A Writer decorator that compresses using the 'deflate' scheme
19+
pub struct DeflateWriter<W> {
20+
inner_writer: W
21+
}
22+
23+
impl<W: Writer> DeflateWriter<W> {
24+
pub fn new(inner_writer: W) -> DeflateWriter<W> {
25+
DeflateWriter {
26+
inner_writer: inner_writer
27+
}
28+
}
29+
}
30+
31+
impl<W: Writer> Writer for DeflateWriter<W> {
32+
fn write(&mut self, _buf: &[u8]) { fail!() }
33+
34+
fn flush(&mut self) { fail!() }
35+
}
36+
37+
impl<W: Writer> Decorator<W> for DeflateWriter<W> {
38+
fn inner(self) -> W {
39+
match self {
40+
DeflateWriter { inner_writer: w } => w
41+
}
42+
}
43+
44+
fn inner_ref<'a>(&'a self) -> &'a W {
45+
match *self {
46+
DeflateWriter { inner_writer: ref w } => w
47+
}
48+
}
49+
50+
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut W {
51+
match *self {
52+
DeflateWriter { inner_writer: ref mut w } => w
53+
}
54+
}
55+
}
56+
57+
/// A Reader decorator that decompresses using the 'deflate' scheme
58+
pub struct InflateReader<R> {
59+
inner_reader: R
60+
}
61+
62+
impl<R: Reader> InflateReader<R> {
63+
pub fn new(inner_reader: R) -> InflateReader<R> {
64+
InflateReader {
65+
inner_reader: inner_reader
66+
}
67+
}
68+
}
69+
70+
impl<R: Reader> Reader for InflateReader<R> {
71+
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }
72+
73+
fn eof(&mut self) -> bool { fail!() }
74+
}
75+
76+
impl<R: Reader> Decorator<R> for InflateReader<R> {
77+
fn inner(self) -> R {
78+
match self {
79+
InflateReader { inner_reader: r } => r
80+
}
81+
}
82+
83+
fn inner_ref<'a>(&'a self) -> &'a R {
84+
match *self {
85+
InflateReader { inner_reader: ref r } => r
86+
}
87+
}
88+
89+
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut R {
90+
match *self {
91+
InflateReader { inner_reader: ref mut r } => r
92+
}
93+
}
94+
}
95+
96+
#[cfg(test)]
97+
mod test {
98+
use prelude::*;
99+
use super::*;
100+
use super::super::mem::*;
101+
use super::super::Decorator;
102+
103+
#[test]
104+
#[ignore]
105+
fn smoke_test() {
106+
let mem_writer = MemWriter::new();
107+
let mut deflate_writer = DeflateWriter::new(mem_writer);
108+
let in_msg = "test";
109+
let in_bytes = in_msg.to_bytes();
110+
deflate_writer.write(in_bytes);
111+
deflate_writer.flush();
112+
let buf = deflate_writer.inner().inner();
113+
let mem_reader = MemReader::new(buf);
114+
let mut inflate_reader = InflateReader::new(mem_reader);
115+
let mut out_bytes = [0, .. 100];
116+
let bytes_read = inflate_reader.read(out_bytes).get();
117+
assert!(bytes_read == in_bytes.len());
118+
let out_msg = str::from_bytes(out_bytes);
119+
assert!(in_msg == out_msg);
120+
}
121+
}

0 commit comments

Comments
 (0)