about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/_int.rs1
-rw-r--r--src/lib/_str.rs2
-rw-r--r--src/lib/_uint.rs1
-rw-r--r--src/lib/_vec.rs1
-rw-r--r--src/lib/bitv.rs4
-rw-r--r--src/lib/dbg.rs2
-rw-r--r--src/lib/deque.rs4
-rw-r--r--src/lib/io.rs105
-rw-r--r--src/lib/list.rs2
-rw-r--r--src/lib/map.rs6
-rw-r--r--src/lib/sha1.rs3
11 files changed, 65 insertions, 66 deletions
diff --git a/src/lib/_int.rs b/src/lib/_int.rs
index 38e161bcfa2..ee660f013b2 100644
--- a/src/lib/_int.rs
+++ b/src/lib/_int.rs
@@ -1,4 +1,3 @@
-import std.sys;
 
 fn add(int x, int y) -> int { ret x + y; }
 fn sub(int x, int y) -> int { ret x - y; }
diff --git a/src/lib/_str.rs b/src/lib/_str.rs
index 0e0e7650943..3f45334924d 100644
--- a/src/lib/_str.rs
+++ b/src/lib/_str.rs
@@ -1,6 +1,6 @@
 import rustrt.sbuf;
 
-import std._vec.rustrt.vbuf;
+import _vec.rustrt.vbuf;
 
 native "rust" mod rustrt {
     type sbuf;
diff --git a/src/lib/_uint.rs b/src/lib/_uint.rs
index 79b5fa242b3..ceee9c77478 100644
--- a/src/lib/_uint.rs
+++ b/src/lib/_uint.rs
@@ -1,4 +1,3 @@
-import std.sys;
 
 fn add(uint x, uint y) -> uint { ret x + y; }
 fn sub(uint x, uint y) -> uint { ret x - y; }
diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs
index 2ffe8bad08b..680c8884f6d 100644
--- a/src/lib/_vec.rs
+++ b/src/lib/_vec.rs
@@ -1,5 +1,4 @@
 import vbuf = rustrt.vbuf;
-import std.option;
 
 type operator2[T,U,V] = fn(&T, &U) -> V;
 
diff --git a/src/lib/bitv.rs b/src/lib/bitv.rs
index adb3dc86279..2322c693b4d 100644
--- a/src/lib/bitv.rs
+++ b/src/lib/bitv.rs
@@ -1,7 +1,3 @@
-import std._uint;
-import std._int;
-import std._vec;
-
 // FIXME: With recursive object types, we could implement binary methods like
 //        union, intersection, and difference. At that point, we could write
 //        an optimizing version of this module that produces a different obj
diff --git a/src/lib/dbg.rs b/src/lib/dbg.rs
index 774052dcaf5..b63f363e702 100644
--- a/src/lib/dbg.rs
+++ b/src/lib/dbg.rs
@@ -5,8 +5,6 @@
  * logging.
  */
 
-import std._vec;
-
 // FIXME: handle 64-bit case.
 const uint const_refcount = 0x7bad_face_u;
 
diff --git a/src/lib/deque.rs b/src/lib/deque.rs
index 1602345703d..776f82e99fe 100644
--- a/src/lib/deque.rs
+++ b/src/lib/deque.rs
@@ -2,10 +2,6 @@
  * A deque, for fun.  Untested as of yet.  Likely buggy.
  */
 
-import std.option;
-import std._vec;
-import std._int;
-
 type t[T] = obj {
             fn size() -> uint;
 
diff --git a/src/lib/io.rs b/src/lib/io.rs
index 0c4eb39e1aa..34c4a98d9ac 100644
--- a/src/lib/io.rs
+++ b/src/lib/io.rs
@@ -1,7 +1,4 @@
-import std.os.libc;
-import std._str;
-import std._vec;
-
+import os.libc;
 
 type stdio_reader = state obj {
                           fn getc() -> int;
@@ -91,35 +88,29 @@ tag fileflag {
     truncate;
 }
 
-fn writefd(int fd, vec[u8] v) {
-    auto len = _vec.len[u8](v);
-    auto count = 0u;
-    auto vbuf;
-    while (count < len) {
-        vbuf = _vec.buf_off[u8](v, count);
-        auto nout = os.libc.write(fd, vbuf, len);
-        if (nout < 0) {
-            log "error dumping buffer";
-            log sys.rustrt.last_os_error();
-            fail;
+state obj fd_buf_writer(int fd, bool must_close) {
+    fn write(vec[u8] v) {
+        auto len = _vec.len[u8](v);
+        auto count = 0u;
+        auto vbuf;
+        while (count < len) {
+            vbuf = _vec.buf_off[u8](v, count);
+            auto nout = os.libc.write(fd, vbuf, len);
+            if (nout < 0) {
+                log "error dumping buffer";
+                log sys.rustrt.last_os_error();
+                fail;
+            }
+            count += nout as uint;
         }
-        count += nout as uint;
     }
-}
-
-fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
-
-    state obj fd_buf_writer(int fd) {
 
-        fn write(vec[u8] v) {
-            writefd(fd, v);
-        }
-
-        drop {
-            os.libc.close(fd);
-        }
+    drop {
+        if (must_close) {os.libc.close(fd);}
     }
+}
 
+fn file_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
     let int fflags =
         os.libc_constants.O_WRONLY() |
         os.libc_constants.O_BINARY();
@@ -142,26 +133,58 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
         log sys.rustrt.last_os_error();
         fail;
     }
-    ret fd_buf_writer(fd);
+    ret fd_buf_writer(fd, true);
 }
 
 type writer =
     state obj {
-          fn write_str(str s);
-          fn write_int(int n);
-          fn write_uint(uint n);
+          impure fn write_str(str s);
+          impure fn write_int(int n);
+          impure fn write_uint(uint n);
     };
 
-fn file_writer(str path,
-               vec[fileflag] flags)
-    -> writer
-{
-    state obj fw(buf_writer out) {
-        fn write_str(str s)   { out.write(_str.bytes(s)); }
-        fn write_int(int n)   { out.write(_str.bytes(_int.to_str(n, 10u))); }
-        fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
+state obj new_writer(buf_writer out) {
+    impure fn write_str(str s) {
+        out.write(_str.bytes(s));
+    }
+    impure fn write_int(int n) {
+        out.write(_str.bytes(_int.to_str(n, 10u)));
+    }
+    impure fn write_uint(uint n) {
+        out.write(_str.bytes(_uint.to_str(n, 10u)));
+    }
+}
+
+fn file_writer(str path, vec[fileflag] flags) -> writer {
+    ret new_writer(file_buf_writer(path, flags));
+}
+
+// FIXME it would be great if this could be a const named stdout
+fn stdout_writer() -> writer {
+    ret new_writer(fd_buf_writer(1, false));
+}
+
+type str_writer =
+    state obj {
+          fn get_writer() -> writer;
+          fn get_str() -> str;
+    };
+
+type str_buf = @rec(mutable str buf);
+
+// TODO awkward! it's not possible to implement a writer with an extra method
+fn string_writer() -> str_writer {
+    auto buf = @rec(mutable buf = "");
+    state obj str_writer_writer(str_buf buf) {
+        impure fn write_str(str s)   { buf.buf += s; }
+        impure fn write_int(int n)   { buf.buf += _int.to_str(n, 10u); }
+        impure fn write_uint(uint n) { buf.buf += _uint.to_str(n, 10u); }
+    }
+    state obj str_writer_wrap(writer wr, str_buf buf) {
+        fn get_writer() -> writer {ret wr;}
+        fn get_str() -> str {ret buf.buf;}
     }
-    ret fw(new_buf_writer(path, flags));
+    ret str_writer_wrap(str_writer_writer(buf), buf);
 }
 
 //
diff --git a/src/lib/list.rs b/src/lib/list.rs
index c4661940a5f..58c2cded6e6 100644
--- a/src/lib/list.rs
+++ b/src/lib/list.rs
@@ -1,5 +1,3 @@
-
-import std.option;
 import option.some;
 import option.none;
 
diff --git a/src/lib/map.rs b/src/lib/map.rs
index 7f760b65967..29e9ba5d691 100644
--- a/src/lib/map.rs
+++ b/src/lib/map.rs
@@ -3,12 +3,6 @@
  * use, but useful as a stress test for rustboot.
  */
 
-import std._int;
-import std.sys;
-import std.option;
-import std._vec;
-
-
 type hashfn[K] = fn(&K) -> uint;
 type eqfn[K] = fn(&K, &K) -> bool;
 
diff --git a/src/lib/sha1.rs b/src/lib/sha1.rs
index 2a6b74d4ff3..a57ea894cdf 100644
--- a/src/lib/sha1.rs
+++ b/src/lib/sha1.rs
@@ -4,9 +4,6 @@
  * point this will want to be rewritten.
  */
 
-import std._vec;
-import std._str;
-
 export sha1;
 export mk_sha1;