De-~[] Mem{Reader,Writer}

This commit is contained in:
Steven Fackler 2014-03-26 22:46:25 -07:00
parent d0e60b72ee
commit 49a8081095
21 changed files with 66 additions and 65 deletions

View File

@ -96,7 +96,7 @@ Use the [`lines`](http://static.rust-lang.org/doc/master/std/io/trait.Buffer.htm
use std::io::BufferedReader;
# use std::io::MemReader;
# let reader = MemReader::new(~[]);
# let reader = MemReader::new(vec!());
let mut reader = BufferedReader::new(reader);
for line in reader.lines() {

View File

@ -23,7 +23,7 @@ use Rng;
/// use rand::{reader, Rng};
/// use std::io::MemReader;
///
/// let mut rng = reader::ReaderRng::new(MemReader::new(~[1,2,3,4,5,6,7,8]));
/// let mut rng = reader::ReaderRng::new(MemReader::new(vec!(1,2,3,4,5,6,7,8)));
/// println!("{:x}", rng.gen::<uint>());
/// ```
pub struct ReaderRng<R> {
@ -80,7 +80,7 @@ mod test {
// transmute from the target to avoid endianness concerns.
let v = ~[1u64, 2u64, 3u64];
let bytes: ~[u8] = unsafe {cast::transmute(v)};
let mut rng = ReaderRng::new(MemReader::new(bytes));
let mut rng = ReaderRng::new(MemReader::new(bytes.move_iter().collect()));
assert_eq!(rng.next_u64(), 1);
assert_eq!(rng.next_u64(), 2);
@ -91,7 +91,7 @@ mod test {
// transmute from the target to avoid endianness concerns.
let v = ~[1u32, 2u32, 3u32];
let bytes: ~[u8] = unsafe {cast::transmute(v)};
let mut rng = ReaderRng::new(MemReader::new(bytes));
let mut rng = ReaderRng::new(MemReader::new(bytes.move_iter().collect()));
assert_eq!(rng.next_u32(), 1);
assert_eq!(rng.next_u32(), 2);
@ -102,7 +102,7 @@ mod test {
let v = [1u8, 2, 3, 4, 5, 6, 7, 8];
let mut w = [0u8, .. 8];
let mut rng = ReaderRng::new(MemReader::new(v.to_owned()));
let mut rng = ReaderRng::new(MemReader::new(Vec::from_slice(v)));
rng.fill_bytes(w);
assert!(v == w);
@ -111,7 +111,7 @@ mod test {
#[test]
#[should_fail]
fn test_reader_rng_insufficient_bytes() {
let mut rng = ReaderRng::new(MemReader::new(~[]));
let mut rng = ReaderRng::new(MemReader::new(vec!()));
let mut v = [0u8, .. 3];
rng.fill_bytes(v);
}

View File

@ -681,7 +681,7 @@ pub fn pretty_print_input(sess: Session,
};
let src_name = source_name(input);
let src = sess.codemap().get_filemap(src_name).src.as_bytes().to_owned();
let src = Vec::from_slice(sess.codemap().get_filemap(src_name).src.as_bytes());
let mut rdr = MemReader::new(src);
match ppm {

View File

@ -741,7 +741,7 @@ impl<'a> Liveness<'a> {
self.write_vars(wr, ln, |idx| self.users.get(idx).writer);
write!(wr, " precedes {}]", self.successors.get(ln.get()).to_str());
}
str::from_utf8_owned(wr.unwrap()).unwrap()
str::from_utf8(wr.unwrap().as_slice()).unwrap().to_owned()
}
fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {

View File

@ -34,7 +34,7 @@ pub fn highlight(src: &str, class: Option<&str>) -> ~str {
lexer::new_string_reader(&sess.span_diagnostic, fm),
class,
&mut out).unwrap();
str::from_utf8_lossy(out.unwrap()).into_owned()
str::from_utf8_lossy(out.unwrap().as_slice()).into_owned()
}
/// Exhausts the `lexer` writing the output into `out`.

View File

@ -312,7 +312,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
}
try!(write!(&mut w, "\\};"));
str::from_utf8_owned(w.unwrap()).unwrap()
str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned()
};
// Write out the shared files. Note that these are shared among all rustdoc

View File

@ -388,7 +388,7 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
let mut encoder = json::Encoder::new(&mut w as &mut io::Writer);
krate.encode(&mut encoder).unwrap();
}
str::from_utf8_owned(w.unwrap()).unwrap()
str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned()
};
let crate_json = match json::from_str(crate_json_str) {
Ok(j) => j,

View File

@ -309,7 +309,7 @@ impl<'a> Encoder<'a> {
}
/// Encode the specified struct into a json [u8]
pub fn buffer_encode<T:Encodable<Encoder<'a>, io::IoError>>(to_encode_object: &T) -> ~[u8] {
pub fn buffer_encode<T:Encodable<Encoder<'a>, io::IoError>>(to_encode_object: &T) -> Vec<u8> {
//Serialize the object in a string using a writer
let mut m = MemWriter::new();
{
@ -322,8 +322,8 @@ impl<'a> Encoder<'a> {
/// Encode the specified struct into a json str
pub fn str_encode<T:Encodable<Encoder<'a>, io::IoError>>(to_encode_object: &T) -> ~str {
let buff:~[u8] = Encoder::buffer_encode(to_encode_object);
str::from_utf8_owned(buff).unwrap()
let buff = Encoder::buffer_encode(to_encode_object);
str::from_utf8(buff.as_slice()).unwrap().to_owned()
}
}
@ -484,7 +484,7 @@ impl<'a> ::Encoder<io::IoError> for Encoder<'a> {
let mut check_encoder = Encoder::new(&mut buf);
try!(f(&mut check_encoder));
let buf = buf.unwrap();
let out = from_utf8(buf).unwrap();
let out = from_utf8(buf.as_slice()).unwrap();
let needs_wrapping = out.char_at(0) != '"' &&
out.char_at_reverse(out.len()) != '"';
if needs_wrapping { try!(write!(self.wr, "\"")); }
@ -715,7 +715,7 @@ impl<'a> ::Encoder<io::IoError> for PrettyEncoder<'a> {
let mut check_encoder = PrettyEncoder::new(&mut buf);
try!(f(&mut check_encoder));
let buf = buf.unwrap();
let out = from_utf8(buf).unwrap();
let out = from_utf8(buf.as_slice()).unwrap();
let needs_wrapping = out.char_at(0) != '"' &&
out.char_at_reverse(out.len()) != '"';
if needs_wrapping { try!(write!(self.wr, "\"")); }
@ -763,7 +763,7 @@ impl Json {
pub fn to_pretty_str(&self) -> ~str {
let mut s = MemWriter::new();
self.to_pretty_writer(&mut s as &mut io::Writer).unwrap();
str::from_utf8_owned(s.unwrap()).unwrap()
str::from_utf8(s.unwrap().as_slice()).unwrap().to_owned()
}
/// If the Json value is an Object, returns the value associated with the provided key.
@ -1927,7 +1927,7 @@ mod tests {
let mut m = MemWriter::new();
f(&mut m as &mut io::Writer);
str::from_utf8_owned(m.unwrap()).unwrap()
str::from_utf8(m.unwrap().as_slice()).unwrap().to_owned()
}
#[test]
@ -2528,7 +2528,7 @@ mod tests {
hm.encode(&mut encoder).unwrap();
}
let bytes = mem_buf.unwrap();
let json_str = from_utf8(bytes).unwrap();
let json_str = from_utf8(bytes.as_slice()).unwrap();
match from_str(json_str) {
Err(_) => fail!("Unable to parse json_str: {:?}", json_str),
_ => {} // it parsed and we are good to go
@ -2548,7 +2548,7 @@ mod tests {
hm.encode(&mut encoder).unwrap();
}
let bytes = mem_buf.unwrap();
let json_str = from_utf8(bytes).unwrap();
let json_str = from_utf8(bytes.as_slice()).unwrap();
match from_str(json_str) {
Err(_) => fail!("Unable to parse json_str: {:?}", json_str),
_ => {} // it parsed and we are good to go

View File

@ -490,7 +490,7 @@ use repr;
use result::{Ok, Err};
use str::StrSlice;
use str;
use slice::ImmutableVector;
use slice::{Vector, ImmutableVector};
use slice;
pub use self::num::radix;
@ -795,7 +795,7 @@ pub fn format(args: &Arguments) -> ~str {
pub unsafe fn format_unsafe(fmt: &[rt::Piece], args: &[Argument]) -> ~str {
let mut output = MemWriter::new();
write_unsafe(&mut output as &mut io::Writer, fmt, args).unwrap();
return str::from_utf8_owned(output.unwrap()).unwrap();
return str::from_utf8(output.unwrap().as_slice()).unwrap().to_owned();
}
impl<'a> Formatter<'a> {

View File

@ -405,7 +405,7 @@ mod test {
#[test]
fn test_buffered_reader() {
let inner = MemReader::new(~[0, 1, 2, 3, 4]);
let inner = MemReader::new(vec!(0, 1, 2, 3, 4));
let mut reader = BufferedReader::with_capacity(2, inner);
let mut buf = [0, 0, 0];
@ -502,7 +502,7 @@ mod test {
#[test]
fn test_read_until() {
let inner = MemReader::new(~[0, 1, 2, 1, 0]);
let inner = MemReader::new(vec!(0, 1, 2, 1, 0));
let mut reader = BufferedReader::with_capacity(2, inner);
assert_eq!(reader.read_until(0), Ok(vec!(0)));
assert_eq!(reader.read_until(2), Ok(vec!(1, 2)));
@ -533,7 +533,7 @@ mod test {
#[test]
fn test_read_line() {
let in_buf = MemReader::new(bytes!("a\nb\nc").to_owned());
let in_buf = MemReader::new(Vec::from_slice(bytes!("a\nb\nc")));
let mut reader = BufferedReader::with_capacity(2, in_buf);
assert_eq!(reader.read_line(), Ok(~"a\n"));
assert_eq!(reader.read_line(), Ok(~"b\n"));
@ -543,7 +543,7 @@ mod test {
#[test]
fn test_lines() {
let in_buf = MemReader::new(bytes!("a\nb\nc").to_owned());
let in_buf = MemReader::new(Vec::from_slice(bytes!("a\nb\nc")));
let mut reader = BufferedReader::with_capacity(2, in_buf);
let mut it = reader.lines();
assert_eq!(it.next(), Some(Ok(~"a\n")));

View File

@ -267,7 +267,7 @@ mod test {
#[test]
fn read_byte() {
let mut reader = MemReader::new(~[10]);
let mut reader = MemReader::new(vec!(10));
let byte = reader.read_byte();
assert!(byte == Ok(10));
}
@ -321,7 +321,7 @@ mod test {
#[test]
fn read_bytes() {
let mut reader = MemReader::new(~[10, 11, 12, 13]);
let mut reader = MemReader::new(vec!(10, 11, 12, 13));
let bytes = reader.read_exact(4).unwrap();
assert!(bytes == vec!(10, 11, 12, 13));
}
@ -337,13 +337,13 @@ mod test {
#[test]
fn read_bytes_eof() {
let mut reader = MemReader::new(~[10, 11]);
let mut reader = MemReader::new(vec!(10, 11));
assert!(reader.read_exact(4).is_err());
}
#[test]
fn push_exact() {
let mut reader = MemReader::new(~[10, 11, 12, 13]);
let mut reader = MemReader::new(vec!(10, 11, 12, 13));
let mut buf = vec!(8, 9);
reader.push_exact(&mut buf, 4).unwrap();
assert!(buf == vec!(8, 9, 10, 11, 12, 13));
@ -361,7 +361,7 @@ mod test {
#[test]
fn push_exact_eof() {
let mut reader = MemReader::new(~[10, 11]);
let mut reader = MemReader::new(vec!(10, 11));
let mut buf = vec!(8, 9);
assert!(reader.push_exact(&mut buf, 4).is_err());
assert!(buf == vec!(8, 9, 10, 11));

View File

@ -18,6 +18,7 @@ use io;
use io::{Reader, Writer, Seek, Buffer, IoError, SeekStyle, IoResult};
use slice;
use slice::{Vector, ImmutableVector, MutableVector, OwnedCloneableVector};
use vec::Vec;
fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult<u64> {
// compute offset as signed and clamp to prevent overflow
@ -49,10 +50,10 @@ fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult<u64>
/// let mut w = MemWriter::new();
/// w.write([0, 1, 2]);
///
/// assert_eq!(w.unwrap(), ~[0, 1, 2]);
/// assert_eq!(w.unwrap(), vec!(0, 1, 2));
/// ```
pub struct MemWriter {
buf: ~[u8],
buf: Vec<u8>,
pos: uint,
}
@ -64,7 +65,7 @@ impl MemWriter {
/// Create a new `MemWriter`, allocating at least `n` bytes for
/// the internal buffer.
pub fn with_capacity(n: uint) -> MemWriter {
MemWriter { buf: slice::with_capacity(n), pos: 0 }
MemWriter { buf: Vec::with_capacity(n), pos: 0 }
}
/// Acquires an immutable reference to the underlying buffer of this
@ -75,7 +76,7 @@ impl MemWriter {
pub fn get_ref<'a>(&'a self) -> &'a [u8] { self.buf.as_slice() }
/// Unwraps this `MemWriter`, returning the underlying buffer
pub fn unwrap(self) -> ~[u8] { self.buf }
pub fn unwrap(self) -> Vec<u8> { self.buf }
}
impl Writer for MemWriter {
@ -127,19 +128,19 @@ impl Seek for MemWriter {
/// # #[allow(unused_must_use)];
/// use std::io::MemReader;
///
/// let mut r = MemReader::new(~[0, 1, 2]);
/// let mut r = MemReader::new(vec!(0, 1, 2));
///
/// assert_eq!(r.read_to_end().unwrap(), vec!(0, 1, 2));
/// ```
pub struct MemReader {
buf: ~[u8],
buf: Vec<u8>,
pos: uint
}
impl MemReader {
/// Creates a new `MemReader` which will read the buffer given. The buffer
/// can be re-acquired through `unwrap`
pub fn new(buf: ~[u8]) -> MemReader {
pub fn new(buf: Vec<u8>) -> MemReader {
MemReader {
buf: buf,
pos: 0
@ -159,7 +160,7 @@ impl MemReader {
pub fn get_ref<'a>(&'a self) -> &'a [u8] { self.buf.as_slice() }
/// Unwraps this `MemReader`, returning the underlying buffer
pub fn unwrap(self) -> ~[u8] { self.buf }
pub fn unwrap(self) -> Vec<u8> { self.buf }
}
impl Reader for MemReader {
@ -425,7 +426,7 @@ mod test {
#[test]
fn test_mem_reader() {
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
let mut reader = MemReader::new(vec!(0, 1, 2, 3, 4, 5, 6, 7));
let mut buf = [];
assert_eq!(reader.read(buf), Ok(0));
assert_eq!(reader.tell(), Ok(0));
@ -440,7 +441,7 @@ mod test {
assert_eq!(reader.read(buf), Ok(3));
assert_eq!(buf.slice(0, 3), &[5, 6, 7]);
assert!(reader.read(buf).is_err());
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
let mut reader = MemReader::new(vec!(0, 1, 2, 3, 4, 5, 6, 7));
assert_eq!(reader.read_until(3).unwrap(), vec!(0, 1, 2, 3));
assert_eq!(reader.read_until(3).unwrap(), vec!(4, 5, 6, 7));
assert!(reader.read(buf).is_err());
@ -525,7 +526,7 @@ mod test {
r.seek(10, SeekSet).unwrap();
assert!(r.read(&mut []).is_err());
let mut r = MemReader::new(~[10]);
let mut r = MemReader::new(vec!(10));
r.seek(10, SeekSet).unwrap();
assert!(r.read(&mut []).is_err());
@ -545,7 +546,7 @@ mod test {
let mut r = BufReader::new(buf);
assert!(r.seek(-1, SeekSet).is_err());
let mut r = MemReader::new(~[10]);
let mut r = MemReader::new(vec!(10));
assert!(r.seek(-1, SeekSet).is_err());
let mut r = MemWriter::new();
@ -558,7 +559,7 @@ mod test {
#[test]
fn io_fill() {
let mut r = MemReader::new(~[1, 2, 3, 4, 5, 6, 7, 8]);
let mut r = MemReader::new(vec!(1, 2, 3, 4, 5, 6, 7, 8));
let mut buf = [0, ..3];
assert_eq!(r.fill(buf), Ok(()));
assert_eq!(buf.as_slice(), &[1, 2, 3]);

View File

@ -87,7 +87,7 @@ mod test {
let mut writer: io::IoResult<MemWriter> = Ok(MemWriter::new());
writer.write([0, 1, 2]).unwrap();
writer.flush().unwrap();
assert_eq!(writer.unwrap().unwrap(), ~[0, 1, 2]);
assert_eq!(writer.unwrap().unwrap(), vec!(0, 1, 2));
}
#[test]
@ -108,7 +108,7 @@ mod test {
#[test]
fn test_option_reader() {
let mut reader: io::IoResult<MemReader> =
Ok(MemReader::new(~[0, 1, 2, 3]));
Ok(MemReader::new(vec!(0, 1, 2, 3)));
let mut buf = [0, 0];
reader.read(buf).unwrap();
assert_eq!(buf.as_slice(), &[0, 1]);

View File

@ -204,7 +204,7 @@ mod test {
#[test]
fn test_limit_reader_unlimited() {
let mut r = MemReader::new(~[0, 1, 2]);
let mut r = MemReader::new(vec!(0, 1, 2));
{
let mut r = LimitReader::new(r.by_ref(), 4);
assert_eq!(vec!(0, 1, 2), r.read_to_end().unwrap());
@ -213,7 +213,7 @@ mod test {
#[test]
fn test_limit_reader_limited() {
let mut r = MemReader::new(~[0, 1, 2]);
let mut r = MemReader::new(vec!(0, 1, 2));
{
let mut r = LimitReader::new(r.by_ref(), 2);
assert_eq!(vec!(0, 1), r.read_to_end().unwrap());
@ -223,7 +223,7 @@ mod test {
#[test]
fn test_limit_reader_limit() {
let r = MemReader::new(~[0, 1, 2]);
let r = MemReader::new(vec!(0, 1, 2));
let mut r = LimitReader::new(r, 3);
assert_eq!(3, r.limit());
assert_eq!(0, r.read_byte().unwrap());
@ -285,26 +285,26 @@ mod test {
#[test]
fn test_chained_reader() {
let rs = ~[MemReader::new(~[0, 1]), MemReader::new(~[]),
MemReader::new(~[2, 3])];
let rs = ~[MemReader::new(vec!(0, 1)), MemReader::new(vec!()),
MemReader::new(vec!(2, 3))];
let mut r = ChainedReader::new(rs.move_iter());
assert_eq!(vec!(0, 1, 2, 3), r.read_to_end().unwrap());
}
#[test]
fn test_tee_reader() {
let mut r = TeeReader::new(MemReader::new(~[0, 1, 2]),
let mut r = TeeReader::new(MemReader::new(vec!(0, 1, 2)),
MemWriter::new());
assert_eq!(vec!(0, 1, 2), r.read_to_end().unwrap());
let (_, w) = r.unwrap();
assert_eq!(~[0, 1, 2], w.unwrap());
assert_eq!(vec!(0, 1, 2), w.unwrap());
}
#[test]
fn test_copy() {
let mut r = MemReader::new(~[0, 1, 2, 3, 4]);
let mut r = MemReader::new(vec!(0, 1, 2, 3, 4));
let mut w = MemWriter::new();
copy(&mut r, &mut w).unwrap();
assert_eq!(~[0, 1, 2, 3, 4], w.unwrap());
assert_eq!(vec!(0, 1, 2, 3, 4), w.unwrap());
}
}

View File

@ -28,7 +28,7 @@ use reflect::{MovePtr, align};
use result::{Ok, Err};
use str::StrSlice;
use to_str::ToStr;
use slice::OwnedVector;
use slice::{Vector, OwnedVector};
use intrinsics::{Disr, Opaque, TyDesc, TyVisitor, get_tydesc, visit_tydesc};
use raw;
@ -617,7 +617,7 @@ pub fn repr_to_str<T>(t: &T) -> ~str {
let mut result = io::MemWriter::new();
write_repr(&mut result as &mut io::Writer, t).unwrap();
str::from_utf8_owned(result.unwrap()).unwrap()
str::from_utf8(result.unwrap().as_slice()).unwrap().to_owned()
}
#[cfg(test)]
@ -635,7 +635,7 @@ fn test_repr() {
fn exact_test<T>(t: &T, e:&str) {
let mut m = io::MemWriter::new();
write_repr(&mut m as &mut io::Writer, t).unwrap();
let s = str::from_utf8_owned(m.unwrap()).unwrap();
let s = str::from_utf8(m.unwrap().as_slice()).unwrap().to_owned();
assert_eq!(s.as_slice(), e);
}

View File

@ -756,7 +756,7 @@ mod test {
macro_rules! t( ($a:expr, $b:expr) => ({
let mut m = MemWriter::new();
super::demangle(&mut m, $a).unwrap();
assert_eq!(str::from_utf8_owned(m.unwrap()).unwrap(), $b.to_owned());
assert_eq!(str::from_utf8(m.unwrap().as_slice()).unwrap().to_owned(), $b.to_owned());
}) )
#[test]

View File

@ -1015,7 +1015,7 @@ mod tests {
use std::io::MemWriter;
let mut m = MemWriter::new();
write_boxplot(&mut m as &mut io::Writer, s, 30).unwrap();
let out = str::from_utf8_owned(m.unwrap()).unwrap();
let out = str::from_utf8(m.unwrap().as_slice()).unwrap().to_owned();
assert_eq!(out, expected);
}

View File

@ -460,7 +460,7 @@ pub fn query_to_str(query: &Query) -> ~str {
write!(&mut writer, "{}={}", encode_component(*k),
encode_component(*v));
}
str::from_utf8_lossy(writer.unwrap()).into_owned()
str::from_utf8_lossy(writer.unwrap().as_slice()).into_owned()
}
/**

View File

@ -256,7 +256,7 @@ fn json_encode<'a, T:Encodable<json::Encoder<'a>, io::IoError>>(t: &T) -> ~str {
let mut writer = MemWriter::new();
let mut encoder = json::Encoder::new(&mut writer as &mut io::Writer);
let _ = t.encode(&mut encoder);
str::from_utf8_owned(writer.unwrap()).unwrap()
str::from_utf8(writer.unwrap().as_slice()).unwrap().to_owned()
}
// FIXME(#5121)

View File

@ -154,7 +154,7 @@ fn main() {
let rdr = if os::getenv("RUST_BENCH").is_some() {
let foo = include_bin!("shootout-k-nucleotide.data");
~MemReader::new(foo.to_owned()) as ~Reader
~MemReader::new(Vec::from_slice(foo)) as ~Reader
} else {
~stdio::stdin() as ~Reader
};

View File

@ -174,7 +174,7 @@ fn test_write() {
writeln!(w, "{foo}", foo="bar");
}
let s = str::from_utf8_owned(buf.unwrap()).unwrap();
let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_owned();
t!(s, "34helloline\nbar\n");
}
@ -198,7 +198,7 @@ fn test_format_args() {
format_args!(|args| { fmt::write(w, args); }, "test");
format_args!(|args| { fmt::write(w, args); }, "{test}", test=3);
}
let s = str::from_utf8_owned(buf.unwrap()).unwrap();
let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_owned();
t!(s, "1test3");
let s = format_args!(fmt::format, "hello {}", "world");