提交 28bcef85 编写于 作者: P Patrick Walton

libserialize: Remove all uses of `~str` from `libserialize`.

Had to make `struct Tm` in `libtime` not serializable for now.
上级 67e39a8e
......@@ -364,7 +364,7 @@ fn json_input(input: &str) -> Result<Output, StrBuf> {
Ok(json::Object(obj)) => {
let mut obj = obj;
// Make sure the schema is what we expect
match obj.pop(&"schema".to_owned()) {
match obj.pop(&"schema".to_strbuf()) {
Some(json::String(version)) => {
if version.as_slice() != SCHEMA_VERSION {
return Err(format_strbuf!(
......@@ -375,7 +375,7 @@ fn json_input(input: &str) -> Result<Output, StrBuf> {
Some(..) => return Err("malformed json".to_strbuf()),
None => return Err("expected a schema version".to_strbuf()),
}
let krate = match obj.pop(&"crate".to_str()) {
let krate = match obj.pop(&"crate".to_strbuf()) {
Some(json) => {
let mut d = json::Decoder::new(json);
Decodable::decode(&mut d).unwrap()
......@@ -404,13 +404,14 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
// "plugins": { output of plugins ... }
// }
let mut json = box collections::TreeMap::new();
json.insert("schema".to_owned(), json::String(SCHEMA_VERSION.to_owned()));
json.insert("schema".to_strbuf(),
json::String(SCHEMA_VERSION.to_strbuf()));
let plugins_json = box res.move_iter()
.filter_map(|opt| {
match opt {
None => None,
Some((string, json)) => {
Some((string.to_owned(), json))
Some((string.to_strbuf(), json))
}
}
}).collect();
......@@ -423,15 +424,15 @@ 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(w.unwrap().as_slice()).unwrap().to_owned()
str::from_utf8(w.unwrap().as_slice()).unwrap().to_strbuf()
};
let crate_json = match json::from_str(crate_json_str) {
let crate_json = match json::from_str(crate_json_str.as_slice()) {
Ok(j) => j,
Err(e) => fail!("Rust generated JSON is invalid: {:?}", e)
};
json.insert("crate".to_owned(), crate_json);
json.insert("plugins".to_owned(), json::Object(plugins_json));
json.insert("crate".to_strbuf(), crate_json);
json.insert("plugins".to_strbuf(), json::Object(plugins_json));
let mut file = try!(File::create(&dst));
try!(json::Object(json).to_writer(&mut file));
......
......@@ -54,7 +54,7 @@ pub struct Config {
pub trait ToBase64 {
/// Converts the value of `self` to a base64 value following the specified
/// format configuration, returning the owned string.
fn to_base64(&self, config: Config) -> ~str;
fn to_base64(&self, config: Config) -> StrBuf;
}
impl<'a> ToBase64 for &'a [u8] {
......@@ -73,7 +73,7 @@ impl<'a> ToBase64 for &'a [u8] {
* }
* ```
*/
fn to_base64(&self, config: Config) -> ~str {
fn to_base64(&self, config: Config) -> StrBuf {
let bytes = match config.char_set {
Standard => STANDARD_CHARS,
UrlSafe => URLSAFE_CHARS
......@@ -146,7 +146,7 @@ fn to_base64(&self, config: Config) -> ~str {
}
unsafe {
str::raw::from_utf8(v.as_slice()).to_owned()
str::raw::from_utf8(v.as_slice()).to_strbuf()
}
}
}
......@@ -195,7 +195,7 @@ impl<'a> FromBase64 for &'a str {
* fn main () {
* let hello_str = bytes!("Hello, World").to_base64(STANDARD);
* println!("base64 output: {}", hello_str);
* let res = hello_str.from_base64();
* let res = hello_str.as_slice().from_base64();
* if res.is_ok() {
* let opt_bytes = StrBuf::from_utf8(res.unwrap());
* if opt_bytes.is_ok() {
......@@ -267,34 +267,35 @@ mod tests {
#[test]
fn test_to_base64_basic() {
assert_eq!("".as_bytes().to_base64(STANDARD), "".to_owned());
assert_eq!("f".as_bytes().to_base64(STANDARD), "Zg==".to_owned());
assert_eq!("fo".as_bytes().to_base64(STANDARD), "Zm8=".to_owned());
assert_eq!("foo".as_bytes().to_base64(STANDARD), "Zm9v".to_owned());
assert_eq!("foob".as_bytes().to_base64(STANDARD), "Zm9vYg==".to_owned());
assert_eq!("fooba".as_bytes().to_base64(STANDARD), "Zm9vYmE=".to_owned());
assert_eq!("foobar".as_bytes().to_base64(STANDARD), "Zm9vYmFy".to_owned());
assert_eq!("".as_bytes().to_base64(STANDARD), "".to_strbuf());
assert_eq!("f".as_bytes().to_base64(STANDARD), "Zg==".to_strbuf());
assert_eq!("fo".as_bytes().to_base64(STANDARD), "Zm8=".to_strbuf());
assert_eq!("foo".as_bytes().to_base64(STANDARD), "Zm9v".to_strbuf());
assert_eq!("foob".as_bytes().to_base64(STANDARD), "Zm9vYg==".to_strbuf());
assert_eq!("fooba".as_bytes().to_base64(STANDARD), "Zm9vYmE=".to_strbuf());
assert_eq!("foobar".as_bytes().to_base64(STANDARD), "Zm9vYmFy".to_strbuf());
}
#[test]
fn test_to_base64_line_break() {
assert!(![0u8, ..1000].to_base64(Config {line_length: None, ..STANDARD})
.contains("\r\n"));
.as_slice()
.contains("\r\n"));
assert_eq!("foobar".as_bytes().to_base64(Config {line_length: Some(4),
..STANDARD}),
"Zm9v\r\nYmFy".to_owned());
"Zm9v\r\nYmFy".to_strbuf());
}
#[test]
fn test_to_base64_padding() {
assert_eq!("f".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zg".to_owned());
assert_eq!("fo".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zm8".to_owned());
assert_eq!("f".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zg".to_strbuf());
assert_eq!("fo".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zm8".to_strbuf());
}
#[test]
fn test_to_base64_url_safe() {
assert_eq!([251, 255].to_base64(URL_SAFE), "-_8".to_owned());
assert_eq!([251, 255].to_base64(STANDARD), "+/8=".to_owned());
assert_eq!([251, 255].to_base64(URL_SAFE), "-_8".to_strbuf());
assert_eq!([251, 255].to_base64(STANDARD), "+/8=".to_strbuf());
}
#[test]
......@@ -339,7 +340,12 @@ fn test_base64_random() {
for _ in range(0, 1000) {
let times = task_rng().gen_range(1u, 100);
let v = Vec::from_fn(times, |_| random::<u8>());
assert_eq!(v.as_slice().to_base64(STANDARD).from_base64().unwrap().as_slice(),
assert_eq!(v.as_slice()
.to_base64(STANDARD)
.as_slice()
.from_base64()
.unwrap()
.as_slice(),
v.as_slice());
}
}
......@@ -360,7 +366,7 @@ pub fn bench_from_base64(b: &mut Bencher) {
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
let sb = s.as_bytes().to_base64(STANDARD);
b.iter(|| {
sb.from_base64().unwrap();
sb.as_slice().from_base64().unwrap();
});
b.bytes = sb.len() as u64;
}
......
......@@ -34,8 +34,8 @@ pub fn as_str_slice<'a>(&'a self) -> &'a str {
str::from_utf8(self.data.slice(self.start, self.end)).unwrap()
}
pub fn as_str(&self) -> ~str {
self.as_str_slice().to_owned()
pub fn as_str(&self) -> StrBuf {
self.as_str_slice().to_strbuf()
}
}
......@@ -80,7 +80,7 @@ pub enum EbmlEncoderTag {
#[deriving(Show)]
pub enum Error {
IntTooBig(uint),
Expected(~str),
Expected(StrBuf),
IoError(io::IoError)
}
// --------------------------------------
......@@ -312,7 +312,10 @@ fn _check_label(&mut self, lbl: &str) -> DecodeResult<()> {
self.pos = r_doc.end;
let str = r_doc.as_str_slice();
if lbl != str {
return Err(Expected(format!("Expected label {} but found {}", lbl, str)));
return Err(Expected(format_strbuf!("Expected label \
{} but found {}",
lbl,
str)));
}
}
}
......@@ -322,7 +325,8 @@ fn _check_label(&mut self, lbl: &str) -> DecodeResult<()> {
fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> DecodeResult<Doc<'doc>> {
debug!(". next_doc(exp_tag={:?})", exp_tag);
if self.pos >= self.parent.end {
return Err(Expected(format!("no more documents in current node!")));
return Err(Expected(format_strbuf!("no more documents in \
current node!")));
}
let TaggedDoc { tag: r_tag, doc: r_doc } =
try!(doc_at(self.parent.data, self.pos));
......@@ -334,12 +338,18 @@ fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> DecodeResult<Doc<'doc>> {
r_doc.start,
r_doc.end);
if r_tag != (exp_tag as uint) {
return Err(Expected(format!("expected EBML doc with tag {:?} but found tag {:?}",
exp_tag, r_tag)));
return Err(Expected(format_strbuf!("expected EBML doc with \
tag {:?} but found tag \
{:?}",
exp_tag,
r_tag)));
}
if r_doc.end > self.parent.end {
return Err(Expected(format!("invalid EBML, child extends to {:#x}, parent to {:#x}",
r_doc.end, self.parent.end)));
return Err(Expected(format_strbuf!("invalid EBML, child \
extends to {:#x}, parent \
to {:#x}",
r_doc.end,
self.parent.end)));
}
self.pos = r_doc.end;
Ok(r_doc)
......@@ -433,7 +443,7 @@ fn read_f32(&mut self) -> DecodeResult<f32> {
fn read_char(&mut self) -> DecodeResult<char> {
Ok(char::from_u32(doc_as_u32(try!(self.next_doc(EsChar)))).unwrap())
}
fn read_str(&mut self) -> DecodeResult<~str> {
fn read_str(&mut self) -> DecodeResult<StrBuf> {
Ok(try!(self.next_doc(EsStr)).as_str())
}
......@@ -570,7 +580,10 @@ fn read_option<T>(&mut self,
match idx {
0 => f(this, false),
1 => f(this, true),
_ => Err(Expected(format!("Expected None or Some"))),
_ => {
Err(Expected(format_strbuf!("Expected None or \
Some")))
}
}
})
})
......
......@@ -16,7 +16,7 @@
pub trait ToHex {
/// Converts the value of `self` to a hex value, returning the owned
/// string.
fn to_hex(&self) -> ~str;
fn to_hex(&self) -> StrBuf;
}
static CHARS: &'static[u8] = bytes!("0123456789abcdef");
......@@ -37,7 +37,7 @@ impl<'a> ToHex for &'a [u8] {
* }
* ```
*/
fn to_hex(&self) -> ~str {
fn to_hex(&self) -> StrBuf {
let mut v = Vec::with_capacity(self.len() * 2);
for &byte in self.iter() {
v.push(CHARS[(byte >> 4) as uint]);
......@@ -45,7 +45,7 @@ fn to_hex(&self) -> ~str {
}
unsafe {
str::raw::from_utf8(v.as_slice()).to_owned()
str::raw::from_utf8(v.as_slice()).to_strbuf()
}
}
}
......@@ -94,7 +94,7 @@ impl<'a> FromHex for &'a str {
* fn main () {
* let hello_str = "Hello, World".as_bytes().to_hex();
* println!("{}", hello_str);
* let bytes = hello_str.from_hex().unwrap();
* let bytes = hello_str.as_slice().from_hex().unwrap();
* println!("{:?}", bytes);
* let result_str = StrBuf::from_utf8(bytes).unwrap();
* println!("{}", result_str);
......@@ -143,7 +143,7 @@ mod tests {
#[test]
pub fn test_to_hex() {
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172".to_owned());
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172".to_strbuf());
}
#[test]
......@@ -174,7 +174,8 @@ pub fn test_from_hex_ignores_whitespace() {
#[test]
pub fn test_to_hex_all_bytes() {
for i in range(0, 256) {
assert_eq!([i as u8].to_hex(), format!("{:02x}", i as uint));
assert_eq!([i as u8].to_hex(),
format_strbuf!("{:02x}", i as uint));
}
}
......@@ -202,7 +203,7 @@ pub fn bench_from_hex(b: &mut Bencher) {
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
let sb = s.as_bytes().to_hex();
b.iter(|| {
sb.from_hex().unwrap();
sb.as_slice().from_hex().unwrap();
});
b.bytes = sb.len() as u64;
}
......
此差异已折叠。
......@@ -108,7 +108,7 @@ pub trait Decoder<E> {
fn read_f64(&mut self) -> Result<f64, E>;
fn read_f32(&mut self) -> Result<f32, E>;
fn read_char(&mut self) -> Result<char, E>;
fn read_str(&mut self) -> Result<~str, E>;
fn read_str(&mut self) -> Result<StrBuf, E>;
// Compound types:
fn read_enum<T>(&mut self, name: &str, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
......@@ -301,18 +301,6 @@ fn encode(&self, s: &mut S) -> Result<(), E> {
}
}
impl<E, S:Encoder<E>> Encodable<S, E> for ~str {
fn encode(&self, s: &mut S) -> Result<(), E> {
s.emit_str(*self)
}
}
impl<E, D:Decoder<E>> Decodable<D, E> for ~str {
fn decode(d: &mut D) -> Result<~str, E> {
d.read_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())
......@@ -321,7 +309,7 @@ fn encode(&self, s: &mut S) -> Result<(), E> {
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())))
Ok(StrBuf::from_str(try!(d.read_str()).as_slice()))
}
}
......
......@@ -106,7 +106,7 @@ fn encode(&self, s: &mut S) -> Result<(), E> {
impl<D:Decoder<E>, E> Decodable<D, E> for Ident {
fn decode(d: &mut D) -> Result<Ident, E> {
Ok(str_to_ident(try!(d.read_str())))
Ok(str_to_ident(try!(d.read_str()).as_slice()))
}
}
......
......@@ -606,7 +606,8 @@ fn equiv(&self, other: & &'a str) -> bool {
impl<D:Decoder<E>, E> Decodable<D, E> for InternedString {
fn decode(d: &mut D) -> Result<InternedString, E> {
Ok(get_name(get_ident_interner().intern(try!(d.read_str()))))
Ok(get_name(get_ident_interner().intern(
try!(d.read_str()).as_slice())))
}
}
......
......@@ -1067,8 +1067,8 @@ fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
impl ToJson for Metric {
fn to_json(&self) -> json::Json {
let mut map = box TreeMap::new();
map.insert("value".to_owned(), json::Number(self.value));
map.insert("noise".to_owned(), json::Number(self.noise));
map.insert("value".to_strbuf(), json::Number(self.value));
map.insert("noise".to_strbuf(), json::Number(self.noise));
json::Object(map)
}
}
......@@ -1105,7 +1105,7 @@ pub fn save(&self, p: &Path) -> io::IoResult<()> {
// FIXME(pcwalton): Yuck.
let mut new_map = TreeMap::new();
for (ref key, ref value) in map.iter() {
new_map.insert(key.to_owned(), (*value).clone());
new_map.insert(key.to_strbuf(), (*value).clone());
}
new_map.to_json().to_pretty_writer(&mut file)
......
......@@ -189,7 +189,7 @@ pub fn tzset() {
/// Holds a calendar date and time broken down into its components (year, month, day, and so on),
/// also called a broken-down time value.
#[deriving(Clone, Eq, Encodable, Decodable, Show)]
#[deriving(Clone, Eq, Show)]
pub struct Tm {
/// Seconds after the minute – [0, 60]
pub tm_sec: i32,
......
......@@ -500,7 +500,7 @@ fn encode(&self, e: &mut T) -> Result<(), E> {
impl<T: Decoder<E>, E> Decodable<T, E> for Uuid {
/// Decode a UUID from a string
fn decode(d: &mut T) -> Result<Uuid, E> {
Ok(from_str(try!(d.read_str())).unwrap())
Ok(from_str(try!(d.read_str()).as_slice()).unwrap())
}
}
......
......@@ -192,7 +192,7 @@ fn save(&self) -> io::IoResult<()> {
// FIXME(pcwalton): Yuck.
let mut new_db_cache = TreeMap::new();
for (ref k, ref v) in self.db_cache.iter() {
new_db_cache.insert((*k).to_owned(), (*v).to_owned());
new_db_cache.insert((*k).to_strbuf(), (*v).to_strbuf());
}
new_db_cache.to_json().to_pretty_writer(&mut f)
......@@ -515,10 +515,13 @@ fn make_path(filename: StrBuf) -> Path {
let pth = pth.clone();
let contents = File::open(&pth).read_to_end().unwrap();
let file_content = from_utf8(contents.as_slice()).unwrap().to_owned();
let file_content = from_utf8(contents.as_slice()).unwrap()
.to_strbuf();
// FIXME (#9639): This needs to handle non-utf8 paths
prep.declare_input("file", pth.as_str().unwrap(), file_content);
prep.declare_input("file",
pth.as_str().unwrap(),
file_content.as_slice());
prep.exec(proc(_exe) {
let out = make_path("foo.o".to_strbuf());
let compiler = if cfg!(windows) {"gcc"} else {"cc"};
......@@ -528,7 +531,7 @@ fn make_path(filename: StrBuf) -> Path {
// Could run sub-rules inside here.
// FIXME (#9639): This needs to handle non-utf8 paths
out.as_str().unwrap().to_owned()
out.as_str().unwrap().to_strbuf()
})
});
......
......@@ -23,7 +23,7 @@ enum object {
fn lookup(table: Box<json::Object>, key: StrBuf, default: StrBuf) -> StrBuf
{
match table.find(&key.to_owned()) {
match table.find(&key.to_strbuf()) {
option::Some(&json::String(ref s)) => {
(*s).to_strbuf()
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册