Skip to content

Commit ad8271d

Browse files
committed
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.
1 parent 1d71971 commit ad8271d

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

compiler/rustc_serialize/src/serialize.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Support code for encoding and decoding types.
22
33
use smallvec::{Array, SmallVec};
4-
use std::alloc::Allocator;
54
use std::borrow::Cow;
65
use std::cell::{Cell, RefCell};
76
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
@@ -277,9 +276,9 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
277276
}
278277
}
279278

280-
impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
281-
fn decode(d: &mut D) -> Box<[T], A> {
282-
let v: Vec<T, A> = Decodable::decode(d);
279+
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
280+
fn decode(d: &mut D) -> Box<[T]> {
281+
let v: Vec<T> = Decodable::decode(d);
283282
v.into_boxed_slice()
284283
}
285284
}
@@ -311,21 +310,10 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
311310
}
312311
}
313312

314-
impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
315-
default fn decode(d: &mut D) -> Vec<T, A> {
313+
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
314+
default fn decode(d: &mut D) -> Vec<T> {
316315
let len = d.read_usize();
317-
let allocator = A::default();
318-
// SAFETY: we set the capacity in advance, only write elements, and
319-
// only set the length at the end once the writing has succeeded.
320-
let mut vec = Vec::with_capacity_in(len, allocator);
321-
unsafe {
322-
let ptr: *mut T = vec.as_mut_ptr();
323-
for i in 0..len {
324-
std::ptr::write(ptr.add(i), Decodable::decode(d));
325-
}
326-
vec.set_len(len);
327-
}
328-
vec
316+
(0..len).map(|_| Decodable::decode(d)).collect()
329317
}
330318
}
331319

@@ -499,16 +487,15 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
499487
}
500488
}
501489

502-
impl<S: Encoder, T: ?Sized + Encodable<S>, A: Allocator + Default> Encodable<S> for Box<T, A> {
490+
impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
503491
fn encode(&self, s: &mut S) {
504492
(**self).encode(s)
505493
}
506494
}
507495

508-
impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<T, A> {
509-
fn decode(d: &mut D) -> Box<T, A> {
510-
let allocator = A::default();
511-
Box::new_in(Decodable::decode(d), allocator)
496+
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
497+
fn decode(d: &mut D) -> Box<T> {
498+
Box::new(Decodable::decode(d))
512499
}
513500
}
514501

0 commit comments

Comments
 (0)