Skip to content

Commit

Permalink
Add serialization support for StrBuf
Browse files Browse the repository at this point in the history
- implement Encodable and Decodable for StrBuf
- implement to_json for StrBuf
  • Loading branch information
hjr3 committed May 1, 2014
1 parent 9f484e6 commit fa6efed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
58 changes: 23 additions & 35 deletions src/libserialize/json.rs
Expand Up @@ -2205,6 +2205,10 @@ impl ToJson for ~str {
fn to_json(&self) -> Json { String((*self).clone()) }
}

impl ToJson for StrBuf {
fn to_json(&self) -> Json { String((*self).as_slice().into_owned()) }
}

impl<A:ToJson,B:ToJson> ToJson for (A, B) {
fn to_json(&self) -> Json {
match *self {
Expand Down Expand Up @@ -2643,41 +2647,25 @@ mod tests {

#[test]
fn test_decode_str() {
let mut decoder = Decoder::new(from_str("\"\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "".to_owned());

let mut decoder = Decoder::new(from_str("\"foo\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "foo".to_owned());

let mut decoder = Decoder::new(from_str("\"\\\"\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "\"".to_owned());

let mut decoder = Decoder::new(from_str("\"\\b\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "\x08".to_owned());

let mut decoder = Decoder::new(from_str("\"\\n\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "\n".to_owned());

let mut decoder = Decoder::new(from_str("\"\\r\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "\r".to_owned());

let mut decoder = Decoder::new(from_str("\"\\t\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "\t".to_owned());

let mut decoder = Decoder::new(from_str("\"\\u12ab\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "\u12ab".to_owned());

let mut decoder = Decoder::new(from_str("\"\\uAB12\"").unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, "\uAB12".to_owned());
let s = [("\"\"", ""),
("\"foo\"", "foo"),
("\"\\\"\"", "\""),
("\"\\b\"", "\x08"),
("\"\\n\"", "\n"),
("\"\\r\"", "\r"),
("\"\\t\"", "\t"),
("\"\\u12ab\"", "\u12ab"),
("\"\\uAB12\"", "\uAB12")];

for &(i, o) in s.iter() {
let mut decoder = Decoder::new(from_str(i).unwrap());
let v: StrBuf = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v.as_slice(), o);

let mut decoder = Decoder::new(from_str(i).unwrap());
let v: ~str = Decodable::decode(&mut decoder).unwrap();
assert_eq!(v, o.to_owned());
}
}

#[test]
Expand Down
12 changes: 12 additions & 0 deletions src/libserialize/serialize.rs
Expand Up @@ -313,6 +313,18 @@ impl<E, D:Decoder<E>> Decodable<D, E> for ~str {
}
}

impl<E, S:Encoder<E>> Encodable<S, E> for StrBuf {
fn encode(&self, s: &mut S) -> Result<(), E> {
s.emit_str(self.as_slice())
}
}

impl<E, D:Decoder<E>> Decodable<D, E> for StrBuf {
fn decode(d: &mut D) -> Result<StrBuf, E> {
Ok(StrBuf::from_str(try!(d.read_str())))
}
}

impl<E, S:Encoder<E>> Encodable<S, E> for f32 {
fn encode(&self, s: &mut S) -> Result<(), E> {
s.emit_f32(*self)
Expand Down

9 comments on commit fa6efed

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at hjr3@fa6efed

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging hjr3/rust/serialize-strbuf = fa6efed into auto

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hjr3/rust/serialize-strbuf = fa6efed merged ok, testing candidate = d5499a63

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at hjr3@fa6efed

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging hjr3/rust/serialize-strbuf = fa6efed into auto

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hjr3/rust/serialize-strbuf = fa6efed merged ok, testing candidate = ee03529

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

@bors
Copy link
Contributor

@bors bors commented on fa6efed May 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = ee03529

Please sign in to comment.