1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A stack-allocated vector, allowing storage of N elements on the stack.
13 use std
::marker
::Unsize
;
14 use std
::iter
::Extend
;
15 use std
::ptr
::{self, drop_in_place}
;
16 use std
::ops
::{Deref, DerefMut, Range}
;
17 use std
::hash
::{Hash, Hasher}
;
22 pub unsafe trait Array
{
24 type PartialStorage
: Default
+ Unsize
<[ManuallyDrop
<Self::Element
>]>;
28 unsafe impl<T
> Array
for [T
; 1] {
30 type PartialStorage
= [ManuallyDrop
<T
>; 1];
34 unsafe impl<T
> Array
for [T
; 8] {
36 type PartialStorage
= [ManuallyDrop
<T
>; 8];
40 pub struct ArrayVec
<A
: Array
> {
42 values
: A
::PartialStorage
45 impl<A
> Hash
for ArrayVec
<A
>
48 fn hash
<H
>(&self, state
: &mut H
) where H
: Hasher
{
49 (&self[..]).hash(state
);
53 impl<A
: Array
> PartialEq
for ArrayVec
<A
> {
54 fn eq(&self, other
: &Self) -> bool
{
59 impl<A
: Array
> Eq
for ArrayVec
<A
> {}
61 impl<A
> Clone
for ArrayVec
<A
>
64 fn clone(&self) -> Self {
65 let mut v
= ArrayVec
::new();
66 v
.extend(self.iter().cloned());
71 impl<A
: Array
> ArrayVec
<A
> {
72 pub fn new() -> Self {
75 values
: Default
::default(),
79 pub fn len(&self) -> usize {
83 pub unsafe fn set_len(&mut self, len
: usize) {
87 /// Panics when the stack vector is full.
88 pub fn push(&mut self, el
: A
::Element
) {
89 let arr
= &mut self.values
as &mut [ManuallyDrop
<_
>];
90 arr
[self.count
] = ManuallyDrop { value: el }
;
94 pub fn pop(&mut self) -> Option
<A
::Element
> {
96 let arr
= &mut self.values
as &mut [ManuallyDrop
<_
>];
99 let value
= ptr
::read(&arr
[self.count
]);
108 impl<A
> Default
for ArrayVec
<A
>
110 fn default() -> Self {
115 impl<A
> fmt
::Debug
for ArrayVec
<A
>
117 A
::Element
: fmt
::Debug
{
118 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
123 impl<A
: Array
> Deref
for ArrayVec
<A
> {
124 type Target
= [A
::Element
];
125 fn deref(&self) -> &Self::Target
{
127 slice
::from_raw_parts(&self.values
as *const _
as *const A
::Element
, self.count
)
132 impl<A
: Array
> DerefMut
for ArrayVec
<A
> {
133 fn deref_mut(&mut self) -> &mut [A
::Element
] {
135 slice
::from_raw_parts_mut(&mut self.values
as *mut _
as *mut A
::Element
, self.count
)
140 impl<A
: Array
> Drop
for ArrayVec
<A
> {
143 drop_in_place(&mut self[..])
148 impl<A
: Array
> Extend
<A
::Element
> for ArrayVec
<A
> {
149 fn extend
<I
>(&mut self, iter
: I
) where I
: IntoIterator
<Item
=A
::Element
> {
156 pub struct Iter
<A
: Array
> {
157 indices
: Range
<usize>,
158 store
: A
::PartialStorage
,
161 impl<A
: Array
> Drop
for Iter
<A
> {
167 impl<A
: Array
> Iterator
for Iter
<A
> {
168 type Item
= A
::Element
;
170 fn next(&mut self) -> Option
<A
::Element
> {
171 let arr
= &self.store
as &[ManuallyDrop
<_
>];
173 self.indices
.next().map(|i
| ptr
::read(&arr
[i
]).value
)
177 fn size_hint(&self) -> (usize, Option
<usize>) {
178 self.indices
.size_hint()
182 impl<A
: Array
> IntoIterator
for ArrayVec
<A
> {
183 type Item
= A
::Element
;
184 type IntoIter
= Iter
<A
>;
185 fn into_iter(self) -> Self::IntoIter
{
187 ptr
::read(&self.values
)
189 let indices
= 0..self.count
;
198 impl<'a
, A
: Array
> IntoIterator
for &'a ArrayVec
<A
> {
199 type Item
= &'a A
::Element
;
200 type IntoIter
= slice
::Iter
<'a
, A
::Element
>;
201 fn into_iter(self) -> Self::IntoIter
{
206 impl<'a
, A
: Array
> IntoIterator
for &'a
mut ArrayVec
<A
> {
207 type Item
= &'a
mut A
::Element
;
208 type IntoIter
= slice
::IterMut
<'a
, A
::Element
>;
209 fn into_iter(self) -> Self::IntoIter
{
214 // FIXME: This should use repr(transparent) from rust-lang/rfcs#1758.
215 #[allow(unions_with_drop_fields)]
216 pub union ManuallyDrop
<T
> {
222 impl<T
> ManuallyDrop
<T
> {
223 fn new() -> ManuallyDrop
<T
> {
230 impl<T
> Default
for ManuallyDrop
<T
> {
231 fn default() -> Self {