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 //! Currently, only the N = 8 case is supported (due to Array only being impl-ed for [T; 8]).
15 use std
::marker
::Unsize
;
16 use std
::iter
::Extend
;
17 use std
::ptr
::drop_in_place
;
18 use std
::ops
::{Deref, DerefMut}
;
22 pub unsafe trait Array
{
24 type PartialStorage
: Default
+ Unsize
<[ManuallyDrop
<Self::Element
>]>;
28 unsafe impl<T
> Array
for [T
; 8] {
30 type PartialStorage
= [ManuallyDrop
<T
>; 8];
34 pub struct ArrayVec
<A
: Array
> {
36 values
: A
::PartialStorage
39 impl<A
: Array
> ArrayVec
<A
> {
40 pub fn new() -> Self {
43 values
: Default
::default(),
48 impl<A
> fmt
::Debug
for ArrayVec
<A
>
50 A
::Element
: fmt
::Debug
{
51 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
56 impl<A
: Array
> Deref
for ArrayVec
<A
> {
57 type Target
= [A
::Element
];
58 fn deref(&self) -> &Self::Target
{
60 slice
::from_raw_parts(&self.values
as *const _
as *const A
::Element
, self.count
)
65 impl<A
: Array
> DerefMut
for ArrayVec
<A
> {
66 fn deref_mut(&mut self) -> &mut [A
::Element
] {
68 slice
::from_raw_parts_mut(&mut self.values
as *mut _
as *mut A
::Element
, self.count
)
73 impl<A
: Array
> Drop
for ArrayVec
<A
> {
76 drop_in_place(&mut self[..])
81 impl<A
: Array
> Extend
<A
::Element
> for ArrayVec
<A
> {
82 fn extend
<I
>(&mut self, iter
: I
) where I
: IntoIterator
<Item
=A
::Element
> {
85 let arr
= &mut self.values
as &mut [ManuallyDrop
<_
>];
86 arr
[self.count
].value
= el
;
93 // FIXME: This should use repr(transparent) from rust-lang/rfcs#1758.
94 #[allow(unions_with_drop_fields)]
95 pub union ManuallyDrop
<T
> {
101 impl<T
> Default
for ManuallyDrop
<T
> {
102 fn default() -> Self {
103 ManuallyDrop { empty: () }