Skip to content

Commit 28bcef8

Browse files
committed
libserialize: Remove all uses of ~str from libserialize.
Had to make `struct Tm` in `libtime` not serializable for now.
1 parent 67e39a8 commit 28bcef8

File tree

13 files changed

+316
-275
lines changed

13 files changed

+316
-275
lines changed

src/librustdoc/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn json_input(input: &str) -> Result<Output, StrBuf> {
364364
Ok(json::Object(obj)) => {
365365
let mut obj = obj;
366366
// Make sure the schema is what we expect
367-
match obj.pop(&"schema".to_owned()) {
367+
match obj.pop(&"schema".to_strbuf()) {
368368
Some(json::String(version)) => {
369369
if version.as_slice() != SCHEMA_VERSION {
370370
return Err(format_strbuf!(
@@ -375,7 +375,7 @@ fn json_input(input: &str) -> Result<Output, StrBuf> {
375375
Some(..) => return Err("malformed json".to_strbuf()),
376376
None => return Err("expected a schema version".to_strbuf()),
377377
}
378-
let krate = match obj.pop(&"crate".to_str()) {
378+
let krate = match obj.pop(&"crate".to_strbuf()) {
379379
Some(json) => {
380380
let mut d = json::Decoder::new(json);
381381
Decodable::decode(&mut d).unwrap()
@@ -404,13 +404,14 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
404404
// "plugins": { output of plugins ... }
405405
// }
406406
let mut json = box collections::TreeMap::new();
407-
json.insert("schema".to_owned(), json::String(SCHEMA_VERSION.to_owned()));
407+
json.insert("schema".to_strbuf(),
408+
json::String(SCHEMA_VERSION.to_strbuf()));
408409
let plugins_json = box res.move_iter()
409410
.filter_map(|opt| {
410411
match opt {
411412
None => None,
412413
Some((string, json)) => {
413-
Some((string.to_owned(), json))
414+
Some((string.to_strbuf(), json))
414415
}
415416
}
416417
}).collect();
@@ -423,15 +424,15 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
423424
let mut encoder = json::Encoder::new(&mut w as &mut io::Writer);
424425
krate.encode(&mut encoder).unwrap();
425426
}
426-
str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned()
427+
str::from_utf8(w.unwrap().as_slice()).unwrap().to_strbuf()
427428
};
428-
let crate_json = match json::from_str(crate_json_str) {
429+
let crate_json = match json::from_str(crate_json_str.as_slice()) {
429430
Ok(j) => j,
430431
Err(e) => fail!("Rust generated JSON is invalid: {:?}", e)
431432
};
432433

433-
json.insert("crate".to_owned(), crate_json);
434-
json.insert("plugins".to_owned(), json::Object(plugins_json));
434+
json.insert("crate".to_strbuf(), crate_json);
435+
json.insert("plugins".to_strbuf(), json::Object(plugins_json));
435436

436437
let mut file = try!(File::create(&dst));
437438
try!(json::Object(json).to_writer(&mut file));

src/libserialize/base64.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static URLSAFE_CHARS: &'static[u8] = bytes!("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
5454
pub trait ToBase64 {
5555
/// Converts the value of `self` to a base64 value following the specified
5656
/// format configuration, returning the owned string.
57-
fn to_base64(&self, config: Config) -> ~str;
57+
fn to_base64(&self, config: Config) -> StrBuf;
5858
}
5959

6060
impl<'a> ToBase64 for &'a [u8] {
@@ -73,7 +73,7 @@ impl<'a> ToBase64 for &'a [u8] {
7373
* }
7474
* ```
7575
*/
76-
fn to_base64(&self, config: Config) -> ~str {
76+
fn to_base64(&self, config: Config) -> StrBuf {
7777
let bytes = match config.char_set {
7878
Standard => STANDARD_CHARS,
7979
UrlSafe => URLSAFE_CHARS
@@ -146,7 +146,7 @@ impl<'a> ToBase64 for &'a [u8] {
146146
}
147147

148148
unsafe {
149-
str::raw::from_utf8(v.as_slice()).to_owned()
149+
str::raw::from_utf8(v.as_slice()).to_strbuf()
150150
}
151151
}
152152
}
@@ -195,7 +195,7 @@ impl<'a> FromBase64 for &'a str {
195195
* fn main () {
196196
* let hello_str = bytes!("Hello, World").to_base64(STANDARD);
197197
* println!("base64 output: {}", hello_str);
198-
* let res = hello_str.from_base64();
198+
* let res = hello_str.as_slice().from_base64();
199199
* if res.is_ok() {
200200
* let opt_bytes = StrBuf::from_utf8(res.unwrap());
201201
* if opt_bytes.is_ok() {
@@ -267,34 +267,35 @@ mod tests {
267267

268268
#[test]
269269
fn test_to_base64_basic() {
270-
assert_eq!("".as_bytes().to_base64(STANDARD), "".to_owned());
271-
assert_eq!("f".as_bytes().to_base64(STANDARD), "Zg==".to_owned());
272-
assert_eq!("fo".as_bytes().to_base64(STANDARD), "Zm8=".to_owned());
273-
assert_eq!("foo".as_bytes().to_base64(STANDARD), "Zm9v".to_owned());
274-
assert_eq!("foob".as_bytes().to_base64(STANDARD), "Zm9vYg==".to_owned());
275-
assert_eq!("fooba".as_bytes().to_base64(STANDARD), "Zm9vYmE=".to_owned());
276-
assert_eq!("foobar".as_bytes().to_base64(STANDARD), "Zm9vYmFy".to_owned());
270+
assert_eq!("".as_bytes().to_base64(STANDARD), "".to_strbuf());
271+
assert_eq!("f".as_bytes().to_base64(STANDARD), "Zg==".to_strbuf());
272+
assert_eq!("fo".as_bytes().to_base64(STANDARD), "Zm8=".to_strbuf());
273+
assert_eq!("foo".as_bytes().to_base64(STANDARD), "Zm9v".to_strbuf());
274+
assert_eq!("foob".as_bytes().to_base64(STANDARD), "Zm9vYg==".to_strbuf());
275+
assert_eq!("fooba".as_bytes().to_base64(STANDARD), "Zm9vYmE=".to_strbuf());
276+
assert_eq!("foobar".as_bytes().to_base64(STANDARD), "Zm9vYmFy".to_strbuf());
277277
}
278278

279279
#[test]
280280
fn test_to_base64_line_break() {
281281
assert!(![0u8, ..1000].to_base64(Config {line_length: None, ..STANDARD})
282-
.contains("\r\n"));
282+
.as_slice()
283+
.contains("\r\n"));
283284
assert_eq!("foobar".as_bytes().to_base64(Config {line_length: Some(4),
284285
..STANDARD}),
285-
"Zm9v\r\nYmFy".to_owned());
286+
"Zm9v\r\nYmFy".to_strbuf());
286287
}
287288

288289
#[test]
289290
fn test_to_base64_padding() {
290-
assert_eq!("f".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zg".to_owned());
291-
assert_eq!("fo".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zm8".to_owned());
291+
assert_eq!("f".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zg".to_strbuf());
292+
assert_eq!("fo".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zm8".to_strbuf());
292293
}
293294

294295
#[test]
295296
fn test_to_base64_url_safe() {
296-
assert_eq!([251, 255].to_base64(URL_SAFE), "-_8".to_owned());
297-
assert_eq!([251, 255].to_base64(STANDARD), "+/8=".to_owned());
297+
assert_eq!([251, 255].to_base64(URL_SAFE), "-_8".to_strbuf());
298+
assert_eq!([251, 255].to_base64(STANDARD), "+/8=".to_strbuf());
298299
}
299300

300301
#[test]
@@ -339,7 +340,12 @@ mod tests {
339340
for _ in range(0, 1000) {
340341
let times = task_rng().gen_range(1u, 100);
341342
let v = Vec::from_fn(times, |_| random::<u8>());
342-
assert_eq!(v.as_slice().to_base64(STANDARD).from_base64().unwrap().as_slice(),
343+
assert_eq!(v.as_slice()
344+
.to_base64(STANDARD)
345+
.as_slice()
346+
.from_base64()
347+
.unwrap()
348+
.as_slice(),
343349
v.as_slice());
344350
}
345351
}
@@ -360,7 +366,7 @@ mod tests {
360366
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
361367
let sb = s.as_bytes().to_base64(STANDARD);
362368
b.iter(|| {
363-
sb.from_base64().unwrap();
369+
sb.as_slice().from_base64().unwrap();
364370
});
365371
b.bytes = sb.len() as u64;
366372
}

src/libserialize/ebml.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ impl<'doc> Doc<'doc> {
3434
str::from_utf8(self.data.slice(self.start, self.end)).unwrap()
3535
}
3636

37-
pub fn as_str(&self) -> ~str {
38-
self.as_str_slice().to_owned()
37+
pub fn as_str(&self) -> StrBuf {
38+
self.as_str_slice().to_strbuf()
3939
}
4040
}
4141

@@ -80,7 +80,7 @@ pub enum EbmlEncoderTag {
8080
#[deriving(Show)]
8181
pub enum Error {
8282
IntTooBig(uint),
83-
Expected(~str),
83+
Expected(StrBuf),
8484
IoError(io::IoError)
8585
}
8686
// --------------------------------------
@@ -312,7 +312,10 @@ pub mod reader {
312312
self.pos = r_doc.end;
313313
let str = r_doc.as_str_slice();
314314
if lbl != str {
315-
return Err(Expected(format!("Expected label {} but found {}", lbl, str)));
315+
return Err(Expected(format_strbuf!("Expected label \
316+
{} but found {}",
317+
lbl,
318+
str)));
316319
}
317320
}
318321
}
@@ -322,7 +325,8 @@ pub mod reader {
322325
fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> DecodeResult<Doc<'doc>> {
323326
debug!(". next_doc(exp_tag={:?})", exp_tag);
324327
if self.pos >= self.parent.end {
325-
return Err(Expected(format!("no more documents in current node!")));
328+
return Err(Expected(format_strbuf!("no more documents in \
329+
current node!")));
326330
}
327331
let TaggedDoc { tag: r_tag, doc: r_doc } =
328332
try!(doc_at(self.parent.data, self.pos));
@@ -334,12 +338,18 @@ pub mod reader {
334338
r_doc.start,
335339
r_doc.end);
336340
if r_tag != (exp_tag as uint) {
337-
return Err(Expected(format!("expected EBML doc with tag {:?} but found tag {:?}",
338-
exp_tag, r_tag)));
341+
return Err(Expected(format_strbuf!("expected EBML doc with \
342+
tag {:?} but found tag \
343+
{:?}",
344+
exp_tag,
345+
r_tag)));
339346
}
340347
if r_doc.end > self.parent.end {
341-
return Err(Expected(format!("invalid EBML, child extends to {:#x}, parent to {:#x}",
342-
r_doc.end, self.parent.end)));
348+
return Err(Expected(format_strbuf!("invalid EBML, child \
349+
extends to {:#x}, parent \
350+
to {:#x}",
351+
r_doc.end,
352+
self.parent.end)));
343353
}
344354
self.pos = r_doc.end;
345355
Ok(r_doc)
@@ -433,7 +443,7 @@ pub mod reader {
433443
fn read_char(&mut self) -> DecodeResult<char> {
434444
Ok(char::from_u32(doc_as_u32(try!(self.next_doc(EsChar)))).unwrap())
435445
}
436-
fn read_str(&mut self) -> DecodeResult<~str> {
446+
fn read_str(&mut self) -> DecodeResult<StrBuf> {
437447
Ok(try!(self.next_doc(EsStr)).as_str())
438448
}
439449

@@ -570,7 +580,10 @@ pub mod reader {
570580
match idx {
571581
0 => f(this, false),
572582
1 => f(this, true),
573-
_ => Err(Expected(format!("Expected None or Some"))),
583+
_ => {
584+
Err(Expected(format_strbuf!("Expected None or \
585+
Some")))
586+
}
574587
}
575588
})
576589
})

src/libserialize/hex.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::fmt;
1616
pub trait ToHex {
1717
/// Converts the value of `self` to a hex value, returning the owned
1818
/// string.
19-
fn to_hex(&self) -> ~str;
19+
fn to_hex(&self) -> StrBuf;
2020
}
2121

2222
static CHARS: &'static[u8] = bytes!("0123456789abcdef");
@@ -37,15 +37,15 @@ impl<'a> ToHex for &'a [u8] {
3737
* }
3838
* ```
3939
*/
40-
fn to_hex(&self) -> ~str {
40+
fn to_hex(&self) -> StrBuf {
4141
let mut v = Vec::with_capacity(self.len() * 2);
4242
for &byte in self.iter() {
4343
v.push(CHARS[(byte >> 4) as uint]);
4444
v.push(CHARS[(byte & 0xf) as uint]);
4545
}
4646

4747
unsafe {
48-
str::raw::from_utf8(v.as_slice()).to_owned()
48+
str::raw::from_utf8(v.as_slice()).to_strbuf()
4949
}
5050
}
5151
}
@@ -94,7 +94,7 @@ impl<'a> FromHex for &'a str {
9494
* fn main () {
9595
* let hello_str = "Hello, World".as_bytes().to_hex();
9696
* println!("{}", hello_str);
97-
* let bytes = hello_str.from_hex().unwrap();
97+
* let bytes = hello_str.as_slice().from_hex().unwrap();
9898
* println!("{:?}", bytes);
9999
* let result_str = StrBuf::from_utf8(bytes).unwrap();
100100
* println!("{}", result_str);
@@ -143,7 +143,7 @@ mod tests {
143143

144144
#[test]
145145
pub fn test_to_hex() {
146-
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172".to_owned());
146+
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172".to_strbuf());
147147
}
148148

149149
#[test]
@@ -174,7 +174,8 @@ mod tests {
174174
#[test]
175175
pub fn test_to_hex_all_bytes() {
176176
for i in range(0, 256) {
177-
assert_eq!([i as u8].to_hex(), format!("{:02x}", i as uint));
177+
assert_eq!([i as u8].to_hex(),
178+
format_strbuf!("{:02x}", i as uint));
178179
}
179180
}
180181

@@ -202,7 +203,7 @@ mod tests {
202203
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
203204
let sb = s.as_bytes().to_hex();
204205
b.iter(|| {
205-
sb.from_hex().unwrap();
206+
sb.as_slice().from_hex().unwrap();
206207
});
207208
b.bytes = sb.len() as u64;
208209
}

0 commit comments

Comments
 (0)