diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2019-12-03 22:52:45 +0100 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2019-12-03 23:35:40 +0100 |
| commit | d84eb506e29bfc8eddb57e84b25cf548bb8e3f5a (patch) | |
| tree | 44da8ef62c2d8351e136d6e7f9da58c1eb38fc21 /src/libarena | |
| parent | 7afe6d9d1f48b998cc88fe6f01ba0082788ba4b9 (diff) | |
| download | rust-d84eb506e29bfc8eddb57e84b25cf548bb8e3f5a.tar.gz rust-d84eb506e29bfc8eddb57e84b25cf548bb8e3f5a.zip | |
Fix TypedArena.
Diffstat (limited to 'src/libarena')
| -rw-r--r-- | src/libarena/lib.rs | 59 |
1 files changed, 12 insertions, 47 deletions
diff --git a/src/libarena/lib.rs b/src/libarena/lib.rs index 66d27a27519..854942dad3d 100644 --- a/src/libarena/lib.rs +++ b/src/libarena/lib.rs @@ -202,53 +202,18 @@ impl<T> TypedArena<T> { #[inline] pub fn alloc_from_iter<I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] { assert!(mem::size_of::<T>() != 0); - let mut iter = iter.into_iter(); - let size_hint = iter.size_hint(); - - match size_hint { - (min, Some(max)) if min == max => { - // We know the exact number of elements the iterator will produce here - let len = min; - - if len == 0 { - return &mut []; - } - - self.ensure_capacity(len); - - let slice = self.ptr.get(); - - unsafe { - let mut ptr = self.ptr.get(); - for _ in 0..len { - // Write into uninitialized memory. - ptr::write(ptr, iter.next().unwrap()); - // Advance the pointer. - ptr = ptr.offset(1); - // Update the pointer per iteration so if `iter.next()` panics - // we destroy the correct amount - self.ptr.set(ptr); - } - slice::from_raw_parts_mut(slice, len) - } - } - _ => { - cold_path(move || -> &mut [T] { - let mut vec: SmallVec<[_; 8]> = iter.collect(); - if vec.is_empty() { - return &mut []; - } - // Move the content to the arena by copying it and then forgetting - // the content of the SmallVec - unsafe { - let len = vec.len(); - let start_ptr = self.alloc_raw_slice(len); - vec.as_ptr().copy_to_nonoverlapping(start_ptr, len); - vec.set_len(0); - slice::from_raw_parts_mut(start_ptr, len) - } - }) - } + let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect(); + if vec.is_empty() { + return &mut []; + } + // Move the content to the arena by copying it and then forgetting + // the content of the SmallVec + unsafe { + let len = vec.len(); + let start_ptr = self.alloc_raw_slice(len); + vec.as_ptr().copy_to_nonoverlapping(start_ptr, len); + vec.set_len(0); + slice::from_raw_parts_mut(start_ptr, len) } } |
