From 42904b02191a6ee34373d04cf439a89badfae508 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 9 Feb 2022 18:17:14 -0500 Subject: [PATCH] Delete Decoder::read_seq --- .../src/dep_graph/serialized.rs | 20 +++---- .../rustc_serialize/src/collection_impls.rs | 52 +++++++++---------- compiler/rustc_serialize/src/serialize.rs | 46 +++++++--------- 3 files changed, 54 insertions(+), 64 deletions(-) diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index 2ac5d5431a5..9778f77384c 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -130,16 +130,16 @@ impl<'a, K: DepKind + Decodable>> Decodable = diff --git a/compiler/rustc_serialize/src/collection_impls.rs b/compiler/rustc_serialize/src/collection_impls.rs index 38f6b402fc5..41faf4f70ac 100644 --- a/compiler/rustc_serialize/src/collection_impls.rs +++ b/compiler/rustc_serialize/src/collection_impls.rs @@ -18,7 +18,8 @@ impl>> Encodable for SmallVec { impl>> Decodable for SmallVec { fn decode(d: &mut D) -> SmallVec { - d.read_seq(|d, len| (0..len).map(|_| Decodable::decode(d)).collect()) + let len = d.read_usize(); + (0..len).map(|_| Decodable::decode(d)).collect() } } @@ -35,7 +36,8 @@ impl> Encodable for LinkedList { impl> Decodable for LinkedList { fn decode(d: &mut D) -> LinkedList { - d.read_seq(|d, len| (0..len).map(|_| Decodable::decode(d)).collect()) + let len = d.read_usize(); + (0..len).map(|_| Decodable::decode(d)).collect() } } @@ -52,7 +54,8 @@ impl> Encodable for VecDeque { impl> Decodable for VecDeque { fn decode(d: &mut D) -> VecDeque { - d.read_seq(|d, len| (0..len).map(|_| Decodable::decode(d)).collect()) + let len = d.read_usize(); + (0..len).map(|_| Decodable::decode(d)).collect() } } @@ -109,13 +112,12 @@ where T: Decodable + PartialEq + Ord, { fn decode(d: &mut D) -> BTreeSet { - d.read_seq(|d, len| { - let mut set = BTreeSet::new(); - for _ in 0..len { - set.insert(Decodable::decode(d)); - } - set - }) + let len = d.read_usize(); + let mut set = BTreeSet::new(); + for _ in 0..len { + set.insert(Decodable::decode(d)); + } + set } } @@ -187,14 +189,13 @@ where S: BuildHasher + Default, { fn decode(d: &mut D) -> HashSet { - d.read_seq(|d, len| { - let state = Default::default(); - let mut set = HashSet::with_capacity_and_hasher(len, state); - for _ in 0..len { - set.insert(Decodable::decode(d)); - } - set - }) + let len = d.read_usize(); + let state = Default::default(); + let mut set = HashSet::with_capacity_and_hasher(len, state); + for _ in 0..len { + set.insert(Decodable::decode(d)); + } + set } } @@ -256,14 +257,13 @@ where S: BuildHasher + Default, { fn decode(d: &mut D) -> indexmap::IndexSet { - d.read_seq(|d, len| { - let state = Default::default(); - let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state); - for _ in 0..len { - set.insert(Decodable::decode(d)); - } - set - }) + let len = d.read_usize(); + let state = Default::default(); + let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state); + for _ in 0..len { + set.insert(Decodable::decode(d)); + } + set } } diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 10aec0294d0..e36e3033fcc 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -201,14 +201,6 @@ pub trait Decoder { fn read_str(&mut self) -> Cow<'_, str>; fn read_raw_bytes_into(&mut self, s: &mut [u8]); - fn read_seq(&mut self, f: F) -> T - where - F: FnOnce(&mut Self, usize) -> T, - { - let len = self.read_usize(); - f(self, len) - } - fn read_map(&mut self, f: F) -> T where F: FnOnce(&mut Self, usize) -> T, @@ -397,19 +389,18 @@ impl> Encodable for Vec { impl> Decodable for Vec { default fn decode(d: &mut D) -> Vec { - d.read_seq(|d, len| { - // SAFETY: we set the capacity in advance, only write elements, and - // only set the length at the end once the writing has succeeded. - let mut vec = Vec::with_capacity(len); - unsafe { - let ptr: *mut T = vec.as_mut_ptr(); - for i in 0..len { - std::ptr::write(ptr.offset(i as isize), Decodable::decode(d)); - } - vec.set_len(len); + let len = d.read_usize(); + // SAFETY: we set the capacity in advance, only write elements, and + // only set the length at the end once the writing has succeeded. + let mut vec = Vec::with_capacity(len); + unsafe { + let ptr: *mut T = vec.as_mut_ptr(); + for i in 0..len { + std::ptr::write(ptr.offset(i as isize), Decodable::decode(d)); } - vec - }) + vec.set_len(len); + } + vec } } @@ -422,14 +413,13 @@ impl, const N: usize> Encodable for [T; N] { impl Decodable for [u8; N] { fn decode(d: &mut D) -> [u8; N] { - d.read_seq(|d, len| { - assert!(len == N); - let mut v = [0u8; N]; - for i in 0..len { - v[i] = Decodable::decode(d); - } - v - }) + let len = d.read_usize(); + assert!(len == N); + let mut v = [0u8; N]; + for i in 0..len { + v[i] = Decodable::decode(d); + } + v } }