Skip to content

Commit 8bc286f

Browse files
committed
auto merge of #15238 : aochagavia/rust/json, r=alexcrichton
### Breaking changes: * **Removed unnecessary `box` from enum variant (`Object(Box<Object>)` becomes `Object(Object)`)** * **Deprecated `Encoder::str_encode`** ### Other changes: * Tried to make the code more idiomatic * Renamed the `wr` field of the `Encoder` and `PrettyEncoder` structs to `writer` * Replaced some `from_utf8` by `from_utf8_owned` to avoid unnecessary allocations * Removed unnecessary `unsafe` code * Added `encode` and `decode` shortcut functions * Implemented `FromStr` for `Json` * Implemented `ToJson` for tuples of arity up to 12 * Fixed some details in the documentation ### Questions * ~~The `encode` shortcut function does the same as the `Encoder::str_encode` function. It seems wrong to me that two functions do exactly the same. Should we deprecate `Encoder::str_encode`?~~ * ~~Do we really want the ToJson trait for tuples? At the moment we have it for (), (A, B), (A, B, C). I would like to remove them.~~ * ~~We are using `String` as key in the `TreeMap` representing a `Json` object. It would be better to use `&str`, but this would require to annotate lots of lifetimes. Is there any easy solution for this?~~ * There is a lot of duplicated code (`PrettyEncoder` copies about 50 lines from `Encoder`). In an OO language this could be solved very elegantly by using inheritance and overriding. What can we do here to reduce the amount of boilerplate? [breaking-change]
2 parents a345c54 + c3cf3b3 commit 8bc286f

File tree

4 files changed

+325
-483
lines changed

4 files changed

+325
-483
lines changed

src/librustdoc/lib.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,17 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
408408
// "crate": { parsed crate ... },
409409
// "plugins": { output of plugins ... }
410410
// }
411-
let mut json = box std::collections::TreeMap::new();
412-
json.insert("schema".to_string(),
413-
json::String(SCHEMA_VERSION.to_string()));
414-
let plugins_json = box res.move_iter()
415-
.filter_map(|opt| {
416-
match opt {
417-
None => None,
418-
Some((string, json)) => {
419-
Some((string.to_string(), json))
420-
}
411+
let mut json = std::collections::TreeMap::new();
412+
json.insert("schema".to_string(), json::String(SCHEMA_VERSION.to_string()));
413+
let plugins_json = res.move_iter()
414+
.filter_map(|opt| {
415+
match opt {
416+
None => None,
417+
Some((string, json)) => {
418+
Some((string.to_string(), json))
421419
}
422-
}).collect();
420+
}
421+
}).collect();
423422

424423
// FIXME #8335: yuck, Rust -> str -> JSON round trip! No way to .encode
425424
// straight to the Rust JSON representation.
@@ -429,7 +428,7 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
429428
let mut encoder = json::Encoder::new(&mut w as &mut io::Writer);
430429
krate.encode(&mut encoder).unwrap();
431430
}
432-
str::from_utf8(w.unwrap().as_slice()).unwrap().to_string()
431+
str::from_utf8_owned(w.unwrap()).unwrap()
433432
};
434433
let crate_json = match json::from_str(crate_json_str.as_slice()) {
435434
Ok(j) => j,
@@ -440,6 +439,5 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
440439
json.insert("plugins".to_string(), json::Object(plugins_json));
441440

442441
let mut file = try!(File::create(&dst));
443-
try!(json::Object(json).to_writer(&mut file));
444-
Ok(())
442+
json::Object(json).to_writer(&mut file)
445443
}

0 commit comments

Comments
 (0)