2016-10-24 19:22:59 -05:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! A stack-allocated vector, allowing storage of N elements on the stack.
|
|
|
|
|
|
|
|
use std::marker::Unsize;
|
|
|
|
use std::iter::Extend;
|
2017-12-22 11:58:39 -06:00
|
|
|
use std::ptr::{self, drop_in_place, NonNull};
|
2017-06-30 13:34:53 -05:00
|
|
|
use std::ops::{Deref, DerefMut, Range};
|
2016-11-02 23:33:35 -05:00
|
|
|
use std::hash::{Hash, Hasher};
|
2016-10-24 19:22:59 -05:00
|
|
|
use std::slice;
|
|
|
|
use std::fmt;
|
2016-11-02 23:33:35 -05:00
|
|
|
use std::mem;
|
2017-03-22 12:11:51 -05:00
|
|
|
use std::mem::ManuallyDrop;
|
2018-03-19 03:01:17 -05:00
|
|
|
use std::ops::Bound::{Excluded, Included, Unbounded};
|
2018-03-19 03:26:29 -05:00
|
|
|
use std::ops::RangeBounds;
|
2016-10-24 19:22:59 -05:00
|
|
|
|
|
|
|
pub unsafe trait Array {
|
|
|
|
type Element;
|
2017-03-22 12:11:51 -05:00
|
|
|
type PartialStorage: Unsize<[ManuallyDrop<Self::Element>]>;
|
2016-10-24 19:22:59 -05:00
|
|
|
const LEN: usize;
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:33:35 -05:00
|
|
|
unsafe impl<T> Array for [T; 1] {
|
|
|
|
type Element = T;
|
|
|
|
type PartialStorage = [ManuallyDrop<T>; 1];
|
|
|
|
const LEN: usize = 1;
|
|
|
|
}
|
|
|
|
|
2016-10-24 19:22:59 -05:00
|
|
|
unsafe impl<T> Array for [T; 8] {
|
|
|
|
type Element = T;
|
|
|
|
type PartialStorage = [ManuallyDrop<T>; 8];
|
|
|
|
const LEN: usize = 8;
|
|
|
|
}
|
|
|
|
|
2017-04-05 06:00:17 -05:00
|
|
|
unsafe impl<T> Array for [T; 32] {
|
|
|
|
type Element = T;
|
|
|
|
type PartialStorage = [ManuallyDrop<T>; 32];
|
|
|
|
const LEN: usize = 32;
|
|
|
|
}
|
|
|
|
|
2016-10-24 19:22:59 -05:00
|
|
|
pub struct ArrayVec<A: Array> {
|
|
|
|
count: usize,
|
|
|
|
values: A::PartialStorage
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:33:35 -05:00
|
|
|
impl<A> Hash for ArrayVec<A>
|
|
|
|
where A: Array,
|
|
|
|
A::Element: Hash {
|
|
|
|
fn hash<H>(&self, state: &mut H) where H: Hasher {
|
|
|
|
(&self[..]).hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A> Clone for ArrayVec<A>
|
|
|
|
where A: Array,
|
|
|
|
A::Element: Clone {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let mut v = ArrayVec::new();
|
|
|
|
v.extend(self.iter().cloned());
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 19:22:59 -05:00
|
|
|
impl<A: Array> ArrayVec<A> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ArrayVec {
|
|
|
|
count: 0,
|
2017-03-22 12:11:51 -05:00
|
|
|
values: unsafe { ::std::mem::uninitialized() },
|
2016-10-24 19:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 23:33:35 -05:00
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.count
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn set_len(&mut self, len: usize) {
|
|
|
|
self.count = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Panics when the stack vector is full.
|
|
|
|
pub fn push(&mut self, el: A::Element) {
|
|
|
|
let arr = &mut self.values as &mut [ManuallyDrop<_>];
|
2017-03-22 12:11:51 -05:00
|
|
|
arr[self.count] = ManuallyDrop::new(el);
|
2016-11-02 23:33:35 -05:00
|
|
|
self.count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop(&mut self) -> Option<A::Element> {
|
|
|
|
if self.count > 0 {
|
|
|
|
let arr = &mut self.values as &mut [ManuallyDrop<_>];
|
|
|
|
self.count -= 1;
|
|
|
|
unsafe {
|
2017-03-22 12:11:51 -05:00
|
|
|
let value = ptr::read(&*arr[self.count]);
|
|
|
|
Some(value)
|
2016-11-02 23:33:35 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 08:25:26 -06:00
|
|
|
|
|
|
|
pub fn drain<R>(&mut self, range: R) -> Drain<A>
|
2018-03-19 03:26:29 -05:00
|
|
|
where R: RangeBounds<usize>
|
2016-12-11 08:25:26 -06:00
|
|
|
{
|
|
|
|
// Memory safety
|
|
|
|
//
|
|
|
|
// When the Drain is first created, it shortens the length of
|
2017-11-21 08:33:45 -06:00
|
|
|
// the source vector to make sure no uninitialized or moved-from elements
|
2016-12-11 08:25:26 -06:00
|
|
|
// are accessible at all if the Drain's destructor never gets to run.
|
|
|
|
//
|
|
|
|
// Drain will ptr::read out the values to remove.
|
|
|
|
// When finished, remaining tail of the vec is copied back to cover
|
|
|
|
// the hole, and the vector length is restored to the new length.
|
|
|
|
//
|
|
|
|
let len = self.len();
|
2017-01-14 11:06:00 -06:00
|
|
|
let start = match range.start() {
|
|
|
|
Included(&n) => n,
|
|
|
|
Excluded(&n) => n + 1,
|
|
|
|
Unbounded => 0,
|
|
|
|
};
|
|
|
|
let end = match range.end() {
|
|
|
|
Included(&n) => n + 1,
|
|
|
|
Excluded(&n) => n,
|
|
|
|
Unbounded => len,
|
|
|
|
};
|
2016-12-11 08:25:26 -06:00
|
|
|
assert!(start <= end);
|
|
|
|
assert!(end <= len);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// set self.vec length's to start, to be safe in case Drain is leaked
|
|
|
|
self.set_len(start);
|
|
|
|
// Use the borrow in the IterMut to indicate borrowing behavior of the
|
|
|
|
// whole Drain iterator (like &mut T).
|
|
|
|
let range_slice = {
|
2017-12-23 02:47:13 -06:00
|
|
|
let arr = &mut self.values as &mut [ManuallyDrop<<A as Array>::Element>];
|
2016-12-11 08:25:26 -06:00
|
|
|
slice::from_raw_parts_mut(arr.as_mut_ptr().offset(start as isize),
|
|
|
|
end - start)
|
|
|
|
};
|
|
|
|
Drain {
|
|
|
|
tail_start: end,
|
|
|
|
tail_len: len - end,
|
|
|
|
iter: range_slice.iter(),
|
2017-12-22 11:58:39 -06:00
|
|
|
array_vec: NonNull::from(self),
|
2016-12-11 08:25:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 23:33:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A> Default for ArrayVec<A>
|
|
|
|
where A: Array {
|
|
|
|
fn default() -> Self {
|
|
|
|
ArrayVec::new()
|
|
|
|
}
|
2016-10-24 19:22:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A> fmt::Debug for ArrayVec<A>
|
|
|
|
where A: Array,
|
|
|
|
A::Element: fmt::Debug {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self[..].fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Array> Deref for ArrayVec<A> {
|
|
|
|
type Target = [A::Element];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
unsafe {
|
|
|
|
slice::from_raw_parts(&self.values as *const _ as *const A::Element, self.count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Array> DerefMut for ArrayVec<A> {
|
|
|
|
fn deref_mut(&mut self) -> &mut [A::Element] {
|
|
|
|
unsafe {
|
|
|
|
slice::from_raw_parts_mut(&mut self.values as *mut _ as *mut A::Element, self.count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Array> Drop for ArrayVec<A> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
drop_in_place(&mut self[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Array> Extend<A::Element> for ArrayVec<A> {
|
|
|
|
fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=A::Element> {
|
|
|
|
for el in iter {
|
2016-11-02 23:33:35 -05:00
|
|
|
self.push(el);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Iter<A: Array> {
|
|
|
|
indices: Range<usize>,
|
|
|
|
store: A::PartialStorage,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Array> Drop for Iter<A> {
|
|
|
|
fn drop(&mut self) {
|
2018-04-04 18:10:38 -05:00
|
|
|
self.for_each(drop);
|
2016-11-02 23:33:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Array> Iterator for Iter<A> {
|
|
|
|
type Item = A::Element;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<A::Element> {
|
|
|
|
let arr = &self.store as &[ManuallyDrop<_>];
|
|
|
|
unsafe {
|
2017-03-22 12:11:51 -05:00
|
|
|
self.indices.next().map(|i| ptr::read(&*arr[i]))
|
2016-11-02 23:33:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.indices.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 08:25:26 -06:00
|
|
|
pub struct Drain<'a, A: Array>
|
|
|
|
where A::Element: 'a
|
|
|
|
{
|
|
|
|
tail_start: usize,
|
|
|
|
tail_len: usize,
|
|
|
|
iter: slice::Iter<'a, ManuallyDrop<A::Element>>,
|
2017-12-22 11:58:39 -06:00
|
|
|
array_vec: NonNull<ArrayVec<A>>,
|
2016-12-11 08:25:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, A: Array> Iterator for Drain<'a, A> {
|
|
|
|
type Item = A::Element;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A::Element> {
|
2017-03-22 12:11:51 -05:00
|
|
|
self.iter.next().map(|elt| unsafe { ptr::read(&**elt) })
|
2016-12-11 08:25:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, A: Array> Drop for Drain<'a, A> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// exhaust self first
|
2018-04-04 18:10:38 -05:00
|
|
|
self.for_each(drop);
|
2016-12-11 08:25:26 -06:00
|
|
|
|
|
|
|
if self.tail_len > 0 {
|
|
|
|
unsafe {
|
2017-12-22 17:16:19 -06:00
|
|
|
let source_array_vec: &mut ArrayVec<A> = self.array_vec.as_mut();
|
2016-12-11 08:25:26 -06:00
|
|
|
// memmove back untouched tail, update to new length
|
|
|
|
let start = source_array_vec.len();
|
|
|
|
let tail = self.tail_start;
|
|
|
|
{
|
2017-12-23 02:47:13 -06:00
|
|
|
let arr =
|
|
|
|
&mut source_array_vec.values as &mut [ManuallyDrop<<A as Array>::Element>];
|
2016-12-11 08:25:26 -06:00
|
|
|
let src = arr.as_ptr().offset(tail as isize);
|
|
|
|
let dst = arr.as_mut_ptr().offset(start as isize);
|
|
|
|
ptr::copy(src, dst, self.tail_len);
|
|
|
|
};
|
|
|
|
source_array_vec.set_len(start + self.tail_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:33:35 -05:00
|
|
|
impl<A: Array> IntoIterator for ArrayVec<A> {
|
|
|
|
type Item = A::Element;
|
|
|
|
type IntoIter = Iter<A>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
let store = unsafe {
|
|
|
|
ptr::read(&self.values)
|
|
|
|
};
|
|
|
|
let indices = 0..self.count;
|
|
|
|
mem::forget(self);
|
|
|
|
Iter {
|
2017-08-07 00:54:09 -05:00
|
|
|
indices,
|
|
|
|
store,
|
2016-10-24 19:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:33:35 -05:00
|
|
|
impl<'a, A: Array> IntoIterator for &'a ArrayVec<A> {
|
|
|
|
type Item = &'a A::Element;
|
|
|
|
type IntoIter = slice::Iter<'a, A::Element>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, A: Array> IntoIterator for &'a mut ArrayVec<A> {
|
|
|
|
type Item = &'a mut A::Element;
|
|
|
|
type IntoIter = slice::IterMut<'a, A::Element>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.iter_mut()
|
|
|
|
}
|
|
|
|
}
|