1
Fork 0

Make with_bytes_reader/with_bytes_writer pure

This commit is contained in:
Erick Tryzelaar 2012-10-18 09:04:47 -07:00
parent 4e03ffdb65
commit b18a15171b
2 changed files with 12 additions and 8 deletions

View file

@ -56,7 +56,7 @@ pub enum DVec<A> {
} }
/// Creates a new, empty dvec /// Creates a new, empty dvec
pub fn DVec<A>() -> DVec<A> { pub pure fn DVec<A>() -> DVec<A> {
DVec_({mut data: ~[]}) DVec_({mut data: ~[]})
} }

View file

@ -328,7 +328,7 @@ impl ByteBuf: Reader {
fn tell() -> uint { self.pos } fn tell() -> uint { self.pos }
} }
pub fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t { pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
f({buf: bytes, mut pos: 0u} as Reader) f({buf: bytes, mut pos: 0u} as Reader)
} }
@ -730,21 +730,25 @@ impl @BytesWriter : Writer {
fn get_type() -> WriterType { (*self).get_type() } fn get_type() -> WriterType { (*self).get_type() }
} }
pub fn BytesWriter() -> BytesWriter { pub pure fn BytesWriter() -> BytesWriter {
BytesWriter { buf: DVec(), mut pos: 0u } BytesWriter { buf: DVec(), mut pos: 0u }
} }
pub fn with_bytes_writer(f: fn(Writer)) -> ~[u8] { pub pure fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
let wr = @BytesWriter(); let wr = @BytesWriter();
f(wr as Writer); f(wr as Writer);
wr.buf.check_out(|buf| move buf) // FIXME (#3758): This should not be needed.
unsafe { wr.buf.check_out(|buf| move buf) }
} }
pub fn with_str_writer(f: fn(Writer)) -> ~str { pub pure fn with_str_writer(f: fn(Writer)) -> ~str {
let mut v = with_bytes_writer(f); let mut v = with_bytes_writer(f);
// Make sure the vector has a trailing null and is proper utf8. // FIXME (#3758): This should not be needed.
v.push(0); unsafe {
// Make sure the vector has a trailing null and is proper utf8.
v.push(0);
}
assert str::is_utf8(v); assert str::is_utf8(v);
unsafe { move ::cast::transmute(move v) } unsafe { move ::cast::transmute(move v) }