Skip to content

Commit 576324f

Browse files
committed
Add ArrayVecCopy, which is a copyable ArrayVec
Fixes bluss#32.
1 parent ae48f8e commit 576324f

File tree

4 files changed

+386
-4
lines changed

4 files changed

+386
-4
lines changed

src/copy.rs

+379
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,379 @@
1+
use std::cmp;
2+
use std::iter;
3+
use std::ptr;
4+
use std::ops;
5+
use std::slice;
6+
7+
// extra traits
8+
use std::borrow::{Borrow, BorrowMut};
9+
use std::hash::{Hash, Hasher};
10+
use std::fmt;
11+
12+
#[cfg(feature="std")]
13+
use std::io;
14+
15+
use Array;
16+
use CapacityError;
17+
use RangeArgument;
18+
use array::Index;
19+
use raw::Drain;
20+
use raw::RawArrayVec;
21+
22+
/// A vector with a fixed capacity that implements `Copy`.
23+
pub struct ArrayVecCopy<A: Array + Copy> {
24+
inner: RawArrayVec<A>,
25+
}
26+
27+
impl<A: Array + Copy> ArrayVecCopy<A> {
28+
/// Create a new empty `ArrayVecCopy`.
29+
///
30+
/// Capacity is inferred from the type parameter.
31+
#[inline]
32+
pub fn new() -> ArrayVecCopy<A> {
33+
ArrayVecCopy {
34+
inner: RawArrayVec::new(),
35+
}
36+
}
37+
38+
/// Return the number of elements in the `ArrayVecCopy`.
39+
#[inline]
40+
pub fn len(&self) -> usize { self.inner.len() }
41+
42+
/// Return the capacity of the `ArrayVecCopy`.
43+
#[inline]
44+
pub fn capacity(&self) -> usize { self.inner.capacity() }
45+
46+
/// Push `element` to the end of the vector.
47+
///
48+
/// Returns `Ok` if the push succeeds.
49+
///
50+
/// **Errors** if the backing array is not large enough to fit the
51+
/// additional element.
52+
#[inline]
53+
pub fn push(&mut self, element: A::Item) -> Result<(), CapacityError<A::Item>> {
54+
self.inner.push(element)
55+
}
56+
57+
/// Insert `element` in position `index`.
58+
///
59+
/// Shift up all elements after `index`. If any is pushed out, `Err` is
60+
/// returned.
61+
///
62+
/// Return `Ok` if no element is shifted out.
63+
///
64+
/// **Panics** if the specified index is greater than the current length.
65+
#[inline]
66+
pub fn insert(&mut self, index: usize, element: A::Item)
67+
-> Result<(), CapacityError<A::Item>>
68+
{
69+
self.inner.insert(index, element)
70+
}
71+
72+
/// Remove the last element in the vector.
73+
///
74+
/// Return `Some(` *element* `)` if the vector is non-empty, else `None`.
75+
#[inline]
76+
pub fn pop(&mut self) -> Option<A::Item> {
77+
self.inner.pop()
78+
}
79+
80+
/// Remove the element at `index` and swap the last element into its place.
81+
///
82+
/// This operation is O(1).
83+
///
84+
/// Return `Some(` *element* `)` if the index is in bounds, else `None`.
85+
#[inline]
86+
pub fn swap_remove(&mut self, index: usize) -> Option<A::Item> {
87+
self.inner.swap_remove(index)
88+
}
89+
90+
/// Remove the element at `index` and shift down the following elements.
91+
///
92+
/// Return `Some(` *element* `)` if the index is in bounds, else `None`.
93+
#[inline]
94+
pub fn remove(&mut self, index: usize) -> Option<A::Item> {
95+
self.inner.remove(index)
96+
}
97+
98+
/// Remove all elements in the vector.
99+
///
100+
/// This is a constant-time operation.
101+
#[inline]
102+
pub fn clear(&mut self) {
103+
unsafe {
104+
self.set_len(0);
105+
}
106+
}
107+
108+
/// Retains only the elements specified by the predicate.
109+
///
110+
/// In other words, remove all elements `e` such that `f(&mut e)` returns false.
111+
/// This method operates in place and preserves the order of the retained
112+
/// elements.
113+
#[inline]
114+
pub fn retain<F>(&mut self, f: F)
115+
where F: FnMut(&mut A::Item) -> bool
116+
{
117+
self.inner.retain(f)
118+
}
119+
120+
/// Set the vector's length without dropping or moving out elements
121+
///
122+
/// May panic if `length` is greater than the capacity.
123+
///
124+
/// This function is `unsafe` because it changes the notion of the
125+
/// number of “valid” elements in the vector. Use with care.
126+
#[inline]
127+
pub unsafe fn set_len(&mut self, length: usize) {
128+
self.inner.set_len(length)
129+
}
130+
131+
132+
/// Create a draining iterator that removes the specified range in the vector
133+
/// and yields the removed items from start to end. The element range is
134+
/// removed even if the iterator is not consumed until the end.
135+
///
136+
/// Note: It is unspecified how many elements are removed from the vector,
137+
/// if the `Drain` value is leaked.
138+
///
139+
/// **Panics** if the starting point is greater than the end point or if
140+
/// the end point is greater than the length of the vector.
141+
pub fn drain<R: RangeArgument>(&mut self, range: R) -> Drain<A> {
142+
self.inner.drain(range)
143+
}
144+
145+
/// Return the inner fixed size array, if it is full to its capacity.
146+
///
147+
/// Return an `Ok` value with the array if length equals capacity,
148+
/// return an `Err` with self otherwise.
149+
///
150+
/// `Note:` This function may incur unproportionally large overhead
151+
/// to move the array out, its performance is not optimal.
152+
pub fn into_inner(self) -> Result<A, Self> {
153+
self.inner.into_inner().map_err(|e| ArrayVecCopy { inner: e })
154+
}
155+
156+
/// Dispose of `self` without the overwriting that is needed in Drop.
157+
pub fn dispose(self) { }
158+
159+
/// Return a slice containing all elements of the vector.
160+
pub fn as_slice(&self) -> &[A::Item] {
161+
self.inner.as_slice()
162+
}
163+
164+
/// Return a mutable slice containing all elements of the vector.
165+
pub fn as_mut_slice(&mut self) -> &mut [A::Item] {
166+
self.inner.as_mut_slice()
167+
}
168+
}
169+
170+
impl<A: Array + Copy> ops::Deref for ArrayVecCopy<A> {
171+
type Target = [A::Item];
172+
#[inline]
173+
fn deref(&self) -> &[A::Item] {
174+
self.inner.deref()
175+
}
176+
}
177+
178+
impl<A: Array + Copy> ops::DerefMut for ArrayVecCopy<A> {
179+
#[inline]
180+
fn deref_mut(&mut self) -> &mut [A::Item] {
181+
self.inner.deref_mut()
182+
}
183+
}
184+
185+
/// Create an `ArrayVecCopy` from an array.
186+
impl<A: Array + Copy> From<A> for ArrayVecCopy<A> {
187+
fn from(array: A) -> Self {
188+
ArrayVecCopy { inner: RawArrayVec::from(array) }
189+
}
190+
}
191+
192+
193+
/// Iterate the `ArrayVecCopy` with references to each element.
194+
impl<'a, A: Array + Copy> IntoIterator for &'a ArrayVecCopy<A> {
195+
type Item = &'a A::Item;
196+
type IntoIter = slice::Iter<'a, A::Item>;
197+
fn into_iter(self) -> Self::IntoIter { self.inner.iter() }
198+
}
199+
200+
/// Iterate the `ArrayVecCopy` with mutable references to each element.
201+
impl<'a, A: Array + Copy> IntoIterator for &'a mut ArrayVecCopy<A> {
202+
type Item = &'a mut A::Item;
203+
type IntoIter = slice::IterMut<'a, A::Item>;
204+
fn into_iter(self) -> Self::IntoIter { self.inner.iter_mut() }
205+
}
206+
207+
/// Iterate the `ArrayVecCopy` with each element by value.
208+
///
209+
/// The vector is consumed by this operation.
210+
impl<A: Array + Copy> IntoIterator for ArrayVecCopy<A> {
211+
type Item = A::Item;
212+
type IntoIter = IntoIter<A>;
213+
fn into_iter(self) -> IntoIter<A> {
214+
IntoIter { index: Index::from(0), v: self }
215+
}
216+
}
217+
218+
219+
/// By-value iterator for `ArrayVecCopy`.
220+
pub struct IntoIter<A: Array + Copy> {
221+
index: A::Index,
222+
v: ArrayVecCopy<A>,
223+
}
224+
225+
impl<A: Array + Copy> Iterator for IntoIter<A> {
226+
type Item = A::Item;
227+
228+
#[inline]
229+
fn next(&mut self) -> Option<A::Item> {
230+
let index = self.index.to_usize();
231+
if index == self.v.len() {
232+
None
233+
} else {
234+
unsafe {
235+
self.index = Index::from(index + 1);
236+
Some(ptr::read(self.v.get_unchecked_mut(index)))
237+
}
238+
}
239+
}
240+
241+
#[inline]
242+
fn size_hint(&self) -> (usize, Option<usize>) {
243+
let len = self.v.len() - self.index.to_usize();
244+
(len, Some(len))
245+
}
246+
}
247+
248+
impl<A: Array + Copy> DoubleEndedIterator for IntoIter<A> {
249+
#[inline]
250+
fn next_back(&mut self) -> Option<A::Item> {
251+
if self.index.to_usize() == self.v.len() {
252+
None
253+
} else {
254+
unsafe {
255+
let new_len = self.v.len() - 1;
256+
self.v.set_len(new_len);
257+
Some(ptr::read(self.v.get_unchecked_mut(new_len)))
258+
}
259+
}
260+
}
261+
}
262+
263+
impl<A: Array + Copy> ExactSizeIterator for IntoIter<A> { }
264+
265+
/// Extend the `ArrayVecCopy` with an iterator.
266+
///
267+
/// Does not extract more items than there is space for. No error
268+
/// occurs if there are more iterator elements.
269+
impl<A: Array + Copy> Extend<A::Item> for ArrayVecCopy<A> {
270+
fn extend<T: IntoIterator<Item=A::Item>>(&mut self, iter: T) {
271+
self.inner.extend(iter)
272+
}
273+
}
274+
275+
/// Create an `ArrayVecCopy` from an iterator.
276+
///
277+
/// Does not extract more items than there is space for. No error
278+
/// occurs if there are more iterator elements.
279+
impl<A: Array + Copy> iter::FromIterator<A::Item> for ArrayVecCopy<A> {
280+
fn from_iter<T: IntoIterator<Item=A::Item>>(iter: T) -> Self {
281+
ArrayVecCopy { inner: RawArrayVec::from_iter(iter) }
282+
}
283+
}
284+
285+
impl<A: Array + Copy> Clone for ArrayVecCopy<A>
286+
where A::Item: Clone
287+
{
288+
#[inline]
289+
fn clone(&self) -> Self {
290+
ArrayVecCopy { inner: self.inner.clone() }
291+
}
292+
293+
#[inline]
294+
fn clone_from(&mut self, rhs: &Self) {
295+
self.inner.clone_from(&rhs.inner)
296+
}
297+
}
298+
299+
impl<A: Array + Copy> Hash for ArrayVecCopy<A>
300+
where A::Item: Hash
301+
{
302+
fn hash<H: Hasher>(&self, state: &mut H) {
303+
self.inner.hash(state)
304+
}
305+
}
306+
307+
impl<A: Array + Copy> PartialEq for ArrayVecCopy<A>
308+
where A::Item: PartialEq
309+
{
310+
fn eq(&self, other: &Self) -> bool {
311+
use std::ops::Deref;
312+
self.inner.eq(other.inner.deref())
313+
}
314+
}
315+
316+
impl<A: Array + Copy> PartialEq<[A::Item]> for ArrayVecCopy<A>
317+
where A::Item: PartialEq
318+
{
319+
fn eq(&self, other: &[A::Item]) -> bool {
320+
self.inner.eq(other)
321+
}
322+
}
323+
324+
impl<A: Array + Copy> Eq for ArrayVecCopy<A> where A::Item: Eq { }
325+
326+
impl<A: Array + Copy> Borrow<[A::Item]> for ArrayVecCopy<A> {
327+
fn borrow(&self) -> &[A::Item] { self.inner.borrow() }
328+
}
329+
330+
impl<A: Array + Copy> BorrowMut<[A::Item]> for ArrayVecCopy<A> {
331+
fn borrow_mut(&mut self) -> &mut [A::Item] { self.inner.borrow_mut() }
332+
}
333+
334+
impl<A: Array + Copy> AsRef<[A::Item]> for ArrayVecCopy<A> {
335+
fn as_ref(&self) -> &[A::Item] { self.inner.as_ref() }
336+
}
337+
338+
impl<A: Array + Copy> AsMut<[A::Item]> for ArrayVecCopy<A> {
339+
fn as_mut(&mut self) -> &mut [A::Item] { self.inner.as_mut() }
340+
}
341+
342+
impl<A: Array + Copy> fmt::Debug for ArrayVecCopy<A> where A::Item: fmt::Debug {
343+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.inner.fmt(f) }
344+
}
345+
346+
impl<A: Array + Copy> Default for ArrayVecCopy<A> {
347+
fn default() -> ArrayVecCopy<A> {
348+
ArrayVecCopy::new()
349+
}
350+
}
351+
352+
impl<A: Array + Copy> PartialOrd for ArrayVecCopy<A> where A::Item: PartialOrd {
353+
#[inline]
354+
fn partial_cmp(&self, other: &ArrayVecCopy<A>) -> Option<cmp::Ordering> {
355+
self.inner.partial_cmp(&other.inner)
356+
}
357+
358+
#[inline] fn lt(&self, other: &Self) -> bool { self.inner.lt(&other.inner) }
359+
#[inline] fn le(&self, other: &Self) -> bool { self.inner.le(&other.inner) }
360+
#[inline] fn ge(&self, other: &Self) -> bool { self.inner.ge(&other.inner) }
361+
#[inline] fn gt(&self, other: &Self) -> bool { self.inner.gt(&other.inner) }
362+
}
363+
364+
impl<A: Array + Copy> Ord for ArrayVecCopy<A> where A::Item: Ord {
365+
fn cmp(&self, other: &ArrayVecCopy<A>) -> cmp::Ordering {
366+
self.inner.cmp(&other.inner)
367+
}
368+
}
369+
370+
#[cfg(feature="std")]
371+
/// `Write` appends written data to the end of the vector.
372+
///
373+
/// Requires `features="std"`.
374+
impl<A: Array<Item=u8> + Copy> io::Write for ArrayVecCopy<A> {
375+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
376+
self.inner.write(data)
377+
}
378+
fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
379+
}

0 commit comments

Comments
 (0)