Skip to content

Commit

Permalink
auto merge of #5960 : brson/rust/io, r=pcwalton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Apr 19, 2013
2 parents bffe23b + 7270fad commit 6510fd9
Show file tree
Hide file tree
Showing 25 changed files with 1,903 additions and 217 deletions.
59 changes: 59 additions & 0 deletions src/libcore/rt/io/comm_adapters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 super::{Reader, Writer};

struct PortReader<P>;

impl<P: GenericPort<~[u8]>> PortReader<P> {
pub fn new(_port: P) -> PortReader<P> { fail!() }
}

impl<P: GenericPort<~[u8]>> Reader for PortReader<P> {
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }

fn eof(&mut self) -> bool { fail!() }
}

struct ChanWriter<C>;

impl<C: GenericChan<~[u8]>> ChanWriter<C> {
pub fn new(_chan: C) -> ChanWriter<C> { fail!() }
}

impl<C: GenericChan<~[u8]>> Writer for ChanWriter<C> {
pub fn write(&mut self, _buf: &[u8]) { fail!() }

pub fn flush(&mut self) { fail!() }
}

struct ReaderPort<R>;

impl<R: Reader> ReaderPort<R> {
pub fn new(_reader: R) -> ReaderPort<R> { fail!() }
}

impl<R: Reader> GenericPort<~[u8]> for ReaderPort<R> {
fn recv(&self) -> ~[u8] { fail!() }

fn try_recv(&self) -> Option<~[u8]> { fail!() }
}

struct WriterChan<W>;

impl<W: Writer> WriterChan<W> {
pub fn new(_writer: W) -> WriterChan<W> { fail!() }
}

impl<W: Writer> GenericChan<~[u8]> for WriterChan<W> {
fn send(&self, _x: ~[u8]) { fail!() }
}

64 changes: 54 additions & 10 deletions src/libcore/rt/io/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,79 @@
// except according to those terms.

use prelude::*;
use super::Stream;
use super::misc::PathLike;
use super::{Reader, Writer, Seek, Close};
use super::{IoError, SeekStyle};

/// Open a file with the default FileMode and FileAccess
/// # XXX are there sane defaults here?
pub fn open_file<P: PathLike>(_path: &P) -> FileStream { fail!() }

/// # XXX
/// * Ugh, this is ridiculous. What is the best way to represent these options?
enum FileMode {
/// Opens an existing file. IoError if file does not exist.
Open,
/// Creates a file. IoError if file exists.
Create,
/// Opens an existing file or creates a new one.
OpenOrCreate,
/// Opens an existing file or creates a new one, positioned at EOF.
Append,
/// Opens an existing file, truncating it to 0 bytes.
Truncate,
/// Opens an existing file or creates a new one, truncating it to 0 bytes.
CreateOrTruncate,
}

enum FileAccess {
Read,
Write,
ReadWrite
}

pub struct FileStream;

pub impl FileStream {
fn new(_path: Path) -> FileStream {
impl FileStream {
pub fn open<P: PathLike>(_path: &P,
_mode: FileMode,
_access: FileAccess
) -> Result<FileStream, IoError> {
fail!()
}
}

impl Stream for FileStream {
fn read(&mut self, _buf: &mut [u8]) -> uint {
impl Reader for FileStream {
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> {
fail!()
}

fn eof(&mut self) -> bool {
fail!()
}
}

fn write(&mut self, _v: &const [u8]) {
fail!()
}
impl Writer for FileStream {
fn write(&mut self, _v: &[u8]) { fail!() }

fn flush(&mut self) { fail!() }
}

impl Seek for FileStream {
fn tell(&self) -> u64 { fail!() }

fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
}

impl Close for FileStream {
fn close(&mut self) { fail!() }
}

#[test]
#[ignore]
fn super_simple_smoke_test_lets_go_read_some_files_and_have_a_good_time() {
let message = "it's alright. have a good time";
let filename = Path("test.txt");
let mut outstream = FileStream::new(filename);
let filename = &Path("test.txt");
let mut outstream = FileStream::open(filename, Create, Read).unwrap();
outstream.write(message.to_bytes());
}
121 changes: 121 additions & 0 deletions src/libcore/rt/io/flate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 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.

//! Some various other I/O types

// NOTE: These ultimately belong somewhere else

use prelude::*;
use super::*;

/// A Writer decorator that compresses using the 'deflate' scheme
pub struct DeflateWriter<W> {
inner_writer: W
}

impl<W: Writer> DeflateWriter<W> {
pub fn new(inner_writer: W) -> DeflateWriter<W> {
DeflateWriter {
inner_writer: inner_writer
}
}
}

impl<W: Writer> Writer for DeflateWriter<W> {
fn write(&mut self, _buf: &[u8]) { fail!() }

fn flush(&mut self) { fail!() }
}

impl<W: Writer> Decorator<W> for DeflateWriter<W> {
fn inner(self) -> W {
match self {
DeflateWriter { inner_writer: w } => w
}
}

fn inner_ref<'a>(&'a self) -> &'a W {
match *self {
DeflateWriter { inner_writer: ref w } => w
}
}

fn inner_mut_ref<'a>(&'a mut self) -> &'a mut W {
match *self {
DeflateWriter { inner_writer: ref mut w } => w
}
}
}

/// A Reader decorator that decompresses using the 'deflate' scheme
pub struct InflateReader<R> {
inner_reader: R
}

impl<R: Reader> InflateReader<R> {
pub fn new(inner_reader: R) -> InflateReader<R> {
InflateReader {
inner_reader: inner_reader
}
}
}

impl<R: Reader> Reader for InflateReader<R> {
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }

fn eof(&mut self) -> bool { fail!() }
}

impl<R: Reader> Decorator<R> for InflateReader<R> {
fn inner(self) -> R {
match self {
InflateReader { inner_reader: r } => r
}
}

fn inner_ref<'a>(&'a self) -> &'a R {
match *self {
InflateReader { inner_reader: ref r } => r
}
}

fn inner_mut_ref<'a>(&'a mut self) -> &'a mut R {
match *self {
InflateReader { inner_reader: ref mut r } => r
}
}
}

#[cfg(test)]
mod test {
use prelude::*;
use super::*;
use super::super::mem::*;
use super::super::Decorator;

#[test]
#[ignore]
fn smoke_test() {
let mem_writer = MemWriter::new();
let mut deflate_writer = DeflateWriter::new(mem_writer);
let in_msg = "test";
let in_bytes = in_msg.to_bytes();
deflate_writer.write(in_bytes);
deflate_writer.flush();
let buf = deflate_writer.inner().inner();
let mem_reader = MemReader::new(buf);
let mut inflate_reader = InflateReader::new(mem_reader);
let mut out_bytes = [0, .. 100];
let bytes_read = inflate_reader.read(out_bytes).get();
assert!(bytes_read == in_bytes.len());
let out_msg = str::from_bytes(out_bytes);
assert!(in_msg == out_msg);
}
}
Loading

0 comments on commit 6510fd9

Please sign in to comment.