diff options
| author | bors <bors@rust-lang.org> | 2022-06-03 17:55:02 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-06-03 17:55:02 +0000 |
| commit | 7e9b92cb43a489b34e2bcb8d21f36198e02eedbc (patch) | |
| tree | e2731c205d38c4d2c11fdb1789add435a21bab23 /compiler/rustc_target/src/json.rs | |
| parent | 9a74608543d499bcc7dd505e195e8bfab9447315 (diff) | |
| parent | 5cc3593c17360edd92977301ca66551a45119619 (diff) | |
| download | rust-7e9b92cb43a489b34e2bcb8d21f36198e02eedbc.tar.gz rust-7e9b92cb43a489b34e2bcb8d21f36198e02eedbc.zip | |
Auto merge of #85993 - bjorn3:serde_json, r=wesleywiser
Remove all json handling from rustc_serialize Json is now handled using serde_json. Where appropriate I have replaced json usage with binary serialization (rmeta files) or manual string formatting (emcc linker arg generation). This allowed for removing and simplifying a lot of code, which hopefully results in faster serialization/deserialization and faster compiles of rustc itself. Where sensible we now use serde. Metadata and incr cache serialization keeps using a heavily modified (compared to crates.io) rustc-serialize version that in the future could probably be extended with zero-copy deserialization or other perf tricks that serde can't support due to supporting more than one serialization format. Note that I had to remove `-Zast-json` and `-Zast-json-noexpand` as the relevant AST types don't implement `serde::Serialize`. Fixes #40177 See also https://github.com/rust-lang/compiler-team/issues/418
Diffstat (limited to 'compiler/rustc_target/src/json.rs')
| -rw-r--r-- | compiler/rustc_target/src/json.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/compiler/rustc_target/src/json.rs b/compiler/rustc_target/src/json.rs new file mode 100644 index 00000000000..b5d92635212 --- /dev/null +++ b/compiler/rustc_target/src/json.rs @@ -0,0 +1,91 @@ +use std::borrow::Cow; +use std::collections::BTreeMap; + +pub use serde_json::Value as Json; +use serde_json::{Map, Number}; + +pub trait ToJson { + fn to_json(&self) -> Json; +} + +impl ToJson for Json { + fn to_json(&self) -> Json { + self.clone() + } +} + +macro_rules! to_json_impl_num { + ($($t:ty), +) => ( + $(impl ToJson for $t { + fn to_json(&self) -> Json { + Json::Number(Number::from(*self)) + } + })+ + ) +} + +to_json_impl_num! { isize, i8, i16, i32, i64, usize, u8, u16, u32, u64 } + +impl ToJson for bool { + fn to_json(&self) -> Json { + Json::Bool(*self) + } +} + +impl ToJson for str { + fn to_json(&self) -> Json { + Json::String(self.to_owned()) + } +} + +impl ToJson for String { + fn to_json(&self) -> Json { + Json::String(self.to_owned()) + } +} + +impl<'a> ToJson for Cow<'a, str> { + fn to_json(&self) -> Json { + Json::String(self.to_string()) + } +} + +impl<A: ToJson> ToJson for [A] { + fn to_json(&self) -> Json { + Json::Array(self.iter().map(|elt| elt.to_json()).collect()) + } +} + +impl<A: ToJson> ToJson for Vec<A> { + fn to_json(&self) -> Json { + Json::Array(self.iter().map(|elt| elt.to_json()).collect()) + } +} + +impl<'a, A: ToJson> ToJson for Cow<'a, [A]> +where + [A]: ToOwned, +{ + fn to_json(&self) -> Json { + Json::Array(self.iter().map(|elt| elt.to_json()).collect()) + } +} + +impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> { + fn to_json(&self) -> Json { + let mut d = Map::new(); + for (key, value) in self { + d.insert(key.to_string(), value.to_json()); + } + Json::Object(d) + } +} + +impl<A: ToJson> ToJson for Option<A> { + fn to_json(&self) -> Json { + match *self { + None => Json::Null, + Some(ref value) => value.to_json(), + } + } +} |
