Skip to content

Commit

Permalink
auto merge of #6046 : brson/rust/io, r=graydon
Browse files Browse the repository at this point in the history
r? @pcwalton

Sorry this is so big, and sorry the first commit is just titled 'wip'.

Some interesting bits

* [LocalServices](brson@f9069ba) - This is the set of runtime capabilities that *all* Rust code should expect access to, including the local heap, GC, logging, unwinding.
* [impl Reader, etc. for Option](brson@5fbb094) - Constructors like `File::open` return Option<FileStream>. This lets you write I/O code without ever unwrapping an option.

This series adds a lot of [documentation](https://github.com/brson/rust/blob/io/src/libcore/rt/io/mod.rs#L11) to `core::rt::io`.
  • Loading branch information
bors committed May 3, 2013
2 parents d9c7d0b + 6c478c7 commit 79aeb52
Show file tree
Hide file tree
Showing 42 changed files with 1,399 additions and 347 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ $(foreach target,$(CFG_TARGET_TRIPLES),\

CORELIB_CRATE := $(S)src/libcore/core.rc
CORELIB_INPUTS := $(wildcard $(addprefix $(S)src/libcore/, \
core.rc *.rs */*.rs */*/*rs))
core.rc *.rs */*.rs */*/*rs */*/*/*rs))

######################################################################
# Standard library variables
Expand Down
74 changes: 35 additions & 39 deletions mk/docs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ DOCS :=


######################################################################
# Pandoc (reference-manual related)
# Docs, from pandoc, rustdoc (which runs pandoc), and node
######################################################################
ifeq ($(CFG_PANDOC),)
$(info cfg: no pandoc found, omitting doc/rust.pdf)
else

ifeq ($(CFG_NODE),)
$(info cfg: no node found, omitting doc/tutorial.html)
else

doc/rust.css: rust.css
@$(call E, cp: $@)
Expand All @@ -34,6 +27,18 @@ doc/manual.css: manual.css
@$(call E, cp: $@)
$(Q)cp -a $< $@ 2> /dev/null

ifeq ($(CFG_PANDOC),)
$(info cfg: no pandoc found, omitting docs)
NO_DOCS = 1
endif

ifeq ($(CFG_NODE),)
$(info cfg: no node found, omitting docs)
NO_DOCS = 1
endif

ifneq ($(NO_DOCS),1)

DOCS += doc/rust.html
doc/rust.html: rust.md doc/version_info.html doc/rust.css doc/manual.css
@$(call E, pandoc: $@)
Expand All @@ -47,19 +52,8 @@ doc/rust.html: rust.md doc/version_info.html doc/rust.css doc/manual.css
--css=manual.css \
--include-before-body=doc/version_info.html \
--output=$@
endif

ifeq ($(CFG_PDFLATEX),)
$(info cfg: no pdflatex found, omitting doc/rust.pdf)
else
ifeq ($(CFG_XETEX),)
$(info cfg: no xetex found, disabling doc/rust.pdf)
else
ifeq ($(CFG_LUATEX),)
$(info cfg: lacking luatex, disabling pdflatex)
else

DOCS += doc/rust.pdf
DOCS += doc/rust.tex
doc/rust.tex: rust.md doc/version.md
@$(call E, pandoc: $@)
$(Q)$(CFG_NODE) $(S)doc/prep.js $< | \
Expand All @@ -70,17 +64,6 @@ doc/rust.tex: rust.md doc/version.md
--from=markdown --to=latex \
--output=$@

doc/rust.pdf: doc/rust.tex
@$(call E, pdflatex: $@)
$(Q)$(CFG_PDFLATEX) \
-interaction=batchmode \
-output-directory=doc \
$<

endif
endif
endif

DOCS += doc/rustpkg.html
doc/rustpkg.html: rustpkg.md doc/version_info.html doc/rust.css doc/manual.css
@$(call E, pandoc: $@)
Expand All @@ -95,13 +78,6 @@ doc/rustpkg.html: rustpkg.md doc/version_info.html doc/rust.css doc/manual.css
--include-before-body=doc/version_info.html \
--output=$@

######################################################################
# Node (tutorial related)
######################################################################
ifeq ($(CFG_NODE),)
$(info cfg: no node found, omitting doc/tutorial.html)
else

DOCS += doc/tutorial.html
doc/tutorial.html: tutorial.md doc/version_info.html doc/rust.css
@$(call E, pandoc: $@)
Expand Down Expand Up @@ -153,9 +129,29 @@ doc/tutorial-tasks.html: tutorial-tasks.md doc/version_info.html doc/rust.css
--include-before-body=doc/version_info.html \
--output=$@

ifeq ($(CFG_PDFLATEX),)
$(info cfg: no pdflatex found, omitting doc/rust.pdf)
else
ifeq ($(CFG_XETEX),)
$(info cfg: no xetex found, disabling doc/rust.pdf)
else
ifeq ($(CFG_LUATEX),)
$(info cfg: lacking luatex, disabling pdflatex)
else

DOCS += doc/rust.pdf
doc/rust.pdf: doc/rust.tex
@$(call E, pdflatex: $@)
$(Q)$(CFG_PDFLATEX) \
-interaction=batchmode \
-output-directory=doc \
$<

endif
endif
endif
endif

endif # No pandoc / node

######################################################################
# LLnextgen (grammar analysis from refman)
Expand Down
23 changes: 23 additions & 0 deletions src/libcore/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,27 @@ mod test {

assert!(trapped);
}

// Issue #6009
mod m {
condition! {
sadness: int -> int;
}

mod n {
use super::sadness;

#[test]
fn test_conditions_are_public() {
let mut trapped = false;
do sadness::cond.trap(|_| {
trapped = true;
0
}).in {
sadness::cond.raise(0);
}
assert!(trapped);
}
}
}
}
3 changes: 3 additions & 0 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ pub mod linkhack {
}
}

// Internal macros
mod macros;

/* The Prelude. */

pub mod prelude;
Expand Down
39 changes: 39 additions & 0 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2012 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.

#[macro_escape];

// Some basic logging
macro_rules! rtdebug_ (
($( $arg:expr),+) => ( {
dumb_println(fmt!( $($arg),+ ));

fn dumb_println(s: &str) {
use io::WriterUtil;
let dbg = ::libc::STDERR_FILENO as ::io::fd_t;
dbg.write_str(s);
dbg.write_str("\n");
}

} )
)

// An alternate version with no output, for turning off logging
macro_rules! rtdebug (
($( $arg:expr),+) => ( $(let _ = $arg)*; )
)

macro_rules! abort(
($( $msg:expr),+) => ( {
rtdebug!($($msg),+);

unsafe { ::libc::abort(); }
} )
)
File renamed without changes.
10 changes: 3 additions & 7 deletions src/libcore/rt/io/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
// except according to those terms.

use prelude::*;
use super::misc::PathLike;
use super::support::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!() }
use super::SeekStyle;

/// # XXX
/// * Ugh, this is ridiculous. What is the best way to represent these options?
Expand Down Expand Up @@ -46,7 +42,7 @@ impl FileStream {
pub fn open<P: PathLike>(_path: &P,
_mode: FileMode,
_access: FileAccess
) -> Result<FileStream, IoError> {
) -> Option<FileStream> {
fail!()
}
}
Expand Down
69 changes: 62 additions & 7 deletions src/libcore/rt/io/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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

use cmp::min;

/// Writes to an owned, growable byte vector
pub struct MemWriter {
Expand All @@ -29,13 +29,15 @@ impl MemWriter {
}

impl Writer for MemWriter {
fn write(&mut self, _buf: &[u8]) { fail!() }
fn write(&mut self, buf: &[u8]) {
self.buf.push_all(buf)
}

fn flush(&mut self) { /* no-op */ }
}

impl Seek for MemWriter {
fn tell(&self) -> u64 { fail!() }
fn tell(&self) -> u64 { self.buf.len() as u64 }

fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
}
Expand Down Expand Up @@ -77,13 +79,27 @@ impl MemReader {
}

impl Reader for MemReader {
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
{ if self.eof() { return None; } }

let write_len = min(buf.len(), self.buf.len() - self.pos);
{
let input = self.buf.slice(self.pos, self.pos + write_len);
let output = vec::mut_slice(buf, 0, write_len);
assert!(input.len() == output.len());
vec::bytes::copy_memory(output, input, write_len);
}
self.pos += write_len;
assert!(self.pos <= self.buf.len());

fn eof(&mut self) -> bool { fail!() }
return Some(write_len);
}

fn eof(&mut self) -> bool { self.pos == self.buf.len() }
}

impl Seek for MemReader {
fn tell(&self) -> u64 { fail!() }
fn tell(&self) -> u64 { self.pos as u64 }

fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
}
Expand Down Expand Up @@ -163,4 +179,43 @@ impl<'self> Seek for BufReader<'self> {
fn tell(&self) -> u64 { fail!() }

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

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

#[test]
fn test_mem_writer() {
let mut writer = MemWriter::new();
assert!(writer.tell() == 0);
writer.write([0]);
assert!(writer.tell() == 1);
writer.write([1, 2, 3]);
writer.write([4, 5, 6, 7]);
assert!(writer.tell() == 8);
assert!(writer.inner() == ~[0, 1, 2, 3, 4, 5 , 6, 7]);
}

#[test]
fn test_mem_reader() {
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
let mut buf = [];
assert!(reader.read(buf) == Some(0));
assert!(reader.tell() == 0);
let mut buf = [0];
assert!(reader.read(buf) == Some(1));
assert!(reader.tell() == 1);
assert!(buf == [0]);
let mut buf = [0, ..4];
assert!(reader.read(buf) == Some(4));
assert!(reader.tell() == 5);
assert!(buf == [1, 2, 3, 4]);
assert!(reader.read(buf) == Some(3));
assert!(buf.slice(0, 3) == [5, 6, 7]);
assert!(reader.eof());
assert!(reader.read(buf) == None);
assert!(reader.eof());
}
}
Loading

0 comments on commit 79aeb52

Please sign in to comment.