diff options
| author | Roy Frostig <[email protected]> | 2010-08-06 15:48:23 -0700 |
|---|---|---|
| committer | Roy Frostig <[email protected]> | 2010-08-06 15:48:23 -0700 |
| commit | 80a1cd3d1e5e39db00a68ad6c1dc5686b775a4ad (patch) | |
| tree | c7da38fa419c3eaa17d10750fedb7da2e40e1aa4 /src/lib/_io.rs | |
| parent | Accept uint literals as literal patterns. (diff) | |
| download | rust-80a1cd3d1e5e39db00a68ad6c1dc5686b775a4ad.tar.xz rust-80a1cd3d1e5e39db00a68ad6c1dc5686b775a4ad.zip | |
Redo yesterday's buf_writer-wrapper in a less silly and convoluted way. Add integer stringifying functions to _int module.
Diffstat (limited to 'src/lib/_io.rs')
| -rw-r--r-- | src/lib/_io.rs | 58 |
1 files changed, 15 insertions, 43 deletions
diff --git a/src/lib/_io.rs b/src/lib/_io.rs index b0b0c313..dbd60e63 100644 --- a/src/lib/_io.rs +++ b/src/lib/_io.rs @@ -112,49 +112,21 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer { ret fd_buf_writer(fd); } -type formatter[T] = fn(&T x) -> vec[u8]; - -type writer[T] = unsafe obj { fn write(&T x); }; - -fn mk_writer[T](str path, - vec[fileflag] flags, - &formatter[T] fmt) - -> writer[T] -{ - unsafe obj w[T](buf_writer out, formatter[T] fmt) { - fn write(&T x) { - out.write(fmt(x)); - } - } - ret w[T](new_buf_writer(path, flags), fmt); -} - -/* TODO: int_writer, uint_writer, ... */ - -fn str_writer(str path, vec[fileflag] flags) -> writer[str] { - auto fmt = _str.bytes; // FIXME (issue #90) - ret mk_writer[str](path, flags, fmt); -} - -fn vec_writer[T](str path, - vec[fileflag] flags, - &formatter[T] inner) - -> writer[vec[T]] +type writer = + unsafe obj { + fn write_str(str s); + fn write_int(int n); + fn write_uint(uint n); + }; + +fn file_writer(str path, + vec[fileflag] flags) + -> writer { - fn fmt[T](&vec[T] v, &formatter[T] inner) -> vec[u8] { - let vec[u8] res = _str.bytes("vec("); - auto first = true; - for (T x in v) { - if (!first) { - res += _str.bytes(", "); - } else { - first = false; - } - res += inner(x); - } - res += _str.bytes(")\n"); - ret res; + unsafe 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_string(n, 10u))); } + fn write_uint(uint n) { out.write(_str.bytes(_int.uto_string(n, 10u))); } } - - ret mk_writer[vec[T]](path, flags, bind fmt[T](_, inner)); + ret fw(new_buf_writer(path, flags)); } |