Skip to content

Commit cd3f31d

Browse files
committed
Handle fallout in libserialize
API Changes: - from_base64() returns Result<Vec<u8>, FromBase64Error> - from_hex() returns Result<Vec<u8>, FromHexError> - json::List is a Vec<Json> - Decodable is no longer implemented on ~[T] (but Encodable still is) - DecoderHelpers::read_to_vec() returns a Result<Vec<T>, E>
1 parent 2a0dac6 commit cd3f31d

File tree

4 files changed

+73
-88
lines changed

4 files changed

+73
-88
lines changed

src/libserialize/base64.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'a> ToBase64 for &'a [u8] {
146146
}
147147

148148
unsafe {
149-
str::raw::from_utf8_owned(v.move_iter().collect())
149+
str::raw::from_utf8(v.as_slice()).to_owned()
150150
}
151151
}
152152
}
@@ -155,7 +155,7 @@ impl<'a> ToBase64 for &'a [u8] {
155155
pub trait FromBase64 {
156156
/// Converts the value of `self`, interpreted as base64 encoded data, into
157157
/// an owned vector of bytes, returning the vector.
158-
fn from_base64(&self) -> Result<~[u8], FromBase64Error>;
158+
fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error>;
159159
}
160160

161161
/// Errors that can occur when decoding a base64 encoded string
@@ -192,22 +192,21 @@ impl<'a> FromBase64 for &'a str {
192192
* ```rust
193193
* extern crate serialize;
194194
* use serialize::base64::{ToBase64, FromBase64, STANDARD};
195-
* use std::str;
196195
*
197196
* fn main () {
198197
* let hello_str = bytes!("Hello, World").to_base64(STANDARD);
199198
* println!("base64 output: {}", hello_str);
200199
* let res = hello_str.from_base64();
201200
* if res.is_ok() {
202-
* let opt_bytes = str::from_utf8_owned(res.unwrap());
201+
* let opt_bytes = StrBuf::from_utf8(res.unwrap());
203202
* if opt_bytes.is_some() {
204203
* println!("decoded from base64: {}", opt_bytes.unwrap());
205204
* }
206205
* }
207206
* }
208207
* ```
209208
*/
210-
fn from_base64(&self) -> Result<~[u8], FromBase64Error> {
209+
fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error> {
211210
let mut r = Vec::new();
212211
let mut buf: u32 = 0;
213212
let mut modulus = 0;
@@ -256,7 +255,7 @@ impl<'a> FromBase64 for &'a str {
256255
_ => return Err(InvalidBase64Length),
257256
}
258257

259-
Ok(r.move_iter().collect())
258+
Ok(r)
260259
}
261260
}
262261

@@ -301,21 +300,21 @@ mod tests {
301300

302301
#[test]
303302
fn test_from_base64_basic() {
304-
assert_eq!("".from_base64().unwrap(), "".as_bytes().to_owned());
305-
assert_eq!("Zg==".from_base64().unwrap(), "f".as_bytes().to_owned());
306-
assert_eq!("Zm8=".from_base64().unwrap(), "fo".as_bytes().to_owned());
307-
assert_eq!("Zm9v".from_base64().unwrap(), "foo".as_bytes().to_owned());
308-
assert_eq!("Zm9vYg==".from_base64().unwrap(), "foob".as_bytes().to_owned());
309-
assert_eq!("Zm9vYmE=".from_base64().unwrap(), "fooba".as_bytes().to_owned());
310-
assert_eq!("Zm9vYmFy".from_base64().unwrap(), "foobar".as_bytes().to_owned());
303+
assert_eq!("".from_base64().unwrap().as_slice(), "".as_bytes());
304+
assert_eq!("Zg==".from_base64().unwrap().as_slice(), "f".as_bytes());
305+
assert_eq!("Zm8=".from_base64().unwrap().as_slice(), "fo".as_bytes());
306+
assert_eq!("Zm9v".from_base64().unwrap().as_slice(), "foo".as_bytes());
307+
assert_eq!("Zm9vYg==".from_base64().unwrap().as_slice(), "foob".as_bytes());
308+
assert_eq!("Zm9vYmE=".from_base64().unwrap().as_slice(), "fooba".as_bytes());
309+
assert_eq!("Zm9vYmFy".from_base64().unwrap().as_slice(), "foobar".as_bytes());
311310
}
312311

313312
#[test]
314313
fn test_from_base64_newlines() {
315-
assert_eq!("Zm9v\r\nYmFy".from_base64().unwrap(),
316-
"foobar".as_bytes().to_owned());
317-
assert_eq!("Zm9vYg==\r\n".from_base64().unwrap(),
318-
"foob".as_bytes().to_owned());
314+
assert_eq!("Zm9v\r\nYmFy".from_base64().unwrap().as_slice(),
315+
"foobar".as_bytes());
316+
assert_eq!("Zm9vYg==\r\n".from_base64().unwrap().as_slice(),
317+
"foob".as_bytes());
319318
}
320319

321320
#[test]
@@ -341,8 +340,8 @@ mod tests {
341340
for _ in range(0, 1000) {
342341
let times = task_rng().gen_range(1u, 100);
343342
let v = Vec::from_fn(times, |_| random::<u8>());
344-
assert_eq!(v.as_slice().to_base64(STANDARD).from_base64().unwrap(),
345-
v.as_slice().to_owned());
343+
assert_eq!(v.as_slice().to_base64(STANDARD).from_base64().unwrap().as_slice(),
344+
v.as_slice());
346345
}
347346
}
348347

src/libserialize/hex.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'a> ToHex for &'a [u8] {
4545
}
4646

4747
unsafe {
48-
str::raw::from_utf8_owned(v.move_iter().collect())
48+
str::raw::from_utf8(v.as_slice()).to_owned()
4949
}
5050
}
5151
}
@@ -54,7 +54,7 @@ impl<'a> ToHex for &'a [u8] {
5454
pub trait FromHex {
5555
/// Converts the value of `self`, interpreted as hexadecimal encoded data,
5656
/// into an owned vector of bytes, returning the vector.
57-
fn from_hex(&self) -> Result<~[u8], FromHexError>;
57+
fn from_hex(&self) -> Result<Vec<u8>, FromHexError>;
5858
}
5959

6060
/// Errors that can occur when decoding a hex encoded string
@@ -91,19 +91,18 @@ impl<'a> FromHex for &'a str {
9191
* ```rust
9292
* extern crate serialize;
9393
* use serialize::hex::{FromHex, ToHex};
94-
* use std::str;
9594
*
9695
* fn main () {
9796
* let hello_str = "Hello, World".as_bytes().to_hex();
9897
* println!("{}", hello_str);
9998
* let bytes = hello_str.from_hex().unwrap();
10099
* println!("{:?}", bytes);
101-
* let result_str = str::from_utf8_owned(bytes).unwrap();
100+
* let result_str = StrBuf::from_utf8(bytes).unwrap();
102101
* println!("{}", result_str);
103102
* }
104103
* ```
105104
*/
106-
fn from_hex(&self) -> Result<~[u8], FromHexError> {
105+
fn from_hex(&self) -> Result<Vec<u8>, FromHexError> {
107106
// This may be an overestimate if there is any whitespace
108107
let mut b = Vec::with_capacity(self.len() / 2);
109108
let mut modulus = 0;
@@ -150,10 +149,10 @@ mod tests {
150149

151150
#[test]
152151
pub fn test_from_hex_okay() {
153-
assert_eq!("666f6f626172".from_hex().unwrap(),
154-
"foobar".as_bytes().to_owned());
155-
assert_eq!("666F6F626172".from_hex().unwrap(),
156-
"foobar".as_bytes().to_owned());
152+
assert_eq!("666f6f626172".from_hex().unwrap().as_slice(),
153+
"foobar".as_bytes());
154+
assert_eq!("666F6F626172".from_hex().unwrap().as_slice(),
155+
"foobar".as_bytes());
157156
}
158157

159158
#[test]
@@ -169,8 +168,8 @@ mod tests {
169168

170169
#[test]
171170
pub fn test_from_hex_ignores_whitespace() {
172-
assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(),
173-
"foobar".as_bytes().to_owned());
171+
assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap().as_slice(),
172+
"foobar".as_bytes());
174173
}
175174

176175
#[test]
@@ -183,8 +182,8 @@ mod tests {
183182
#[test]
184183
pub fn test_from_hex_all_bytes() {
185184
for i in range(0, 256) {
186-
assert_eq!(format!("{:02x}", i as uint).from_hex().unwrap(), ~[i as u8]);
187-
assert_eq!(format!("{:02X}", i as uint).from_hex().unwrap(), ~[i as u8]);
185+
assert_eq!(format!("{:02x}", i as uint).from_hex().unwrap().as_slice(), &[i as u8]);
186+
assert_eq!(format!("{:02X}", i as uint).from_hex().unwrap().as_slice(), &[i as u8]);
188187
}
189188
}
190189

src/libserialize/json.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub enum Json {
258258
Null,
259259
}
260260

261-
pub type List = ~[Json];
261+
pub type List = Vec<Json>;
262262
pub type Object = TreeMap<~str, Json>;
263263

264264
/// The errors that can arise while parsing a JSON stream.
@@ -2211,7 +2211,7 @@ impl<A:ToJson,B:ToJson> ToJson for (A, B) {
22112211
fn to_json(&self) -> Json {
22122212
match *self {
22132213
(ref a, ref b) => {
2214-
List(box [a.to_json(), b.to_json()])
2214+
List(vec![a.to_json(), b.to_json()])
22152215
}
22162216
}
22172217
}
@@ -2221,7 +2221,7 @@ impl<A:ToJson,B:ToJson,C:ToJson> ToJson for (A, B, C) {
22212221
fn to_json(&self) -> Json {
22222222
match *self {
22232223
(ref a, ref b, ref c) => {
2224-
List(box [a.to_json(), b.to_json(), c.to_json()])
2224+
List(vec![a.to_json(), b.to_json(), c.to_json()])
22252225
}
22262226
}
22272227
}
@@ -2298,12 +2298,12 @@ mod tests {
22982298
struct Inner {
22992299
a: (),
23002300
b: uint,
2301-
c: ~[~str],
2301+
c: Vec<~str>,
23022302
}
23032303

23042304
#[deriving(Eq, Encodable, Decodable, Show)]
23052305
struct Outer {
2306-
inner: ~[Inner],
2306+
inner: Vec<Inner>,
23072307
}
23082308

23092309
fn mk_object(items: &[(~str, Json)]) -> Json {
@@ -2360,22 +2360,22 @@ mod tests {
23602360

23612361
#[test]
23622362
fn test_write_list() {
2363-
assert_eq!(List(~[]).to_str(), "[]".to_owned());
2364-
assert_eq!(List(~[]).to_pretty_str(), "[]".to_owned());
2363+
assert_eq!(List(vec![]).to_str(), "[]".to_owned());
2364+
assert_eq!(List(vec![]).to_pretty_str(), "[]".to_owned());
23652365

2366-
assert_eq!(List(~[Boolean(true)]).to_str(), "[true]".to_owned());
2366+
assert_eq!(List(vec![Boolean(true)]).to_str(), "[true]".to_owned());
23672367
assert_eq!(
2368-
List(~[Boolean(true)]).to_pretty_str(),
2368+
List(vec![Boolean(true)]).to_pretty_str(),
23692369
"\
23702370
[\n \
23712371
true\n\
23722372
]".to_owned()
23732373
);
23742374

2375-
let long_test_list = List(box [
2375+
let long_test_list = List(vec![
23762376
Boolean(false),
23772377
Null,
2378-
List(box [String("foo\nbar".to_owned()), Number(3.5)])]);
2378+
List(vec![String("foo\nbar".to_owned()), Number(3.5)])]);
23792379

23802380
assert_eq!(long_test_list.to_str(),
23812381
"[false,null,[\"foo\\nbar\",3.5]]".to_owned());
@@ -2411,7 +2411,7 @@ mod tests {
24112411
);
24122412

24132413
let complex_obj = mk_object([
2414-
("b".to_owned(), List(box [
2414+
("b".to_owned(), List(vec![
24152415
mk_object([("c".to_owned(), String("\x0c\r".to_owned()))]),
24162416
mk_object([("d".to_owned(), String("".to_owned()))])
24172417
]))
@@ -2443,7 +2443,7 @@ mod tests {
24432443

24442444
let a = mk_object([
24452445
("a".to_owned(), Boolean(true)),
2446-
("b".to_owned(), List(box [
2446+
("b".to_owned(), List(vec![
24472447
mk_object([("c".to_owned(), String("\x0c\r".to_owned()))]),
24482448
mk_object([("d".to_owned(), String("".to_owned()))])
24492449
]))
@@ -2678,44 +2678,44 @@ mod tests {
26782678
assert_eq!(from_str("[1,]"), Err(SyntaxError(InvalidSyntax, 1, 4)));
26792679
assert_eq!(from_str("[6 7]"), Err(SyntaxError(InvalidSyntax, 1, 4)));
26802680

2681-
assert_eq!(from_str("[]"), Ok(List(~[])));
2682-
assert_eq!(from_str("[ ]"), Ok(List(~[])));
2683-
assert_eq!(from_str("[true]"), Ok(List(~[Boolean(true)])));
2684-
assert_eq!(from_str("[ false ]"), Ok(List(~[Boolean(false)])));
2685-
assert_eq!(from_str("[null]"), Ok(List(~[Null])));
2681+
assert_eq!(from_str("[]"), Ok(List(vec![])));
2682+
assert_eq!(from_str("[ ]"), Ok(List(vec![])));
2683+
assert_eq!(from_str("[true]"), Ok(List(vec![Boolean(true)])));
2684+
assert_eq!(from_str("[ false ]"), Ok(List(vec![Boolean(false)])));
2685+
assert_eq!(from_str("[null]"), Ok(List(vec![Null])));
26862686
assert_eq!(from_str("[3, 1]"),
2687-
Ok(List(~[Number(3.0), Number(1.0)])));
2687+
Ok(List(vec![Number(3.0), Number(1.0)])));
26882688
assert_eq!(from_str("\n[3, 2]\n"),
2689-
Ok(List(~[Number(3.0), Number(2.0)])));
2689+
Ok(List(vec![Number(3.0), Number(2.0)])));
26902690
assert_eq!(from_str("[2, [4, 1]]"),
2691-
Ok(List(~[Number(2.0), List(~[Number(4.0), Number(1.0)])])));
2691+
Ok(List(vec![Number(2.0), List(vec![Number(4.0), Number(1.0)])])));
26922692
}
26932693

26942694
#[test]
26952695
fn test_decode_list() {
26962696
let mut decoder = Decoder::new(from_str("[]").unwrap());
2697-
let v: ~[()] = Decodable::decode(&mut decoder).unwrap();
2698-
assert_eq!(v, ~[]);
2697+
let v: Vec<()> = Decodable::decode(&mut decoder).unwrap();
2698+
assert_eq!(v, vec![]);
26992699

27002700
let mut decoder = Decoder::new(from_str("[null]").unwrap());
2701-
let v: ~[()] = Decodable::decode(&mut decoder).unwrap();
2702-
assert_eq!(v, ~[()]);
2701+
let v: Vec<()> = Decodable::decode(&mut decoder).unwrap();
2702+
assert_eq!(v, vec![()]);
27032703

27042704
let mut decoder = Decoder::new(from_str("[true]").unwrap());
2705-
let v: ~[bool] = Decodable::decode(&mut decoder).unwrap();
2706-
assert_eq!(v, ~[true]);
2705+
let v: Vec<bool> = Decodable::decode(&mut decoder).unwrap();
2706+
assert_eq!(v, vec![true]);
27072707

27082708
let mut decoder = Decoder::new(from_str("[true]").unwrap());
2709-
let v: ~[bool] = Decodable::decode(&mut decoder).unwrap();
2710-
assert_eq!(v, ~[true]);
2709+
let v: Vec<bool> = Decodable::decode(&mut decoder).unwrap();
2710+
assert_eq!(v, vec![true]);
27112711

27122712
let mut decoder = Decoder::new(from_str("[3, 1]").unwrap());
2713-
let v: ~[int] = Decodable::decode(&mut decoder).unwrap();
2714-
assert_eq!(v, ~[3, 1]);
2713+
let v: Vec<int> = Decodable::decode(&mut decoder).unwrap();
2714+
assert_eq!(v, vec![3, 1]);
27152715

27162716
let mut decoder = Decoder::new(from_str("[[3], [1, 2]]").unwrap());
2717-
let v: ~[~[uint]] = Decodable::decode(&mut decoder).unwrap();
2718-
assert_eq!(v, ~[~[3], ~[1, 2]]);
2717+
let v: Vec<Vec<uint>> = Decodable::decode(&mut decoder).unwrap();
2718+
assert_eq!(v, vec![vec![3], vec![1, 2]]);
27192719
}
27202720

27212721
#[test]
@@ -2750,7 +2750,7 @@ mod tests {
27502750
"{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(),
27512751
mk_object([
27522752
("a".to_owned(), Number(1.0)),
2753-
("b".to_owned(), List(~[Boolean(true)]))
2753+
("b".to_owned(), List(vec![Boolean(true)]))
27542754
]));
27552755
assert_eq!(from_str(
27562756
"{".to_owned() +
@@ -2763,7 +2763,7 @@ mod tests {
27632763
"}").unwrap(),
27642764
mk_object([
27652765
("a".to_owned(), Number(1.0)),
2766-
("b".to_owned(), List(~[
2766+
("b".to_owned(), List(vec![
27672767
Boolean(true),
27682768
String("foo\nbar".to_owned()),
27692769
mk_object([
@@ -2785,8 +2785,8 @@ mod tests {
27852785
assert_eq!(
27862786
v,
27872787
Outer {
2788-
inner: ~[
2789-
Inner { a: (), b: 2, c: ~["abc".to_owned(), "xyz".to_owned()] }
2788+
inner: vec![
2789+
Inner { a: (), b: 2, c: vec!["abc".to_owned(), "xyz".to_owned()] }
27902790
]
27912791
}
27922792
);
@@ -2837,7 +2837,7 @@ mod tests {
28372837
x: f64,
28382838
y: bool,
28392839
z: ~str,
2840-
w: ~[DecodeStruct]
2840+
w: Vec<DecodeStruct>
28412841
}
28422842
#[deriving(Decodable)]
28432843
enum DecodeEnum {

0 commit comments

Comments
 (0)