-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
25 changed files
with
1,903 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.