about summary refs log tree commit diff
path: root/compiler/rustc_serialize/src/serialize.rs
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2023-10-06 10:16:20 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2023-10-06 10:30:03 +1100
commitad8271dd56bc3a0ef4a1a28af1ef321608d950b2 (patch)
tree468715ff15122b15e064c0a7f7a68497bec6ac68 /compiler/rustc_serialize/src/serialize.rs
parent1d71971973cd7429e9cf61c27bfb95daaac86486 (diff)
downloadrust-ad8271dd56bc3a0ef4a1a28af1ef321608d950b2.tar.gz
rust-ad8271dd56bc3a0ef4a1a28af1ef321608d950b2.zip
Use `collect` to decode `Vec`.
It's hyper-optimized, we don't need our own unsafe code here.

This requires getting rid of all the `Allocator` stuff, which isn't
needed anyway.
Diffstat (limited to 'compiler/rustc_serialize/src/serialize.rs')
-rw-r--r--compiler/rustc_serialize/src/serialize.rs33
1 files changed, 10 insertions, 23 deletions
diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs
index 0a340ac09fb..63bd3457eb9 100644
--- a/compiler/rustc_serialize/src/serialize.rs
+++ b/compiler/rustc_serialize/src/serialize.rs
@@ -1,7 +1,6 @@
 //! Support code for encoding and decoding types.
 
 use smallvec::{Array, SmallVec};
-use std::alloc::Allocator;
 use std::borrow::Cow;
 use std::cell::{Cell, RefCell};
 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
@@ -277,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()
     }
 }
@@ -311,21 +310,10 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
     }
 }
 
-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()
     }
 }
 
@@ -499,16 +487,15 @@ 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))
     }
 }