summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_serialize
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /compiler/rustc_serialize
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_serialize')
-rw-r--r--compiler/rustc_serialize/Cargo.toml4
-rw-r--r--compiler/rustc_serialize/src/collection_impls.rs279
-rw-r--r--compiler/rustc_serialize/src/lib.rs20
-rw-r--r--compiler/rustc_serialize/src/serialize.rs262
4 files changed, 247 insertions, 318 deletions
diff --git a/compiler/rustc_serialize/Cargo.toml b/compiler/rustc_serialize/Cargo.toml
index 46b923e8c..8bf98c163 100644
--- a/compiler/rustc_serialize/Cargo.toml
+++ b/compiler/rustc_serialize/Cargo.toml
@@ -4,10 +4,14 @@ version = "0.0.0"
edition = "2021"
[dependencies]
+# tidy-alphabetical-start
indexmap = "2.0.0"
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.12"
+# tidy-alphabetical-end
[dev-dependencies]
+# tidy-alphabetical-start
rustc_macros = { path = "../rustc_macros" }
tempfile = "3.2"
+# tidy-alphabetical-end
diff --git a/compiler/rustc_serialize/src/collection_impls.rs b/compiler/rustc_serialize/src/collection_impls.rs
deleted file mode 100644
index 8f8c50411..000000000
--- a/compiler/rustc_serialize/src/collection_impls.rs
+++ /dev/null
@@ -1,279 +0,0 @@
-//! Implementations of serialization for structures found in liballoc
-
-use crate::{Decodable, Decoder, Encodable, Encoder};
-use smallvec::{Array, SmallVec};
-use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
-use std::hash::{BuildHasher, Hash};
-use std::rc::Rc;
-use std::sync::Arc;
-use thin_vec::ThinVec;
-
-impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
- fn encode(&self, s: &mut S) {
- let slice: &[A::Item] = self;
- slice.encode(s);
- }
-}
-
-impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
- fn decode(d: &mut D) -> SmallVec<A> {
- let len = d.read_usize();
- (0..len).map(|_| Decodable::decode(d)).collect()
- }
-}
-
-impl<S: Encoder, T: Encodable<S>> Encodable<S> for ThinVec<T> {
- fn encode(&self, s: &mut S) {
- self.as_slice().encode(s);
- }
-}
-
-impl<D: Decoder, T: Decodable<D>> Decodable<D> for ThinVec<T> {
- fn decode(d: &mut D) -> ThinVec<T> {
- let len = d.read_usize();
- (0..len).map(|_| Decodable::decode(d)).collect()
- }
-}
-
-impl<S: Encoder, T: Encodable<S>> Encodable<S> for LinkedList<T> {
- fn encode(&self, s: &mut S) {
- s.emit_usize(self.len());
- for e in self.iter() {
- e.encode(s);
- }
- }
-}
-
-impl<D: Decoder, T: Decodable<D>> Decodable<D> for LinkedList<T> {
- fn decode(d: &mut D) -> LinkedList<T> {
- let len = d.read_usize();
- (0..len).map(|_| Decodable::decode(d)).collect()
- }
-}
-
-impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
- fn encode(&self, s: &mut S) {
- s.emit_usize(self.len());
- for e in self.iter() {
- e.encode(s);
- }
- }
-}
-
-impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
- fn decode(d: &mut D) -> VecDeque<T> {
- let len = d.read_usize();
- (0..len).map(|_| Decodable::decode(d)).collect()
- }
-}
-
-impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V>
-where
- K: Encodable<S> + PartialEq + Ord,
- V: Encodable<S>,
-{
- fn encode(&self, e: &mut S) {
- e.emit_usize(self.len());
- for (key, val) in self.iter() {
- key.encode(e);
- val.encode(e);
- }
- }
-}
-
-impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
-where
- K: Decodable<D> + PartialEq + Ord,
- V: Decodable<D>,
-{
- fn decode(d: &mut D) -> BTreeMap<K, V> {
- let len = d.read_usize();
- let mut map = BTreeMap::new();
- for _ in 0..len {
- let key = Decodable::decode(d);
- let val = Decodable::decode(d);
- map.insert(key, val);
- }
- map
- }
-}
-
-impl<S: Encoder, T> Encodable<S> for BTreeSet<T>
-where
- T: Encodable<S> + PartialEq + Ord,
-{
- fn encode(&self, s: &mut S) {
- s.emit_usize(self.len());
- for e in self.iter() {
- e.encode(s);
- }
- }
-}
-
-impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
-where
- T: Decodable<D> + PartialEq + Ord,
-{
- fn decode(d: &mut D) -> BTreeSet<T> {
- let len = d.read_usize();
- let mut set = BTreeSet::new();
- for _ in 0..len {
- set.insert(Decodable::decode(d));
- }
- set
- }
-}
-
-impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S>
-where
- K: Encodable<E> + Eq,
- V: Encodable<E>,
- S: BuildHasher,
-{
- fn encode(&self, e: &mut E) {
- e.emit_usize(self.len());
- for (key, val) in self.iter() {
- key.encode(e);
- val.encode(e);
- }
- }
-}
-
-impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
-where
- K: Decodable<D> + Hash + Eq,
- V: Decodable<D>,
- S: BuildHasher + Default,
-{
- fn decode(d: &mut D) -> HashMap<K, V, S> {
- let len = d.read_usize();
- let state = Default::default();
- let mut map = HashMap::with_capacity_and_hasher(len, state);
- for _ in 0..len {
- let key = Decodable::decode(d);
- let val = Decodable::decode(d);
- map.insert(key, val);
- }
- map
- }
-}
-
-impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S>
-where
- T: Encodable<E> + Eq,
- S: BuildHasher,
-{
- fn encode(&self, s: &mut E) {
- s.emit_usize(self.len());
- for e in self.iter() {
- e.encode(s);
- }
- }
-}
-
-impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
-where
- T: Decodable<D> + Hash + Eq,
- S: BuildHasher + Default,
-{
- fn decode(d: &mut D) -> HashSet<T, S> {
- 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
- }
-}
-
-impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S>
-where
- K: Encodable<E> + Hash + Eq,
- V: Encodable<E>,
- S: BuildHasher,
-{
- fn encode(&self, e: &mut E) {
- e.emit_usize(self.len());
- for (key, val) in self.iter() {
- key.encode(e);
- val.encode(e);
- }
- }
-}
-
-impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
-where
- K: Decodable<D> + Hash + Eq,
- V: Decodable<D>,
- S: BuildHasher + Default,
-{
- fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
- let len = d.read_usize();
- let state = Default::default();
- let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state);
- for _ in 0..len {
- let key = Decodable::decode(d);
- let val = Decodable::decode(d);
- map.insert(key, val);
- }
- map
- }
-}
-
-impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S>
-where
- T: Encodable<E> + Hash + Eq,
- S: BuildHasher,
-{
- fn encode(&self, s: &mut E) {
- s.emit_usize(self.len());
- for e in self.iter() {
- e.encode(s);
- }
- }
-}
-
-impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
-where
- T: Decodable<D> + Hash + Eq,
- S: BuildHasher + Default,
-{
- fn decode(d: &mut D) -> indexmap::IndexSet<T, S> {
- 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
- }
-}
-
-impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
- fn encode(&self, s: &mut E) {
- let slice: &[T] = self;
- slice.encode(s);
- }
-}
-
-impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
- fn decode(d: &mut D) -> Rc<[T]> {
- let vec: Vec<T> = Decodable::decode(d);
- vec.into()
- }
-}
-
-impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
- fn encode(&self, s: &mut E) {
- let slice: &[T] = self;
- slice.encode(s);
- }
-}
-
-impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> {
- fn decode(d: &mut D) -> Arc<[T]> {
- let vec: Vec<T> = Decodable::decode(d);
- vec.into()
- }
-}
diff --git a/compiler/rustc_serialize/src/lib.rs b/compiler/rustc_serialize/src/lib.rs
index dd40b3cf0..cfa54072e 100644
--- a/compiler/rustc_serialize/src/lib.rs
+++ b/compiler/rustc_serialize/src/lib.rs
@@ -1,25 +1,22 @@
//! Support code for encoding and decoding types.
-/*
-Core encoding and decoding interfaces.
-*/
-
#![doc(
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
html_playground_url = "https://play.rust-lang.org/",
test(attr(allow(unused_variables), deny(warnings)))
)]
-#![feature(never_type)]
+#![cfg_attr(not(bootstrap), doc(rust_logo))]
+#![cfg_attr(not(bootstrap), allow(internal_features))]
+#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
+#![feature(allocator_api)]
#![feature(associated_type_bounds)]
-#![feature(min_specialization)]
+#![feature(const_option)]
#![feature(core_intrinsics)]
-#![feature(maybe_uninit_slice)]
-#![feature(new_uninit)]
-#![feature(allocator_api)]
+#![feature(inline_const)]
+#![feature(min_specialization)]
+#![feature(never_type)]
#![feature(ptr_sub_ptr)]
#![feature(slice_first_last_chunk)]
-#![feature(inline_const)]
-#![feature(const_option)]
#![cfg_attr(test, feature(test))]
#![allow(rustc::internal)]
#![deny(rustc::untranslatable_diagnostic)]
@@ -27,7 +24,6 @@ Core encoding and decoding interfaces.
pub use self::serialize::{Decodable, Decoder, Encodable, Encoder};
-mod collection_impls;
mod serialize;
pub mod leb128;
diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs
index 06166cabc..63bd3457e 100644
--- a/compiler/rustc_serialize/src/serialize.rs
+++ b/compiler/rustc_serialize/src/serialize.rs
@@ -1,12 +1,15 @@
//! Support code for encoding and decoding types.
-use std::alloc::Allocator;
+use smallvec::{Array, SmallVec};
use std::borrow::Cow;
use std::cell::{Cell, RefCell};
+use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
+use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;
use std::path;
use std::rc::Rc;
use std::sync::Arc;
+use thin_vec::ThinVec;
/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
/// This way we can skip validation and still be relatively sure that deserialization
@@ -273,9 +276,9 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
}
}
-impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
- fn decode(d: &mut D) -> Box<[T], A> {
- let v: Vec<T, A> = Decodable::decode(d);
+impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
+ fn decode(d: &mut D) -> Box<[T]> {
+ let v: Vec<T> = Decodable::decode(d);
v.into_boxed_slice()
}
}
@@ -303,33 +306,20 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
fn encode(&self, s: &mut S) {
- let slice: &[T] = self;
- slice.encode(s);
+ self.as_slice().encode(s);
}
}
-impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
- default fn decode(d: &mut D) -> Vec<T, A> {
+impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
+ default fn decode(d: &mut D) -> Vec<T> {
let len = d.read_usize();
- let allocator = A::default();
- // 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_in(len, allocator);
- unsafe {
- let ptr: *mut T = vec.as_mut_ptr();
- for i in 0..len {
- std::ptr::write(ptr.add(i), Decodable::decode(d));
- }
- vec.set_len(len);
- }
- vec
+ (0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, T: Encodable<S>, const N: usize> Encodable<S> for [T; N] {
fn encode(&self, s: &mut S) {
- let slice: &[T] = self;
- slice.encode(s);
+ self.as_slice().encode(s);
}
}
@@ -497,15 +487,233 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
}
}
-impl<S: Encoder, T: ?Sized + Encodable<S>, A: Allocator + Default> Encodable<S> for Box<T, A> {
+impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
fn encode(&self, s: &mut S) {
(**self).encode(s)
}
}
-impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<T, A> {
- fn decode(d: &mut D) -> Box<T, A> {
- let allocator = A::default();
- Box::new_in(Decodable::decode(d), allocator)
+impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
+ fn decode(d: &mut D) -> Box<T> {
+ Box::new(Decodable::decode(d))
+ }
+}
+
+impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
+ fn encode(&self, s: &mut S) {
+ self.as_slice().encode(s);
+ }
+}
+
+impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
+ fn decode(d: &mut D) -> SmallVec<A> {
+ let len = d.read_usize();
+ (0..len).map(|_| Decodable::decode(d)).collect()
+ }
+}
+
+impl<S: Encoder, T: Encodable<S>> Encodable<S> for ThinVec<T> {
+ fn encode(&self, s: &mut S) {
+ self.as_slice().encode(s);
+ }
+}
+
+impl<D: Decoder, T: Decodable<D>> Decodable<D> for ThinVec<T> {
+ fn decode(d: &mut D) -> ThinVec<T> {
+ let len = d.read_usize();
+ (0..len).map(|_| Decodable::decode(d)).collect()
+ }
+}
+
+impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
+ fn encode(&self, s: &mut S) {
+ s.emit_usize(self.len());
+ for e in self.iter() {
+ e.encode(s);
+ }
+ }
+}
+
+impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
+ fn decode(d: &mut D) -> VecDeque<T> {
+ let len = d.read_usize();
+ (0..len).map(|_| Decodable::decode(d)).collect()
+ }
+}
+
+impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V>
+where
+ K: Encodable<S> + PartialEq + Ord,
+ V: Encodable<S>,
+{
+ fn encode(&self, e: &mut S) {
+ e.emit_usize(self.len());
+ for (key, val) in self.iter() {
+ key.encode(e);
+ val.encode(e);
+ }
+ }
+}
+
+impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
+where
+ K: Decodable<D> + PartialEq + Ord,
+ V: Decodable<D>,
+{
+ fn decode(d: &mut D) -> BTreeMap<K, V> {
+ let len = d.read_usize();
+ (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
+ }
+}
+
+impl<S: Encoder, T> Encodable<S> for BTreeSet<T>
+where
+ T: Encodable<S> + PartialEq + Ord,
+{
+ fn encode(&self, s: &mut S) {
+ s.emit_usize(self.len());
+ for e in self.iter() {
+ e.encode(s);
+ }
+ }
+}
+
+impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
+where
+ T: Decodable<D> + PartialEq + Ord,
+{
+ fn decode(d: &mut D) -> BTreeSet<T> {
+ let len = d.read_usize();
+ (0..len).map(|_| Decodable::decode(d)).collect()
+ }
+}
+
+impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S>
+where
+ K: Encodable<E> + Eq,
+ V: Encodable<E>,
+ S: BuildHasher,
+{
+ fn encode(&self, e: &mut E) {
+ e.emit_usize(self.len());
+ for (key, val) in self.iter() {
+ key.encode(e);
+ val.encode(e);
+ }
+ }
+}
+
+impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
+where
+ K: Decodable<D> + Hash + Eq,
+ V: Decodable<D>,
+ S: BuildHasher + Default,
+{
+ fn decode(d: &mut D) -> HashMap<K, V, S> {
+ let len = d.read_usize();
+ (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
+ }
+}
+
+impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S>
+where
+ T: Encodable<E> + Eq,
+ S: BuildHasher,
+{
+ fn encode(&self, s: &mut E) {
+ s.emit_usize(self.len());
+ for e in self.iter() {
+ e.encode(s);
+ }
+ }
+}
+
+impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
+where
+ T: Decodable<D> + Hash + Eq,
+ S: BuildHasher + Default,
+{
+ fn decode(d: &mut D) -> HashSet<T, S> {
+ let len = d.read_usize();
+ (0..len).map(|_| Decodable::decode(d)).collect()
+ }
+}
+
+impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S>
+where
+ K: Encodable<E> + Hash + Eq,
+ V: Encodable<E>,
+ S: BuildHasher,
+{
+ fn encode(&self, e: &mut E) {
+ e.emit_usize(self.len());
+ for (key, val) in self.iter() {
+ key.encode(e);
+ val.encode(e);
+ }
+ }
+}
+
+impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
+where
+ K: Decodable<D> + Hash + Eq,
+ V: Decodable<D>,
+ S: BuildHasher + Default,
+{
+ fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
+ let len = d.read_usize();
+ (0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
+ }
+}
+
+impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S>
+where
+ T: Encodable<E> + Hash + Eq,
+ S: BuildHasher,
+{
+ fn encode(&self, s: &mut E) {
+ s.emit_usize(self.len());
+ for e in self.iter() {
+ e.encode(s);
+ }
+ }
+}
+
+impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
+where
+ T: Decodable<D> + Hash + Eq,
+ S: BuildHasher + Default,
+{
+ fn decode(d: &mut D) -> indexmap::IndexSet<T, S> {
+ let len = d.read_usize();
+ (0..len).map(|_| Decodable::decode(d)).collect()
+ }
+}
+
+impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
+ fn encode(&self, s: &mut E) {
+ let slice: &[T] = self;
+ slice.encode(s);
+ }
+}
+
+impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
+ fn decode(d: &mut D) -> Rc<[T]> {
+ let vec: Vec<T> = Decodable::decode(d);
+ vec.into()
+ }
+}
+
+impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
+ fn encode(&self, s: &mut E) {
+ let slice: &[T] = self;
+ slice.encode(s);
+ }
+}
+
+impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> {
+ fn decode(d: &mut D) -> Arc<[T]> {
+ let vec: Vec<T> = Decodable::decode(d);
+ vec.into()
}
}