implement Writer for Vec<u8>
The trait has an obvious, sensible implementation directly on vectors so the MemWriter wrapper is unnecessary. This will halt the trend towards providing all of the vector methods on MemWriter along with eliminating the noise caused by conversions between the two types. It also provides the useful default Writer methods on Vec<u8>. After the type is removed and code has been migrated, it would make sense to add a new implementation of MemWriter with seeking support. The simple use cases can be covered with vectors alone, and ones with the need for seeks can use a new MemWriter implementation.
This commit is contained in:
parent
9c96a79a74
commit
85c2c2e38c
24 changed files with 120 additions and 129 deletions
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
extern crate serialize;
|
||||
|
||||
use std::io::MemWriter;
|
||||
use std::io;
|
||||
use serialize::{Encodable, Encoder};
|
||||
|
||||
|
|
@ -18,14 +17,14 @@ pub fn buffer_encode<'a,
|
|||
T:Encodable<serialize::json::Encoder<'a>,io::IoError>>(
|
||||
to_encode_object: &T)
|
||||
-> Vec<u8> {
|
||||
let mut m = MemWriter::new();
|
||||
let mut m = Vec::new();
|
||||
{
|
||||
let mut encoder =
|
||||
serialize::json::Encoder::new(&mut m as &mut io::Writer);
|
||||
//~^ ERROR `m` does not live long enough
|
||||
to_encode_object.encode(&mut encoder);
|
||||
}
|
||||
m.unwrap()
|
||||
m
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -31,12 +31,11 @@ fn test_rbml<'a, 'b, A:
|
|||
Encodable<EBWriter::Encoder<'a>> +
|
||||
Decodable<EBReader::Decoder<'b>>
|
||||
>(a1: &A) {
|
||||
let mut wr = std::io::MemWriter::new();
|
||||
let mut wr = Vec::new();
|
||||
let mut rbml_w = EBwriter::Encoder::new(&mut wr);
|
||||
a1.encode(&mut rbml_w);
|
||||
let bytes = wr.get_ref();
|
||||
|
||||
let d: serialize::rbml::Doc<'a> = EBDoc::new(bytes);
|
||||
let d: serialize::rbml::Doc<'a> = EBDoc::new(wr[]);
|
||||
let mut decoder: EBReader::Decoder<'a> = EBreader::Decoder::new(d);
|
||||
let a2: A = Decodable::decode(&mut decoder);
|
||||
assert!(*a1 == a2);
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
// no-pretty-expanded
|
||||
|
||||
#![allow(unused_must_use, dead_code)]
|
||||
#![allow(unused_must_use, dead_code, deprecated)]
|
||||
#![feature(macro_rules)]
|
||||
|
||||
use std::io::MemWriter;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@
|
|||
extern crate serialize;
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::io::MemWriter;
|
||||
use serialize::{Encodable, Decodable};
|
||||
use serialize::json;
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ extern crate rand;
|
|||
extern crate rbml;
|
||||
extern crate serialize;
|
||||
|
||||
use std::io::MemWriter;
|
||||
use rand::{random, Rand};
|
||||
use rbml;
|
||||
use rbml::Doc;
|
||||
|
|
@ -59,10 +58,10 @@ struct G<T> {
|
|||
fn roundtrip<'a, T: Rand + Eq + Encodable<Encoder<'a>> +
|
||||
Decodable<Decoder<'a>>>() {
|
||||
let obj: T = random();
|
||||
let mut w = MemWriter::new();
|
||||
let mut w = Vec::new();
|
||||
let mut e = Encoder::new(&mut w);
|
||||
obj.encode(&mut e);
|
||||
let doc = rbml::Doc::new(@w.get_ref());
|
||||
let doc = rbml::Doc::new(@w[]);
|
||||
let mut dec = Decoder::new(doc);
|
||||
let obj2 = Decodable::decode(&mut dec);
|
||||
assert!(obj == obj2);
|
||||
|
|
|
|||
|
|
@ -16,9 +16,7 @@
|
|||
#![allow(unused_must_use)]
|
||||
|
||||
use std::fmt;
|
||||
use std::io::MemWriter;
|
||||
use std::io;
|
||||
use std::str;
|
||||
|
||||
struct A;
|
||||
struct B;
|
||||
|
|
@ -161,7 +159,7 @@ pub fn main() {
|
|||
// Basic test to make sure that we can invoke the `write!` macro with an
|
||||
// io::Writer instance.
|
||||
fn test_write() {
|
||||
let mut buf = MemWriter::new();
|
||||
let mut buf = Vec::new();
|
||||
write!(&mut buf as &mut io::Writer, "{}", 3i);
|
||||
{
|
||||
let w = &mut buf as &mut io::Writer;
|
||||
|
|
@ -171,7 +169,7 @@ fn test_write() {
|
|||
writeln!(w, "{foo}", foo="bar");
|
||||
}
|
||||
|
||||
let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
|
||||
let s = String::from_utf8(buf).unwrap();
|
||||
t!(s, "34helloline\nbar\n");
|
||||
}
|
||||
|
||||
|
|
@ -188,14 +186,14 @@ fn test_print() {
|
|||
// Just make sure that the macros are defined, there's not really a lot that we
|
||||
// can do with them just yet (to test the output)
|
||||
fn test_format_args() {
|
||||
let mut buf = MemWriter::new();
|
||||
let mut buf = Vec::new();
|
||||
{
|
||||
let w = &mut buf as &mut io::Writer;
|
||||
format_args!(|args| { write!(w, "{}", args); }, "{}", 1i);
|
||||
format_args!(|args| { write!(w, "{}", args); }, "test");
|
||||
format_args!(|args| { write!(w, "{}", args); }, "{test}", test=3i);
|
||||
}
|
||||
let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
|
||||
let s = String::from_utf8(buf).unwrap();
|
||||
t!(s, "1test3");
|
||||
|
||||
let s = format_args!(fmt::format, "hello {}", "world");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue