Shift writer_util to a derived impl attached to the writer trait

This commit is contained in:
Graydon Hoare 2012-08-02 17:17:07 -07:00
parent ff00edb7e1
commit 009352101d
3 changed files with 46 additions and 14 deletions

View File

@ -533,7 +533,34 @@ fn u64_from_be_bytes(data: ~[u8], start: uint, size: uint) -> u64 {
return val;
}
impl writer_util for writer {
// FIXME: #3048 combine trait+impl (or just move these to
// default methods on writer)
trait writer_util {
fn write_char(ch: char);
fn write_str(s: &str);
fn write_line(s: &str);
fn write_int(n: int);
fn write_uint(n: uint);
fn write_le_uint(n: uint);
fn write_le_int(n: int);
fn write_be_uint(n: uint);
fn write_be_int(n: int);
fn write_be_u64(n: u64);
fn write_be_u32(n: u32);
fn write_be_u16(n: u16);
fn write_be_i64(n: i64);
fn write_be_i32(n: i32);
fn write_be_i16(n: i16);
fn write_le_u64(n: u64);
fn write_le_u32(n: u32);
fn write_le_u16(n: u16);
fn write_le_i64(n: i64);
fn write_le_i32(n: i32);
fn write_le_i16(n: i16);
fn write_u8(n: u8);
}
impl<T:writer> T : writer_util {
fn write_char(ch: char) {
if ch as uint < 128u {
self.write(&[ch as u8]);
@ -552,17 +579,17 @@ impl writer_util for writer {
fn write_uint(n: uint) {
uint::to_str_bytes(false, n, 10u, |buf| self.write(buf))
}
fn write_le_uint(n: uint, size: uint) {
u64_to_le_bytes(n as u64, size, |v| self.write(v))
fn write_le_uint(n: uint) {
u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
}
fn write_le_int(n: int, size: uint) {
u64_to_le_bytes(n as u64, size, |v| self.write(v))
fn write_le_int(n: int) {
u64_to_le_bytes(n as u64, int::bytes, |v| self.write(v))
}
fn write_be_uint(n: uint, size: uint) {
u64_to_be_bytes(n as u64, size, |v| self.write(v))
fn write_be_uint(n: uint) {
u64_to_be_bytes(n as u64, uint::bytes, |v| self.write(v))
}
fn write_be_int(n: int, size: uint) {
u64_to_be_bytes(n as u64, size, |v| self.write(v))
fn write_be_int(n: int) {
u64_to_be_bytes(n as u64, int::bytes, |v| self.write(v))
}
fn write_be_u64(n: u64) {
u64_to_be_bytes(n, 8u, |v| self.write(v))

View File

@ -411,7 +411,7 @@ impl printer for printer {
{offset: 0, pbreak: broken(inconsistent)}
}
}
fn write_str(s: ~str) {
fn print_str(s: ~str) {
while self.pending_indentation > 0 {
self.out.write_str(~" ");
self.pending_indentation -= 1;
@ -471,7 +471,7 @@ impl printer for printer {
assert (L == len);
// assert L <= space;
self.space -= len;
self.write_str(*s);
self.print_str(*s);
}
EOF {
// EOF should never get here.

View File

@ -955,7 +955,8 @@ fn encode_index<T>(ebml_w: ebml::writer, buckets: ~[@~[entry<T>]],
ebml_w.start_tag(tag_index_buckets_bucket);
for vec::each(*bucket) |elt| {
ebml_w.start_tag(tag_index_buckets_bucket_elt);
writer.write_be_uint(elt.pos, 4u);
assert elt.pos < (u32::max_value as uint);
writer.write_be_u32(elt.pos as u32);
write_fn(writer, elt.val);
ebml_w.end_tag();
}
@ -963,7 +964,10 @@ fn encode_index<T>(ebml_w: ebml::writer, buckets: ~[@~[entry<T>]],
}
ebml_w.end_tag();
ebml_w.start_tag(tag_index_table);
for bucket_locs.each |pos| { writer.write_be_uint(pos, 4u); }
for bucket_locs.each |pos| {
assert pos < (u32::max_value as uint);
writer.write_be_u32(pos as u32);
}
ebml_w.end_tag();
ebml_w.end_tag();
}
@ -971,7 +975,8 @@ fn encode_index<T>(ebml_w: ebml::writer, buckets: ~[@~[entry<T>]],
fn write_str(writer: io::writer, &&s: ~str) { writer.write_str(s); }
fn write_int(writer: io::writer, &&n: int) {
writer.write_be_uint(n as uint, 4u);
assert n < (u32::max_value as int);
writer.write_be_u32(n as u32);
}
fn encode_meta_item(ebml_w: ebml::writer, mi: meta_item) {