1
Fork 0

Rollup merge of #58318 - taiki-e:libserialize-2018, r=Centril

libserialize => 2018

Transitions `libserialize` to Rust 2018; cc #58099

This includes a commit from #58252 (thanks @h-michael!)

r? @Centril
This commit is contained in:
Mazdak Farrokhzad 2019-02-12 04:42:24 +01:00 committed by GitHub
commit 308c07bc3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 1607 additions and 1609 deletions

View file

@ -2,6 +2,7 @@
authors = ["The Rust Project Developers"]
name = "serialize"
version = "0.0.0"
edition = "2018"
[lib]
name = "serialize"

View file

@ -2,7 +2,7 @@
use std::hash::{Hash, BuildHasher};
use {Decodable, Encodable, Decoder, Encoder};
use crate::{Decodable, Encodable, Decoder, Encoder};
use std::collections::{LinkedList, VecDeque, BTreeMap, BTreeSet, HashMap, HashSet};
use std::rc::Rc;
use std::sync::Arc;

View file

@ -60,7 +60,7 @@ pub enum FromHexError {
}
impl fmt::Display for FromHexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
InvalidHexCharacter(ch, idx) =>
write!(f, "Invalid character '{}' at position {}", ch, idx),
@ -145,8 +145,8 @@ impl FromHex for str {
#[cfg(test)]
mod tests {
extern crate test;
use self::test::Bencher;
use hex::{FromHex, ToHex};
use test::Bencher;
use crate::hex::{FromHex, ToHex};
#[test]
pub fn test_to_hex() {

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,8 @@ Core encoding and decoding interfaces.
html_playground_url = "https://play.rust-lang.org/",
test(attr(allow(unused_variables), deny(warnings))))]
#![deny(rust_2018_idioms)]
#![feature(box_syntax)]
#![feature(core_intrinsics)]
#![feature(specialization)]
@ -20,8 +22,6 @@ pub use self::serialize::{Decoder, Encoder, Decodable, Encodable};
pub use self::serialize::{SpecializationError, SpecializedEncoder, SpecializedDecoder};
pub use self::serialize::{UseSpecializedEncodable, UseSpecializedDecodable};
extern crate smallvec;
mod serialize;
mod collection_impls;
@ -30,7 +30,3 @@ pub mod json;
pub mod opaque;
pub mod leb128;
mod rustc_serialize {
pub use serialize::*;
}

View file

@ -1,6 +1,6 @@
use leb128::{self, read_signed_leb128, write_signed_leb128};
use crate::leb128::{self, read_signed_leb128, write_signed_leb128};
use crate::serialize;
use std::borrow::Cow;
use serialize;
// -----------------------------------------------------------------------------
// Encoder
@ -312,7 +312,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
}
#[inline]
fn read_str(&mut self) -> Result<Cow<str>, Self::Error> {
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error> {
let len = self.read_usize()?;
let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();
self.position += len;
@ -324,288 +324,3 @@ impl<'a> serialize::Decoder for Decoder<'a> {
err.to_string()
}
}
#[cfg(test)]
mod tests {
use serialize::{Encodable, Decodable};
use std::fmt::Debug;
use super::{Encoder, Decoder};
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
struct Struct {
a: (),
b: u8,
c: u16,
d: u32,
e: u64,
f: usize,
g: i8,
h: i16,
i: i32,
j: i64,
k: isize,
l: char,
m: String,
n: f32,
o: f64,
p: bool,
q: Option<u32>,
}
fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) {
let mut encoder = Encoder::new(Vec::new());
for value in &values {
Encodable::encode(&value, &mut encoder).unwrap();
}
let data = encoder.into_inner();
let mut decoder = Decoder::new(&data[..], 0);
for value in values {
let decoded = Decodable::decode(&mut decoder).unwrap();
assert_eq!(value, decoded);
}
}
#[test]
fn test_unit() {
check_round_trip(vec![(), (), (), ()]);
}
#[test]
fn test_u8() {
let mut vec = vec![];
for i in ::std::u8::MIN..::std::u8::MAX {
vec.push(i);
}
check_round_trip(vec);
}
#[test]
fn test_u16() {
for i in ::std::u16::MIN..::std::u16::MAX {
check_round_trip(vec![1, 2, 3, i, i, i]);
}
}
#[test]
fn test_u32() {
check_round_trip(vec![1, 2, 3, ::std::u32::MIN, 0, 1, ::std::u32::MAX, 2, 1]);
}
#[test]
fn test_u64() {
check_round_trip(vec![1, 2, 3, ::std::u64::MIN, 0, 1, ::std::u64::MAX, 2, 1]);
}
#[test]
fn test_usize() {
check_round_trip(vec![1, 2, 3, ::std::usize::MIN, 0, 1, ::std::usize::MAX, 2, 1]);
}
#[test]
fn test_i8() {
let mut vec = vec![];
for i in ::std::i8::MIN..::std::i8::MAX {
vec.push(i);
}
check_round_trip(vec);
}
#[test]
fn test_i16() {
for i in ::std::i16::MIN..::std::i16::MAX {
check_round_trip(vec![-1, 2, -3, i, i, i, 2]);
}
}
#[test]
fn test_i32() {
check_round_trip(vec![-1, 2, -3, ::std::i32::MIN, 0, 1, ::std::i32::MAX, 2, 1]);
}
#[test]
fn test_i64() {
check_round_trip(vec![-1, 2, -3, ::std::i64::MIN, 0, 1, ::std::i64::MAX, 2, 1]);
}
#[test]
fn test_isize() {
check_round_trip(vec![-1, 2, -3, ::std::isize::MIN, 0, 1, ::std::isize::MAX, 2, 1]);
}
#[test]
fn test_bool() {
check_round_trip(vec![false, true, true, false, false]);
}
#[test]
fn test_f32() {
let mut vec = vec![];
for i in -100..100 {
vec.push((i as f32) / 3.0);
}
check_round_trip(vec);
}
#[test]
fn test_f64() {
let mut vec = vec![];
for i in -100..100 {
vec.push((i as f64) / 3.0);
}
check_round_trip(vec);
}
#[test]
fn test_char() {
let vec = vec!['a', 'b', 'c', 'd', 'A', 'X', ' ', '#', 'Ö', 'Ä', 'µ', '€'];
check_round_trip(vec);
}
#[test]
fn test_string() {
let vec = vec!["abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
"abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string()];
check_round_trip(vec);
}
#[test]
fn test_option() {
check_round_trip(vec![Some(-1i8)]);
check_round_trip(vec![Some(-2i16)]);
check_round_trip(vec![Some(-3i32)]);
check_round_trip(vec![Some(-4i64)]);
check_round_trip(vec![Some(-5isize)]);
let none_i8: Option<i8> = None;
check_round_trip(vec![none_i8]);
let none_i16: Option<i16> = None;
check_round_trip(vec![none_i16]);
let none_i32: Option<i32> = None;
check_round_trip(vec![none_i32]);
let none_i64: Option<i64> = None;
check_round_trip(vec![none_i64]);
let none_isize: Option<isize> = None;
check_round_trip(vec![none_isize]);
}
#[test]
fn test_struct() {
check_round_trip(vec![Struct {
a: (),
b: 10,
c: 11,
d: 12,
e: 13,
f: 14,
g: 15,
h: 16,
i: 17,
j: 18,
k: 19,
l: 'x',
m: "abc".to_string(),
n: 20.5,
o: 21.5,
p: false,
q: None,
}]);
check_round_trip(vec![Struct {
a: (),
b: 101,
c: 111,
d: 121,
e: 131,
f: 141,
g: -15,
h: -16,
i: -17,
j: -18,
k: -19,
l: 'y',
m: "def".to_string(),
n: -20.5,
o: -21.5,
p: true,
q: Some(1234567),
}]);
}
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
enum Enum {
Variant1,
Variant2(usize, f32),
Variant3 {
a: i32,
b: char,
c: bool,
},
}
#[test]
fn test_enum() {
check_round_trip(vec![Enum::Variant1,
Enum::Variant2(1, 2.5),
Enum::Variant3 {
a: 3,
b: 'b',
c: false,
},
Enum::Variant3 {
a: -4,
b: 'f',
c: true,
}]);
}
#[test]
fn test_sequence() {
let mut vec = vec![];
for i in -100i64..100i64 {
vec.push(i * 100000);
}
check_round_trip(vec![vec]);
}
#[test]
fn test_hash_map() {
use std::collections::HashMap;
let mut map = HashMap::new();
for i in -100i64..100i64 {
map.insert(i * 100000, i * 10000);
}
check_round_trip(vec![map]);
}
#[test]
fn test_tuples() {
check_round_trip(vec![('x', (), false, 0.5f32)]);
check_round_trip(vec![(9i8, 10u16, 1.5f64)]);
check_round_trip(vec![(-12i16, 11u8, 12usize)]);
check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]);
check_round_trip(vec![(String::new(), "some string".to_string())]);
}
}

View file

@ -175,7 +175,7 @@ pub trait Decoder {
fn read_f64(&mut self) -> Result<f64, Self::Error>;
fn read_f32(&mut self) -> Result<f32, Self::Error>;
fn read_char(&mut self) -> Result<char, Self::Error>;
fn read_str(&mut self) -> Result<Cow<str>, Self::Error>;
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error>;
// Compound types:
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,282 @@
extern crate serialize as rustc_serialize;
use rustc_serialize::{Encodable, Decodable};
use rustc_serialize::opaque::{Encoder, Decoder};
use std::fmt::Debug;
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
struct Struct {
a: (),
b: u8,
c: u16,
d: u32,
e: u64,
f: usize,
g: i8,
h: i16,
i: i32,
j: i64,
k: isize,
l: char,
m: String,
n: f32,
o: f64,
p: bool,
q: Option<u32>,
}
fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) {
let mut encoder = Encoder::new(Vec::new());
for value in &values {
Encodable::encode(&value, &mut encoder).unwrap();
}
let data = encoder.into_inner();
let mut decoder = Decoder::new(&data[..], 0);
for value in values {
let decoded = Decodable::decode(&mut decoder).unwrap();
assert_eq!(value, decoded);
}
}
#[test]
fn test_unit() {
check_round_trip(vec![(), (), (), ()]);
}
#[test]
fn test_u8() {
let mut vec = vec![];
for i in ::std::u8::MIN..::std::u8::MAX {
vec.push(i);
}
check_round_trip(vec);
}
#[test]
fn test_u16() {
for i in ::std::u16::MIN..::std::u16::MAX {
check_round_trip(vec![1, 2, 3, i, i, i]);
}
}
#[test]
fn test_u32() {
check_round_trip(vec![1, 2, 3, ::std::u32::MIN, 0, 1, ::std::u32::MAX, 2, 1]);
}
#[test]
fn test_u64() {
check_round_trip(vec![1, 2, 3, ::std::u64::MIN, 0, 1, ::std::u64::MAX, 2, 1]);
}
#[test]
fn test_usize() {
check_round_trip(vec![1, 2, 3, ::std::usize::MIN, 0, 1, ::std::usize::MAX, 2, 1]);
}
#[test]
fn test_i8() {
let mut vec = vec![];
for i in ::std::i8::MIN..::std::i8::MAX {
vec.push(i);
}
check_round_trip(vec);
}
#[test]
fn test_i16() {
for i in ::std::i16::MIN..::std::i16::MAX {
check_round_trip(vec![-1, 2, -3, i, i, i, 2]);
}
}
#[test]
fn test_i32() {
check_round_trip(vec![-1, 2, -3, ::std::i32::MIN, 0, 1, ::std::i32::MAX, 2, 1]);
}
#[test]
fn test_i64() {
check_round_trip(vec![-1, 2, -3, ::std::i64::MIN, 0, 1, ::std::i64::MAX, 2, 1]);
}
#[test]
fn test_isize() {
check_round_trip(vec![-1, 2, -3, ::std::isize::MIN, 0, 1, ::std::isize::MAX, 2, 1]);
}
#[test]
fn test_bool() {
check_round_trip(vec![false, true, true, false, false]);
}
#[test]
fn test_f32() {
let mut vec = vec![];
for i in -100..100 {
vec.push((i as f32) / 3.0);
}
check_round_trip(vec);
}
#[test]
fn test_f64() {
let mut vec = vec![];
for i in -100..100 {
vec.push((i as f64) / 3.0);
}
check_round_trip(vec);
}
#[test]
fn test_char() {
let vec = vec!['a', 'b', 'c', 'd', 'A', 'X', ' ', '#', 'Ö', 'Ä', 'µ', '€'];
check_round_trip(vec);
}
#[test]
fn test_string() {
let vec = vec!["abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
"abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string()];
check_round_trip(vec);
}
#[test]
fn test_option() {
check_round_trip(vec![Some(-1i8)]);
check_round_trip(vec![Some(-2i16)]);
check_round_trip(vec![Some(-3i32)]);
check_round_trip(vec![Some(-4i64)]);
check_round_trip(vec![Some(-5isize)]);
let none_i8: Option<i8> = None;
check_round_trip(vec![none_i8]);
let none_i16: Option<i16> = None;
check_round_trip(vec![none_i16]);
let none_i32: Option<i32> = None;
check_round_trip(vec![none_i32]);
let none_i64: Option<i64> = None;
check_round_trip(vec![none_i64]);
let none_isize: Option<isize> = None;
check_round_trip(vec![none_isize]);
}
#[test]
fn test_struct() {
check_round_trip(vec![Struct {
a: (),
b: 10,
c: 11,
d: 12,
e: 13,
f: 14,
g: 15,
h: 16,
i: 17,
j: 18,
k: 19,
l: 'x',
m: "abc".to_string(),
n: 20.5,
o: 21.5,
p: false,
q: None,
}]);
check_round_trip(vec![Struct {
a: (),
b: 101,
c: 111,
d: 121,
e: 131,
f: 141,
g: -15,
h: -16,
i: -17,
j: -18,
k: -19,
l: 'y',
m: "def".to_string(),
n: -20.5,
o: -21.5,
p: true,
q: Some(1234567),
}]);
}
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
enum Enum {
Variant1,
Variant2(usize, f32),
Variant3 {
a: i32,
b: char,
c: bool,
},
}
#[test]
fn test_enum() {
check_round_trip(vec![Enum::Variant1,
Enum::Variant2(1, 2.5),
Enum::Variant3 {
a: 3,
b: 'b',
c: false,
},
Enum::Variant3 {
a: -4,
b: 'f',
c: true,
}]);
}
#[test]
fn test_sequence() {
let mut vec = vec![];
for i in -100i64..100i64 {
vec.push(i * 100000);
}
check_round_trip(vec![vec]);
}
#[test]
fn test_hash_map() {
use std::collections::HashMap;
let mut map = HashMap::new();
for i in -100i64..100i64 {
map.insert(i * 100000, i * 10000);
}
check_round_trip(vec![map]);
}
#[test]
fn test_tuples() {
check_round_trip(vec![('x', (), false, 0.5f32)]);
check_round_trip(vec![(9i8, 10u16, 1.5f64)]);
check_round_trip(vec![(-12i16, 11u8, 12usize)]);
check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]);
check_round_trip(vec![(String::new(), "some string".to_string())]);
}