Fix rustc_serialize unit tests

This commit is contained in:
Matthew Jasper 2020-07-18 20:14:50 +01:00
parent ae7951ed43
commit c4f91bb281
5 changed files with 34 additions and 23 deletions

View File

@ -3877,6 +3877,7 @@ name = "rustc_serialize"
version = "0.0.0"
dependencies = [
"indexmap",
"rustc_macros",
"smallvec 1.4.0",
]

View File

@ -11,3 +11,6 @@ path = "lib.rs"
[dependencies]
indexmap = "1"
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
[dev-dependencies]
rustc_macros = { path = "../librustc_macros" }

View File

@ -47,17 +47,17 @@
//!
//! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via
//! the serialization API.
//! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait.
//! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait.
//! To be able to encode a piece of data, it must implement the `serialize::Encodable` trait.
//! To be able to decode a piece of data, it must implement the `serialize::Decodable` trait.
//! The Rust compiler provides an annotation to automatically generate the code for these traits:
//! `#[derive(RustcDecodable, RustcEncodable)]`
//! `#[derive(Decodable, Encodable)]`
//!
//! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects.
//! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value.
//! A `json::Json` value can be encoded as a string or buffer using the functions described above.
//! You can also use the `json::Encoder` object, which implements the `Encoder` trait.
//!
//! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory.
//! When using `ToJson` the `Encodable` trait implementation is not mandatory.
//!
//! # Examples of use
//!
@ -68,10 +68,11 @@
//!
//! ```rust
//! # #![feature(rustc_private)]
//! use rustc_macros::{Decodable, Encodable};
//! use rustc_serialize::json;
//!
//! // Automatically generate `Decodable` and `Encodable` trait implementations
//! #[derive(RustcDecodable, RustcEncodable)]
//! #[derive(Decodable, Encodable)]
//! pub struct TestStruct {
//! data_int: u8,
//! data_str: String,
@ -100,6 +101,7 @@
//!
//! ```rust
//! # #![feature(rustc_private)]
//! use rustc_macros::Encodable;
//! use rustc_serialize::json::{self, ToJson, Json};
//!
//! // A custom data structure
@ -115,8 +117,8 @@
//! }
//! }
//!
//! // Only generate `RustcEncodable` trait implementation
//! #[derive(RustcEncodable)]
//! // Only generate `Encodable` trait implementation
//! #[derive(Encodable)]
//! pub struct ComplexNumRecord {
//! uid: u8,
//! dsc: String,
@ -137,11 +139,12 @@
//!
//! ```rust
//! # #![feature(rustc_private)]
//! use rustc_macros::Decodable;
//! use std::collections::BTreeMap;
//! use rustc_serialize::json::{self, Json, ToJson};
//!
//! // Only generate `RustcDecodable` trait implementation
//! #[derive(RustcDecodable)]
//! // Only generate `Decodable` trait implementation
//! #[derive(Decodable)]
//! pub struct TestStruct {
//! data_int: u8,
//! data_str: String,

View File

@ -9,6 +9,7 @@ use json::{
from_str, DecodeResult, Decoder, DecoderError, Encoder, EncoderError, Json, JsonEvent, Parser,
StackElement,
};
use rustc_macros::{Decodable, Encodable};
use rustc_serialize::json;
use rustc_serialize::{Decodable, Encodable};
@ -17,7 +18,7 @@ use std::io::prelude::*;
use std::string;
use Animal::*;
#[derive(RustcDecodable, Eq, PartialEq, Debug)]
#[derive(Decodable, Eq, PartialEq, Debug)]
struct OptionData {
opt: Option<usize>,
}
@ -48,20 +49,20 @@ fn test_decode_option_malformed() {
);
}
#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
#[derive(PartialEq, Encodable, Decodable, Debug)]
enum Animal {
Dog,
Frog(string::String, isize),
}
#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
#[derive(PartialEq, Encodable, Decodable, Debug)]
struct Inner {
a: (),
b: usize,
c: Vec<string::String>,
}
#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
#[derive(PartialEq, Encodable, Decodable, Debug)]
struct Outer {
inner: Vec<Inner>,
}
@ -568,7 +569,7 @@ fn test_decode_struct() {
);
}
#[derive(RustcDecodable)]
#[derive(Decodable)]
struct FloatStruct {
f: f64,
a: Vec<f64>,
@ -616,7 +617,7 @@ fn test_multiline_errors() {
assert_eq!(from_str("{\n \"foo\":\n \"bar\""), Err(SyntaxError(EOFWhileParsingObject, 3, 8)));
}
#[derive(RustcDecodable)]
#[derive(Decodable)]
#[allow(dead_code)]
struct DecodeStruct {
x: f64,
@ -624,12 +625,12 @@ struct DecodeStruct {
z: string::String,
w: Vec<DecodeStruct>,
}
#[derive(RustcDecodable)]
#[derive(Decodable)]
enum DecodeEnum {
A(f64),
B(string::String),
}
fn check_err<T: Decodable>(to_parse: &'static str, expected: DecoderError) {
fn check_err<T: Decodable<Decoder>>(to_parse: &'static str, expected: DecoderError) {
let res: DecodeResult<T> = match from_str(to_parse) {
Err(e) => Err(ParseError(e)),
Ok(json) => Decodable::decode(&mut Decoder::new(json)),
@ -933,7 +934,7 @@ fn test_prettyencoder_indent_level_param() {
#[test]
fn test_hashmap_with_enum_key() {
use std::collections::HashMap;
#[derive(RustcEncodable, Eq, Hash, PartialEq, RustcDecodable, Debug)]
#[derive(Encodable, Eq, Hash, PartialEq, Decodable, Debug)]
enum Enum {
Foo,
#[allow(dead_code)]
@ -1254,7 +1255,7 @@ fn test_to_json() {
#[test]
fn test_encode_hashmap_with_arbitrary_key() {
use std::collections::HashMap;
#[derive(PartialEq, Eq, Hash, RustcEncodable)]
#[derive(PartialEq, Eq, Hash, Encodable)]
struct ArbitraryType(usize);
let mut hm: HashMap<ArbitraryType, bool> = HashMap::new();
hm.insert(ArbitraryType(1), true);

View File

@ -1,10 +1,11 @@
#![allow(rustc::internal)]
use rustc_macros::{Decodable, Encodable};
use rustc_serialize::opaque::{Decoder, Encoder};
use rustc_serialize::{Decodable, Encodable};
use std::fmt::Debug;
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
struct Struct {
a: (),
b: u8,
@ -27,11 +28,13 @@ struct Struct {
q: Option<u32>,
}
fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) {
fn check_round_trip<T: Encodable<Encoder> + for<'a> Decodable<Decoder<'a>> + PartialEq + Debug>(
values: Vec<T>,
) {
let mut encoder = Encoder::new(Vec::new());
for value in &values {
Encodable::encode(&value, &mut encoder).unwrap();
Encodable::encode(value, &mut encoder).unwrap();
}
let data = encoder.into_inner();
@ -225,7 +228,7 @@ fn test_struct() {
}]);
}
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
enum Enum {
Variant1,
Variant2(usize, f32),