|
1 | 1 | //! Support code for encoding and decoding types. |
2 | 2 |
|
3 | 3 | use smallvec::{Array, SmallVec}; |
4 | | -use std::alloc::Allocator; |
5 | 4 | use std::borrow::Cow; |
6 | 5 | use std::cell::{Cell, RefCell}; |
7 | 6 | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; |
@@ -277,9 +276,9 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> { |
277 | 276 | } |
278 | 277 | } |
279 | 278 |
|
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); |
283 | 282 | v.into_boxed_slice() |
284 | 283 | } |
285 | 284 | } |
@@ -311,21 +310,10 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> { |
311 | 310 | } |
312 | 311 | } |
313 | 312 |
|
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> { |
316 | 315 | 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() |
329 | 317 | } |
330 | 318 | } |
331 | 319 |
|
@@ -499,16 +487,15 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> { |
499 | 487 | } |
500 | 488 | } |
501 | 489 |
|
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> { |
503 | 491 | fn encode(&self, s: &mut S) { |
504 | 492 | (**self).encode(s) |
505 | 493 | } |
506 | 494 | } |
507 | 495 |
|
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)) |
512 | 499 | } |
513 | 500 | } |
514 | 501 |
|
|
0 commit comments