Merge pull request #172 from bluss/min-const-gen
Use const generics in ArrayVec
This commit is contained in:
@@ -13,15 +13,14 @@ env:
|
||||
jobs:
|
||||
tests:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- rust: 1.36.0 # MSRV
|
||||
- rust: 1.51.0 # MSRV
|
||||
features: serde
|
||||
- rust: stable
|
||||
features: serde
|
||||
- rust: stable
|
||||
features: array-sizes-33-128 array-sizes-129-255
|
||||
features:
|
||||
- rust: beta
|
||||
features: serde
|
||||
- rust: nightly
|
||||
|
||||
@@ -39,9 +39,6 @@ default = ["std"]
|
||||
std = []
|
||||
unstable-const-fn = []
|
||||
|
||||
array-sizes-33-128 = []
|
||||
array-sizes-129-255 = []
|
||||
|
||||
[profile.bench]
|
||||
debug = true
|
||||
[profile.release]
|
||||
|
||||
@@ -7,7 +7,7 @@ use arrayvec::ArrayString;
|
||||
use bencher::Bencher;
|
||||
|
||||
fn try_push_c(b: &mut Bencher) {
|
||||
let mut v = ArrayString::<[u8; 512]>::new();
|
||||
let mut v = ArrayString::<512>::new();
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
while v.try_push('c').is_ok() {
|
||||
@@ -18,7 +18,7 @@ fn try_push_c(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn try_push_alpha(b: &mut Bencher) {
|
||||
let mut v = ArrayString::<[u8; 512]>::new();
|
||||
let mut v = ArrayString::<512>::new();
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
while v.try_push('α').is_ok() {
|
||||
@@ -30,7 +30,7 @@ fn try_push_alpha(b: &mut Bencher) {
|
||||
|
||||
// Yes, pushing a string char-by-char is slow. Use .push_str.
|
||||
fn try_push_string(b: &mut Bencher) {
|
||||
let mut v = ArrayString::<[u8; 512]>::new();
|
||||
let mut v = ArrayString::<512>::new();
|
||||
let input = "abcαβγ“”";
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
@@ -45,7 +45,7 @@ fn try_push_string(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn push_c(b: &mut Bencher) {
|
||||
let mut v = ArrayString::<[u8; 512]>::new();
|
||||
let mut v = ArrayString::<512>::new();
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
while !v.is_full() {
|
||||
@@ -57,7 +57,7 @@ fn push_c(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn push_alpha(b: &mut Bencher) {
|
||||
let mut v = ArrayString::<[u8; 512]>::new();
|
||||
let mut v = ArrayString::<512>::new();
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
while !v.is_full() {
|
||||
@@ -69,7 +69,7 @@ fn push_alpha(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn push_string(b: &mut Bencher) {
|
||||
let mut v = ArrayString::<[u8; 512]>::new();
|
||||
let mut v = ArrayString::<512>::new();
|
||||
let input = "abcαβγ“”";
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
|
||||
+5
-5
@@ -10,7 +10,7 @@ use bencher::Bencher;
|
||||
use bencher::black_box;
|
||||
|
||||
fn extend_with_constant(b: &mut Bencher) {
|
||||
let mut v = ArrayVec::<[u8; 512]>::new();
|
||||
let mut v = ArrayVec::<u8, 512>::new();
|
||||
let cap = v.capacity();
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
@@ -22,7 +22,7 @@ fn extend_with_constant(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn extend_with_range(b: &mut Bencher) {
|
||||
let mut v = ArrayVec::<[u8; 512]>::new();
|
||||
let mut v = ArrayVec::<u8, 512>::new();
|
||||
let cap = v.capacity();
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
@@ -34,7 +34,7 @@ fn extend_with_range(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn extend_with_slice(b: &mut Bencher) {
|
||||
let mut v = ArrayVec::<[u8; 512]>::new();
|
||||
let mut v = ArrayVec::<u8, 512>::new();
|
||||
let data = [1; 512];
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
@@ -46,7 +46,7 @@ fn extend_with_slice(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn extend_with_write(b: &mut Bencher) {
|
||||
let mut v = ArrayVec::<[u8; 512]>::new();
|
||||
let mut v = ArrayVec::<u8, 512>::new();
|
||||
let data = [1; 512];
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
@@ -57,7 +57,7 @@ fn extend_with_write(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn extend_from_slice(b: &mut Bencher) {
|
||||
let mut v = ArrayVec::<[u8; 512]>::new();
|
||||
let mut v = ArrayVec::<u8, 512>::new();
|
||||
let data = [1; 512];
|
||||
b.iter(|| {
|
||||
v.clear();
|
||||
|
||||
-151
@@ -1,151 +0,0 @@
|
||||
|
||||
/// Trait for fixed size arrays.
|
||||
///
|
||||
/// This trait is implemented for some specific array sizes, see
|
||||
/// the implementor list below. At the current state of Rust we can't
|
||||
/// make this fully general for every array size.
|
||||
///
|
||||
/// The following crate features add more array sizes (and they are not
|
||||
/// enabled by default due to their impact on compliation speed).
|
||||
///
|
||||
/// - `array-sizes-33-128`: All sizes 33 to 128 are implemented
|
||||
/// (a few in this range are included by default).
|
||||
/// - `array-sizes-129-255`: All sizes 129 to 255 are implemented
|
||||
/// (a few in this range are included by default).
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// This trait can *only* be implemented by fixed-size arrays or types with
|
||||
/// *exactly* the representation of a fixed size array (of the right element
|
||||
/// type and capacity).
|
||||
///
|
||||
/// Normally this trait is an implementation detail of arrayvec and doesn’t
|
||||
/// need implementing.
|
||||
pub unsafe trait Array {
|
||||
/// The array’s element type
|
||||
type Item;
|
||||
/// The smallest type that can index and tell the length of the array.
|
||||
#[doc(hidden)]
|
||||
type Index: Index;
|
||||
/// The array's element capacity
|
||||
const CAPACITY: usize;
|
||||
fn as_slice(&self) -> &[Self::Item];
|
||||
fn as_mut_slice(&mut self) -> &mut [Self::Item];
|
||||
}
|
||||
|
||||
pub trait Index : PartialEq + Copy {
|
||||
const ZERO: Self;
|
||||
fn to_usize(self) -> usize;
|
||||
fn from(_: usize) -> Self;
|
||||
}
|
||||
|
||||
impl Index for () {
|
||||
const ZERO: Self = ();
|
||||
#[inline(always)]
|
||||
fn to_usize(self) -> usize { 0 }
|
||||
#[inline(always)]
|
||||
fn from(_ix: usize) -> Self { () }
|
||||
}
|
||||
|
||||
impl Index for bool {
|
||||
const ZERO: Self = false;
|
||||
#[inline(always)]
|
||||
fn to_usize(self) -> usize { self as usize }
|
||||
#[inline(always)]
|
||||
fn from(ix: usize) -> Self { ix != 0 }
|
||||
}
|
||||
|
||||
impl Index for u8 {
|
||||
const ZERO: Self = 0;
|
||||
#[inline(always)]
|
||||
fn to_usize(self) -> usize { self as usize }
|
||||
#[inline(always)]
|
||||
fn from(ix: usize) -> Self { ix as u8 }
|
||||
}
|
||||
|
||||
impl Index for u16 {
|
||||
const ZERO: Self = 0;
|
||||
#[inline(always)]
|
||||
fn to_usize(self) -> usize { self as usize }
|
||||
#[inline(always)]
|
||||
fn from(ix: usize) -> Self { ix as u16 }
|
||||
}
|
||||
|
||||
impl Index for u32 {
|
||||
const ZERO: Self = 0;
|
||||
#[inline(always)]
|
||||
fn to_usize(self) -> usize { self as usize }
|
||||
#[inline(always)]
|
||||
fn from(ix: usize) -> Self { ix as u32 }
|
||||
}
|
||||
|
||||
impl Index for usize {
|
||||
const ZERO: Self = 0;
|
||||
#[inline(always)]
|
||||
fn to_usize(self) -> usize { self }
|
||||
#[inline(always)]
|
||||
fn from(ix: usize) -> Self { ix }
|
||||
}
|
||||
|
||||
macro_rules! fix_array_impl {
|
||||
($index_type:ty, $len:expr ) => (
|
||||
unsafe impl<T> Array for [T; $len] {
|
||||
type Item = T;
|
||||
type Index = $index_type;
|
||||
const CAPACITY: usize = $len;
|
||||
#[doc(hidden)]
|
||||
fn as_slice(&self) -> &[Self::Item] { self }
|
||||
#[doc(hidden)]
|
||||
fn as_mut_slice(&mut self) -> &mut [Self::Item] { self }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! fix_array_impl_recursive {
|
||||
($index_type:ty, ) => ();
|
||||
($index_type:ty, $($len:expr,)*) => (
|
||||
$(fix_array_impl!($index_type, $len);)*
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
fix_array_impl_recursive!((), 0,);
|
||||
fix_array_impl_recursive!(bool, 1,);
|
||||
fix_array_impl_recursive!(u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
|
||||
28, 29, 30, 31, );
|
||||
|
||||
#[cfg(not(feature="array-sizes-33-128"))]
|
||||
fix_array_impl_recursive!(u8, 32, 40, 48, 50, 56, 64, 72, 96, 100, 128, );
|
||||
|
||||
#[cfg(feature="array-sizes-33-128")]
|
||||
fix_array_impl_recursive!(u8,
|
||||
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
|
||||
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
|
||||
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
|
||||
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
|
||||
125, 126, 127, 128,
|
||||
);
|
||||
|
||||
#[cfg(not(feature="array-sizes-129-255"))]
|
||||
fix_array_impl_recursive!(u8, 160, 192, 200, 224,);
|
||||
|
||||
#[cfg(feature="array-sizes-129-255")]
|
||||
fix_array_impl_recursive!(u8,
|
||||
129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
|
||||
141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
|
||||
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
|
||||
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
|
||||
189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
|
||||
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
|
||||
221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
|
||||
237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
|
||||
253, 254, 255,
|
||||
);
|
||||
|
||||
fix_array_impl_recursive!(u16, 256, 384, 512, 768, 1024, 2048, 4096, 8192, 16384, 32768,);
|
||||
// This array size doesn't exist on 16-bit
|
||||
#[cfg(any(target_pointer_width="32", target_pointer_width="64"))]
|
||||
fix_array_impl_recursive!(u32, 1 << 16,);
|
||||
|
||||
+84
-109
@@ -3,6 +3,7 @@ use std::cmp;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
@@ -10,16 +11,12 @@ use std::str;
|
||||
use std::str::FromStr;
|
||||
use std::str::Utf8Error;
|
||||
|
||||
use crate::array::Array;
|
||||
use crate::array::Index;
|
||||
use crate::CapacityError;
|
||||
use crate::char::encode_utf8;
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
||||
|
||||
use super::MaybeUninit as MaybeUninitCopy;
|
||||
|
||||
/// A string with a fixed capacity.
|
||||
///
|
||||
/// The `ArrayString` is a string backed by a fixed size array. It keeps track
|
||||
@@ -28,24 +25,21 @@ use super::MaybeUninit as MaybeUninitCopy;
|
||||
/// The string is a contiguous value that you can store directly on the stack
|
||||
/// if needed.
|
||||
#[derive(Copy)]
|
||||
pub struct ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
{
|
||||
xs: MaybeUninitCopy<A>,
|
||||
len: A::Index,
|
||||
pub struct ArrayString<const CAP: usize> {
|
||||
// the `len` first elements of the array are initialized
|
||||
xs: [MaybeUninit<u8>; CAP],
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<A> Default for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Default for ArrayString<CAP>
|
||||
{
|
||||
/// Return an empty `ArrayString`
|
||||
fn default() -> ArrayString<A> {
|
||||
fn default() -> ArrayString<CAP> {
|
||||
ArrayString::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> ArrayString<CAP>
|
||||
{
|
||||
/// Create a new empty `ArrayString`.
|
||||
///
|
||||
@@ -54,34 +48,28 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 16]>::new();
|
||||
/// let mut string = ArrayString::<16>::new();
|
||||
/// string.push_str("foo");
|
||||
/// assert_eq!(&string[..], "foo");
|
||||
/// assert_eq!(string.capacity(), 16);
|
||||
/// ```
|
||||
#[cfg(not(feature="unstable-const-fn"))]
|
||||
pub fn new() -> ArrayString<A> {
|
||||
pub fn new() -> ArrayString<CAP> {
|
||||
unsafe {
|
||||
ArrayString {
|
||||
xs: MaybeUninitCopy::uninitialized(),
|
||||
len: Index::ZERO,
|
||||
}
|
||||
ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="unstable-const-fn")]
|
||||
pub const fn new() -> ArrayString<A> {
|
||||
pub const fn new() -> ArrayString<CAP> {
|
||||
unsafe {
|
||||
ArrayString {
|
||||
xs: MaybeUninitCopy::uninitialized(),
|
||||
len: Index::ZERO,
|
||||
}
|
||||
ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the length of the string.
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize { self.len.to_usize() }
|
||||
pub fn len(&self) -> usize { self.len }
|
||||
|
||||
/// Returns whether the string is empty.
|
||||
#[inline]
|
||||
@@ -96,7 +84,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 3]>::from("foo").unwrap();
|
||||
/// let mut string = ArrayString::<3>::from("foo").unwrap();
|
||||
/// assert_eq!(&string[..], "foo");
|
||||
/// assert_eq!(string.len(), 3);
|
||||
/// assert_eq!(string.capacity(), 3);
|
||||
@@ -116,13 +104,16 @@ impl<A> ArrayString<A>
|
||||
///
|
||||
/// let string = ArrayString::from_byte_string(b"hello world").unwrap();
|
||||
/// ```
|
||||
pub fn from_byte_string(b: &A) -> Result<Self, Utf8Error> {
|
||||
let len = str::from_utf8(b.as_slice())?.len();
|
||||
debug_assert_eq!(len, A::CAPACITY);
|
||||
Ok(ArrayString {
|
||||
xs: MaybeUninitCopy::from(*b),
|
||||
len: Index::from(A::CAPACITY),
|
||||
})
|
||||
pub fn from_byte_string(b: &[u8; CAP]) -> Result<Self, Utf8Error> {
|
||||
let len = str::from_utf8(b)?.len();
|
||||
debug_assert_eq!(len, CAP);
|
||||
let mut vec = Self::new();
|
||||
unsafe {
|
||||
(b as *const [u8; CAP] as *const [MaybeUninit<u8>; CAP])
|
||||
.copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit<u8>; CAP], 1);
|
||||
vec.set_len(CAP);
|
||||
}
|
||||
Ok(vec)
|
||||
}
|
||||
|
||||
/// Return the capacity of the `ArrayString`.
|
||||
@@ -130,18 +121,18 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let string = ArrayString::<[_; 3]>::new();
|
||||
/// let string = ArrayString::<3>::new();
|
||||
/// assert_eq!(string.capacity(), 3);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn capacity(&self) -> usize { A::CAPACITY }
|
||||
pub fn capacity(&self) -> usize { CAP }
|
||||
|
||||
/// Return if the `ArrayString` is completely filled.
|
||||
///
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 1]>::new();
|
||||
/// let mut string = ArrayString::<1>::new();
|
||||
/// assert!(!string.is_full());
|
||||
/// string.push_str("A");
|
||||
/// assert!(string.is_full());
|
||||
@@ -155,7 +146,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 2]>::new();
|
||||
/// let mut string = ArrayString::<2>::new();
|
||||
///
|
||||
/// string.push('a');
|
||||
/// string.push('b');
|
||||
@@ -175,7 +166,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 2]>::new();
|
||||
/// let mut string = ArrayString::<2>::new();
|
||||
///
|
||||
/// string.try_push('a').unwrap();
|
||||
/// string.try_push('b').unwrap();
|
||||
@@ -187,7 +178,7 @@ impl<A> ArrayString<A>
|
||||
pub fn try_push(&mut self, c: char) -> Result<(), CapacityError<char>> {
|
||||
let len = self.len();
|
||||
unsafe {
|
||||
let ptr = self.xs.ptr_mut().add(len);
|
||||
let ptr = self.as_mut_ptr().add(len);
|
||||
let remaining_cap = self.capacity() - len;
|
||||
match encode_utf8(c, ptr, remaining_cap) {
|
||||
Ok(n) => {
|
||||
@@ -206,7 +197,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 2]>::new();
|
||||
/// let mut string = ArrayString::<2>::new();
|
||||
///
|
||||
/// string.push_str("a");
|
||||
/// string.push_str("d");
|
||||
@@ -226,7 +217,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 2]>::new();
|
||||
/// let mut string = ArrayString::<2>::new();
|
||||
///
|
||||
/// string.try_push_str("a").unwrap();
|
||||
/// let overflow1 = string.try_push_str("bc");
|
||||
@@ -242,7 +233,7 @@ impl<A> ArrayString<A>
|
||||
return Err(CapacityError::new(s));
|
||||
}
|
||||
unsafe {
|
||||
let dst = self.xs.ptr_mut().add(self.len());
|
||||
let dst = self.as_mut_ptr().add(self.len());
|
||||
let src = s.as_ptr();
|
||||
ptr::copy_nonoverlapping(src, dst, s.len());
|
||||
let newl = self.len() + s.len();
|
||||
@@ -258,7 +249,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut s = ArrayString::<[_; 3]>::from("foo").unwrap();
|
||||
/// let mut s = ArrayString::<3>::from("foo").unwrap();
|
||||
///
|
||||
/// assert_eq!(s.pop(), Some('o'));
|
||||
/// assert_eq!(s.pop(), Some('o'));
|
||||
@@ -288,7 +279,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut string = ArrayString::<[_; 6]>::from("foobar").unwrap();
|
||||
/// let mut string = ArrayString::<6>::from("foobar").unwrap();
|
||||
/// string.truncate(3);
|
||||
/// assert_eq!(&string[..], "foo");
|
||||
/// string.truncate(4);
|
||||
@@ -318,7 +309,7 @@ impl<A> ArrayString<A>
|
||||
/// ```
|
||||
/// use arrayvec::ArrayString;
|
||||
///
|
||||
/// let mut s = ArrayString::<[_; 3]>::from("foo").unwrap();
|
||||
/// let mut s = ArrayString::<3>::from("foo").unwrap();
|
||||
///
|
||||
/// assert_eq!(s.remove(0), 'f');
|
||||
/// assert_eq!(s.remove(1), 'o');
|
||||
@@ -333,8 +324,8 @@ impl<A> ArrayString<A>
|
||||
let next = idx + ch.len_utf8();
|
||||
let len = self.len();
|
||||
unsafe {
|
||||
ptr::copy(self.xs.ptr().add(next),
|
||||
self.xs.ptr_mut().add(idx),
|
||||
ptr::copy(self.as_ptr().add(next),
|
||||
self.as_mut_ptr().add(idx),
|
||||
len - next);
|
||||
self.set_len(len - (next - idx));
|
||||
}
|
||||
@@ -357,103 +348,99 @@ impl<A> ArrayString<A>
|
||||
/// and may use other debug assertions.
|
||||
pub unsafe fn set_len(&mut self, length: usize) {
|
||||
debug_assert!(length <= self.capacity());
|
||||
self.len = Index::from(length);
|
||||
self.len = length;
|
||||
}
|
||||
|
||||
/// Return a string slice of the whole `ArrayString`.
|
||||
pub fn as_str(&self) -> &str {
|
||||
self
|
||||
}
|
||||
|
||||
fn as_ptr(&self) -> *const u8 {
|
||||
self.xs.as_ptr() as *const u8
|
||||
}
|
||||
|
||||
fn as_mut_ptr(&mut self) -> *mut u8 {
|
||||
self.xs.as_mut_ptr() as *mut u8
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> Deref for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Deref for ArrayString<CAP>
|
||||
{
|
||||
type Target = str;
|
||||
#[inline]
|
||||
fn deref(&self) -> &str {
|
||||
unsafe {
|
||||
let sl = slice::from_raw_parts(self.xs.ptr(), self.len.to_usize());
|
||||
let sl = slice::from_raw_parts(self.as_ptr(), self.len);
|
||||
str::from_utf8_unchecked(sl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> DerefMut for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> DerefMut for ArrayString<CAP>
|
||||
{
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut str {
|
||||
unsafe {
|
||||
let sl = slice::from_raw_parts_mut(self.xs.ptr_mut(), self.len.to_usize());
|
||||
let sl = slice::from_raw_parts_mut(self.as_mut_ptr(), self.len);
|
||||
str::from_utf8_unchecked_mut(sl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> PartialEq for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> PartialEq for ArrayString<CAP>
|
||||
{
|
||||
fn eq(&self, rhs: &Self) -> bool {
|
||||
**self == **rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> PartialEq<str> for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> PartialEq<str> for ArrayString<CAP>
|
||||
{
|
||||
fn eq(&self, rhs: &str) -> bool {
|
||||
&**self == rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> PartialEq<ArrayString<A>> for str
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> PartialEq<ArrayString<CAP>> for str
|
||||
{
|
||||
fn eq(&self, rhs: &ArrayString<A>) -> bool {
|
||||
fn eq(&self, rhs: &ArrayString<CAP>) -> bool {
|
||||
self == &**rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> Eq for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Eq for ArrayString<CAP>
|
||||
{ }
|
||||
|
||||
impl<A> Hash for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Hash for ArrayString<CAP>
|
||||
{
|
||||
fn hash<H: Hasher>(&self, h: &mut H) {
|
||||
(**self).hash(h)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> Borrow<str> for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Borrow<str> for ArrayString<CAP>
|
||||
{
|
||||
fn borrow(&self) -> &str { self }
|
||||
}
|
||||
|
||||
impl<A> AsRef<str> for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> AsRef<str> for ArrayString<CAP>
|
||||
{
|
||||
fn as_ref(&self) -> &str { self }
|
||||
}
|
||||
|
||||
impl<A> fmt::Debug for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> fmt::Debug for ArrayString<CAP>
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
|
||||
}
|
||||
|
||||
impl<A> fmt::Display for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> fmt::Display for ArrayString<CAP>
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
|
||||
}
|
||||
|
||||
/// `Write` appends written data to the end of the string.
|
||||
impl<A> fmt::Write for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> fmt::Write for ArrayString<CAP>
|
||||
{
|
||||
fn write_char(&mut self, c: char) -> fmt::Result {
|
||||
self.try_push(c).map_err(|_| fmt::Error)
|
||||
@@ -464,10 +451,9 @@ impl<A> fmt::Write for ArrayString<A>
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> Clone for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Clone for ArrayString<CAP>
|
||||
{
|
||||
fn clone(&self) -> ArrayString<A> {
|
||||
fn clone(&self) -> ArrayString<CAP> {
|
||||
*self
|
||||
}
|
||||
fn clone_from(&mut self, rhs: &Self) {
|
||||
@@ -477,8 +463,7 @@ impl<A> Clone for ArrayString<A>
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> PartialOrd for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> PartialOrd for ArrayString<CAP>
|
||||
{
|
||||
fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
|
||||
(**self).partial_cmp(&**rhs)
|
||||
@@ -489,8 +474,7 @@ impl<A> PartialOrd for ArrayString<A>
|
||||
fn ge(&self, rhs: &Self) -> bool { **self >= **rhs }
|
||||
}
|
||||
|
||||
impl<A> PartialOrd<str> for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
|
||||
{
|
||||
fn partial_cmp(&self, rhs: &str) -> Option<cmp::Ordering> {
|
||||
(**self).partial_cmp(rhs)
|
||||
@@ -501,28 +485,25 @@ impl<A> PartialOrd<str> for ArrayString<A>
|
||||
fn ge(&self, rhs: &str) -> bool { &**self >= rhs }
|
||||
}
|
||||
|
||||
impl<A> PartialOrd<ArrayString<A>> for str
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str
|
||||
{
|
||||
fn partial_cmp(&self, rhs: &ArrayString<A>) -> Option<cmp::Ordering> {
|
||||
fn partial_cmp(&self, rhs: &ArrayString<CAP>) -> Option<cmp::Ordering> {
|
||||
self.partial_cmp(&**rhs)
|
||||
}
|
||||
fn lt(&self, rhs: &ArrayString<A>) -> bool { self < &**rhs }
|
||||
fn le(&self, rhs: &ArrayString<A>) -> bool { self <= &**rhs }
|
||||
fn gt(&self, rhs: &ArrayString<A>) -> bool { self > &**rhs }
|
||||
fn ge(&self, rhs: &ArrayString<A>) -> bool { self >= &**rhs }
|
||||
fn lt(&self, rhs: &ArrayString<CAP>) -> bool { self < &**rhs }
|
||||
fn le(&self, rhs: &ArrayString<CAP>) -> bool { self <= &**rhs }
|
||||
fn gt(&self, rhs: &ArrayString<CAP>) -> bool { self > &**rhs }
|
||||
fn ge(&self, rhs: &ArrayString<CAP>) -> bool { self >= &**rhs }
|
||||
}
|
||||
|
||||
impl<A> Ord for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Ord for ArrayString<CAP>
|
||||
{
|
||||
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
|
||||
(**self).cmp(&**rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> FromStr for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> FromStr for ArrayString<CAP>
|
||||
{
|
||||
type Err = CapacityError;
|
||||
|
||||
@@ -533,8 +514,7 @@ impl<A> FromStr for ArrayString<A>
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
/// Requires crate feature `"serde"`
|
||||
impl<A> Serialize for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<const CAP: usize> Serialize for ArrayString<CAP>
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
@@ -545,8 +525,7 @@ impl<A> Serialize for ArrayString<A>
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
/// Requires crate feature `"serde"`
|
||||
impl<'de, A> Deserialize<'de> for ArrayString<A>
|
||||
where A: Array<Item=u8> + Copy
|
||||
impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
@@ -554,13 +533,13 @@ impl<'de, A> Deserialize<'de> for ArrayString<A>
|
||||
use serde::de::{self, Visitor};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
struct ArrayStringVisitor<A: Array<Item=u8>>(PhantomData<A>);
|
||||
struct ArrayStringVisitor<const CAP: usize>(PhantomData<[u8; CAP]>);
|
||||
|
||||
impl<'de, A: Copy + Array<Item=u8>> Visitor<'de> for ArrayStringVisitor<A> {
|
||||
type Value = ArrayString<A>;
|
||||
impl<'de, const CAP: usize> Visitor<'de> for ArrayStringVisitor<CAP> {
|
||||
type Value = ArrayString<CAP>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a string no more than {} bytes long", A::CAPACITY)
|
||||
write!(formatter, "a string no more than {} bytes long", CAP)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
@@ -578,13 +557,11 @@ impl<'de, A> Deserialize<'de> for ArrayString<A>
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_str(ArrayStringVisitor::<A>(PhantomData))
|
||||
deserializer.deserialize_str(ArrayStringVisitor(PhantomData))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, A> TryFrom<&'a str> for ArrayString<A>
|
||||
where
|
||||
A: Array<Item = u8> + Copy
|
||||
impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
|
||||
{
|
||||
type Error = CapacityError<&'a str>;
|
||||
|
||||
@@ -595,9 +572,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, A> TryFrom<fmt::Arguments<'a>> for ArrayString<A>
|
||||
where
|
||||
A: Array<Item = u8> + Copy
|
||||
impl<'a, const CAP: usize> TryFrom<fmt::Arguments<'a>> for ArrayString<CAP>
|
||||
{
|
||||
type Error = CapacityError<fmt::Error>;
|
||||
|
||||
|
||||
+214
-221
@@ -14,15 +14,14 @@ use std::fmt;
|
||||
#[cfg(feature="std")]
|
||||
use std::io;
|
||||
|
||||
use crate::maybe_uninit::MaybeUninit;
|
||||
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
||||
|
||||
use crate::array::Array;
|
||||
use crate::errors::CapacityError;
|
||||
use crate::array::Index;
|
||||
use crate::arrayvec_impl::ArrayVecImpl;
|
||||
|
||||
/// A vector with a fixed capacity.
|
||||
///
|
||||
@@ -36,12 +35,13 @@ use crate::array::Index;
|
||||
/// that the full slice API is available.
|
||||
///
|
||||
/// ArrayVec can be converted into a by value iterator.
|
||||
pub struct ArrayVec<A: Array> {
|
||||
xs: MaybeUninit<A>,
|
||||
len: A::Index,
|
||||
pub struct ArrayVec<T, const CAP: usize> {
|
||||
// the `len` first elements of the array are initialized
|
||||
xs: [MaybeUninit<T>; CAP],
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<A: Array> Drop for ArrayVec<A> {
|
||||
impl<T, const CAP: usize> Drop for ArrayVec<T, CAP> {
|
||||
fn drop(&mut self) {
|
||||
self.clear();
|
||||
|
||||
@@ -56,7 +56,10 @@ macro_rules! panic_oob {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> ArrayVec<A> {
|
||||
impl<T, const CAP: usize> ArrayVec<T, CAP> {
|
||||
/// Capacity
|
||||
const CAPACITY: usize = CAP;
|
||||
|
||||
/// Create a new empty `ArrayVec`.
|
||||
///
|
||||
/// Capacity is inferred from the type parameter.
|
||||
@@ -64,23 +67,23 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 16]>::new();
|
||||
/// let mut array = ArrayVec::<_, 16>::new();
|
||||
/// array.push(1);
|
||||
/// array.push(2);
|
||||
/// assert_eq!(&array[..], &[1, 2]);
|
||||
/// assert_eq!(array.capacity(), 16);
|
||||
/// ```
|
||||
#[cfg(not(feature="unstable-const-fn"))]
|
||||
pub fn new() -> ArrayVec<A> {
|
||||
pub fn new() -> ArrayVec<T, CAP> {
|
||||
unsafe {
|
||||
ArrayVec { xs: MaybeUninit::uninitialized(), len: Index::ZERO }
|
||||
ArrayVec { xs: MaybeUninit::uninit().assume_init(), len: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="unstable-const-fn")]
|
||||
pub const fn new() -> ArrayVec<A> {
|
||||
pub const fn new() -> ArrayVec<T, CAP> {
|
||||
unsafe {
|
||||
ArrayVec { xs: MaybeUninit::uninitialized(), len: Index::ZERO }
|
||||
ArrayVec { xs: MaybeUninit::uninit().assume_init(), len: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +97,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// assert_eq!(array.len(), 2);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize { self.len.to_usize() }
|
||||
pub fn len(&self) -> usize { self.len as usize }
|
||||
|
||||
/// Returns whether the `ArrayVec` is empty.
|
||||
///
|
||||
@@ -117,14 +120,14 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// assert_eq!(array.capacity(), 3);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn capacity(&self) -> usize { A::CAPACITY }
|
||||
pub fn capacity(&self) -> usize { CAP }
|
||||
|
||||
/// Return if the `ArrayVec` is completely filled.
|
||||
///
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 1]>::new();
|
||||
/// let mut array = ArrayVec::<_, 1>::new();
|
||||
/// assert!(!array.is_full());
|
||||
/// array.push(1);
|
||||
/// assert!(array.is_full());
|
||||
@@ -151,15 +154,15 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 2]>::new();
|
||||
/// let mut array = ArrayVec::<_, 2>::new();
|
||||
///
|
||||
/// array.push(1);
|
||||
/// array.push(2);
|
||||
///
|
||||
/// assert_eq!(&array[..], &[1, 2]);
|
||||
/// ```
|
||||
pub fn push(&mut self, element: A::Item) {
|
||||
self.try_push(element).unwrap()
|
||||
pub fn push(&mut self, element: T) {
|
||||
ArrayVecImpl::push(self, element)
|
||||
}
|
||||
|
||||
/// Push `element` to the end of the vector.
|
||||
@@ -170,7 +173,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 2]>::new();
|
||||
/// let mut array = ArrayVec::<_, 2>::new();
|
||||
///
|
||||
/// let push1 = array.try_push(1);
|
||||
/// let push2 = array.try_push(2);
|
||||
@@ -184,18 +187,10 @@ impl<A: Array> ArrayVec<A> {
|
||||
///
|
||||
/// assert!(overflow.is_err());
|
||||
/// ```
|
||||
pub fn try_push(&mut self, element: A::Item) -> Result<(), CapacityError<A::Item>> {
|
||||
if self.len() < A::CAPACITY {
|
||||
unsafe {
|
||||
self.push_unchecked(element);
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err(CapacityError::new(element))
|
||||
}
|
||||
pub fn try_push(&mut self, element: T) -> Result<(), CapacityError<T>> {
|
||||
ArrayVecImpl::try_push(self, element)
|
||||
}
|
||||
|
||||
|
||||
/// Push `element` to the end of the vector without checking the capacity.
|
||||
///
|
||||
/// It is up to the caller to ensure the capacity of the vector is
|
||||
@@ -206,7 +201,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 2]>::new();
|
||||
/// let mut array = ArrayVec::<_, 2>::new();
|
||||
///
|
||||
/// if array.len() + 2 <= array.capacity() {
|
||||
/// unsafe {
|
||||
@@ -217,16 +212,38 @@ impl<A: Array> ArrayVec<A> {
|
||||
///
|
||||
/// assert_eq!(&array[..], &[1, 2]);
|
||||
/// ```
|
||||
pub unsafe fn push_unchecked(&mut self, element: A::Item) {
|
||||
let len = self.len();
|
||||
debug_assert!(len < A::CAPACITY);
|
||||
ptr::write(self.get_unchecked_ptr(len), element);
|
||||
self.set_len(len + 1);
|
||||
pub unsafe fn push_unchecked(&mut self, element: T) {
|
||||
ArrayVecImpl::push_unchecked(self, element)
|
||||
}
|
||||
|
||||
/// Shortens the vector, keeping the first `len` elements and dropping
|
||||
/// the rest.
|
||||
///
|
||||
/// If `len` is greater than the vector’s current length this has no
|
||||
/// effect.
|
||||
///
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::from([1, 2, 3, 4, 5]);
|
||||
/// array.truncate(3);
|
||||
/// assert_eq!(&array[..], &[1, 2, 3]);
|
||||
/// array.truncate(4);
|
||||
/// assert_eq!(&array[..], &[1, 2, 3]);
|
||||
/// ```
|
||||
pub fn truncate(&mut self, new_len: usize) {
|
||||
ArrayVecImpl::truncate(self, new_len)
|
||||
}
|
||||
|
||||
/// Remove all elements in the vector.
|
||||
pub fn clear(&mut self) {
|
||||
ArrayVecImpl::clear(self)
|
||||
}
|
||||
|
||||
|
||||
/// Get pointer to where element at `index` would be
|
||||
unsafe fn get_unchecked_ptr(&mut self, index: usize) -> *mut A::Item {
|
||||
self.xs.ptr_mut().add(index)
|
||||
unsafe fn get_unchecked_ptr(&mut self, index: usize) -> *mut T {
|
||||
self.as_mut_ptr().add(index)
|
||||
}
|
||||
|
||||
/// Insert `element` at position `index`.
|
||||
@@ -242,14 +259,14 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 2]>::new();
|
||||
/// let mut array = ArrayVec::<_, 2>::new();
|
||||
///
|
||||
/// array.insert(0, "x");
|
||||
/// array.insert(0, "y");
|
||||
/// assert_eq!(&array[..], &["y", "x"]);
|
||||
///
|
||||
/// ```
|
||||
pub fn insert(&mut self, index: usize, element: A::Item) {
|
||||
pub fn insert(&mut self, index: usize, element: T) {
|
||||
self.try_insert(index, element).unwrap()
|
||||
}
|
||||
|
||||
@@ -265,7 +282,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 2]>::new();
|
||||
/// let mut array = ArrayVec::<_, 2>::new();
|
||||
///
|
||||
/// assert!(array.try_insert(0, "x").is_ok());
|
||||
/// assert!(array.try_insert(0, "y").is_ok());
|
||||
@@ -273,7 +290,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// assert_eq!(&array[..], &["y", "x"]);
|
||||
///
|
||||
/// ```
|
||||
pub fn try_insert(&mut self, index: usize, element: A::Item) -> Result<(), CapacityError<A::Item>> {
|
||||
pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), CapacityError<T>> {
|
||||
if index > self.len() {
|
||||
panic_oob!("try_insert", index, self.len())
|
||||
}
|
||||
@@ -306,22 +323,15 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::<[_; 2]>::new();
|
||||
/// let mut array = ArrayVec::<_, 2>::new();
|
||||
///
|
||||
/// array.push(1);
|
||||
///
|
||||
/// assert_eq!(array.pop(), Some(1));
|
||||
/// assert_eq!(array.pop(), None);
|
||||
/// ```
|
||||
pub fn pop(&mut self) -> Option<A::Item> {
|
||||
if self.len() == 0 {
|
||||
return None;
|
||||
}
|
||||
unsafe {
|
||||
let new_len = self.len() - 1;
|
||||
self.set_len(new_len);
|
||||
Some(ptr::read(self.get_unchecked_ptr(new_len)))
|
||||
}
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
ArrayVecImpl::pop(self)
|
||||
}
|
||||
|
||||
/// Remove the element at `index` and swap the last element into its place.
|
||||
@@ -343,7 +353,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// assert_eq!(array.swap_remove(1), 2);
|
||||
/// assert_eq!(&array[..], &[3]);
|
||||
/// ```
|
||||
pub fn swap_remove(&mut self, index: usize) -> A::Item {
|
||||
pub fn swap_remove(&mut self, index: usize) -> T {
|
||||
self.swap_pop(index)
|
||||
.unwrap_or_else(|| {
|
||||
panic_oob!("swap_remove", index, self.len())
|
||||
@@ -367,7 +377,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
///
|
||||
/// assert_eq!(array.swap_pop(10), None);
|
||||
/// ```
|
||||
pub fn swap_pop(&mut self, index: usize) -> Option<A::Item> {
|
||||
pub fn swap_pop(&mut self, index: usize) -> Option<T> {
|
||||
let len = self.len();
|
||||
if index >= len {
|
||||
return None;
|
||||
@@ -391,7 +401,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// assert_eq!(removed_elt, 1);
|
||||
/// assert_eq!(&array[..], &[2, 3]);
|
||||
/// ```
|
||||
pub fn remove(&mut self, index: usize) -> A::Item {
|
||||
pub fn remove(&mut self, index: usize) -> T {
|
||||
self.pop_at(index)
|
||||
.unwrap_or_else(|| {
|
||||
panic_oob!("remove", index, self.len())
|
||||
@@ -414,7 +424,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// assert!(array.pop_at(2).is_none());
|
||||
/// assert!(array.pop_at(10).is_none());
|
||||
/// ```
|
||||
pub fn pop_at(&mut self, index: usize) -> Option<A::Item> {
|
||||
pub fn pop_at(&mut self, index: usize) -> Option<T> {
|
||||
if index >= self.len() {
|
||||
None
|
||||
} else {
|
||||
@@ -422,36 +432,6 @@ impl<A: Array> ArrayVec<A> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Shortens the vector, keeping the first `len` elements and dropping
|
||||
/// the rest.
|
||||
///
|
||||
/// If `len` is greater than the vector’s current length this has no
|
||||
/// effect.
|
||||
///
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut array = ArrayVec::from([1, 2, 3, 4, 5]);
|
||||
/// array.truncate(3);
|
||||
/// assert_eq!(&array[..], &[1, 2, 3]);
|
||||
/// array.truncate(4);
|
||||
/// assert_eq!(&array[..], &[1, 2, 3]);
|
||||
/// ```
|
||||
pub fn truncate(&mut self, new_len: usize) {
|
||||
unsafe {
|
||||
if new_len < self.len() {
|
||||
let tail: *mut [_] = &mut self[new_len..];
|
||||
self.len = Index::from(new_len);
|
||||
ptr::drop_in_place(tail);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove all elements in the vector.
|
||||
pub fn clear(&mut self) {
|
||||
self.truncate(0)
|
||||
}
|
||||
|
||||
/// Retains only the elements specified by the predicate.
|
||||
///
|
||||
/// In other words, remove all elements `e` such that `f(&mut e)` returns false.
|
||||
@@ -466,7 +446,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// assert_eq!(&array[..], &[1, 3]);
|
||||
/// ```
|
||||
pub fn retain<F>(&mut self, mut f: F)
|
||||
where F: FnMut(&mut A::Item) -> bool
|
||||
where F: FnMut(&mut T) -> bool
|
||||
{
|
||||
let len = self.len();
|
||||
let mut del = 0;
|
||||
@@ -495,7 +475,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// not greater than the capacity.
|
||||
pub unsafe fn set_len(&mut self, length: usize) {
|
||||
debug_assert!(length <= self.capacity());
|
||||
self.len = Index::from(length);
|
||||
self.len = length;
|
||||
}
|
||||
|
||||
/// Copy and appends all elements in a slice to the `ArrayVec`.
|
||||
@@ -503,7 +483,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
|
||||
/// let mut vec: ArrayVec<usize, 10> = ArrayVec::new();
|
||||
/// vec.push(1);
|
||||
/// vec.try_extend_from_slice(&[2, 3]).unwrap();
|
||||
/// assert_eq!(&vec[..], &[1, 2, 3]);
|
||||
@@ -516,8 +496,8 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// slice.
|
||||
///
|
||||
/// [`remaining_capacity`]: #method.remaining_capacity
|
||||
pub fn try_extend_from_slice(&mut self, other: &[A::Item]) -> Result<(), CapacityError>
|
||||
where A::Item: Copy,
|
||||
pub fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), CapacityError>
|
||||
where T: Copy,
|
||||
{
|
||||
if self.remaining_capacity() < other.len() {
|
||||
return Err(CapacityError::new(()));
|
||||
@@ -527,7 +507,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
let other_len = other.len();
|
||||
|
||||
unsafe {
|
||||
let dst = self.xs.ptr_mut().add(self_len);
|
||||
let dst = self.get_unchecked_ptr(self_len);
|
||||
ptr::copy_nonoverlapping(other.as_ptr(), dst, other_len);
|
||||
self.set_len(self_len + other_len);
|
||||
}
|
||||
@@ -548,11 +528,11 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// use arrayvec::ArrayVec;
|
||||
///
|
||||
/// let mut v = ArrayVec::from([1, 2, 3]);
|
||||
/// let u: ArrayVec<[_; 3]> = v.drain(0..2).collect();
|
||||
/// let u: ArrayVec<_, 3> = v.drain(0..2).collect();
|
||||
/// assert_eq!(&v[..], &[3]);
|
||||
/// assert_eq!(&u[..], &[1, 2]);
|
||||
/// ```
|
||||
pub fn drain<R>(&mut self, range: R) -> Drain<A>
|
||||
pub fn drain<R>(&mut self, range: R) -> Drain<T, CAP>
|
||||
where R: RangeBounds<usize>
|
||||
{
|
||||
// Memory safety
|
||||
@@ -579,7 +559,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
self.drain_range(start, end)
|
||||
}
|
||||
|
||||
fn drain_range(&mut self, start: usize, end: usize) -> Drain<A>
|
||||
fn drain_range(&mut self, start: usize, end: usize) -> Drain<T, CAP>
|
||||
{
|
||||
let len = self.len();
|
||||
|
||||
@@ -588,7 +568,7 @@ impl<A: Array> ArrayVec<A> {
|
||||
|
||||
// Calling `set_len` creates a fresh and thus unique mutable references, making all
|
||||
// older aliases we created invalid. So we cannot call that function.
|
||||
self.len = Index::from(start);
|
||||
self.len = start;
|
||||
|
||||
unsafe {
|
||||
Drain {
|
||||
@@ -604,12 +584,12 @@ impl<A: Array> ArrayVec<A> {
|
||||
///
|
||||
/// Return an `Ok` value with the array if length equals capacity,
|
||||
/// return an `Err` with self otherwise.
|
||||
pub fn into_inner(self) -> Result<A, Self> {
|
||||
pub fn into_inner(self) -> Result<[T; CAP], Self> {
|
||||
if self.len() < self.capacity() {
|
||||
Err(self)
|
||||
} else {
|
||||
unsafe {
|
||||
let array = ptr::read(self.xs.ptr() as *const A);
|
||||
let array = ptr::read(self.as_ptr() as *const [T; CAP]);
|
||||
mem::forget(self);
|
||||
Ok(array)
|
||||
}
|
||||
@@ -624,46 +604,62 @@ impl<A: Array> ArrayVec<A> {
|
||||
}
|
||||
|
||||
/// Return a slice containing all elements of the vector.
|
||||
pub fn as_slice(&self) -> &[A::Item] {
|
||||
self
|
||||
pub fn as_slice(&self) -> &[T] {
|
||||
ArrayVecImpl::as_slice(self)
|
||||
}
|
||||
|
||||
/// Return a mutable slice containing all elements of the vector.
|
||||
pub fn as_mut_slice(&mut self) -> &mut [A::Item] {
|
||||
self
|
||||
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
||||
ArrayVecImpl::as_mut_slice(self)
|
||||
}
|
||||
|
||||
/// Return a raw pointer to the vector's buffer.
|
||||
pub fn as_ptr(&self) -> *const A::Item {
|
||||
self.xs.ptr()
|
||||
pub fn as_ptr(&self) -> *const T {
|
||||
ArrayVecImpl::as_ptr(self)
|
||||
}
|
||||
|
||||
/// Return a raw mutable pointer to the vector's buffer.
|
||||
pub fn as_mut_ptr(&mut self) -> *mut A::Item {
|
||||
self.xs.ptr_mut()
|
||||
pub fn as_mut_ptr(&mut self) -> *mut T {
|
||||
ArrayVecImpl::as_mut_ptr(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> Deref for ArrayVec<A> {
|
||||
type Target = [A::Item];
|
||||
#[inline]
|
||||
fn deref(&self) -> &[A::Item] {
|
||||
unsafe {
|
||||
slice::from_raw_parts(self.xs.ptr(), self.len())
|
||||
}
|
||||
impl<T, const CAP: usize> ArrayVecImpl for ArrayVec<T, CAP> {
|
||||
type Item = T;
|
||||
const CAPACITY: usize = CAP;
|
||||
|
||||
fn len(&self) -> usize { self.len() }
|
||||
|
||||
unsafe fn set_len(&mut self, length: usize) {
|
||||
debug_assert!(length <= CAP);
|
||||
self.len = length;
|
||||
}
|
||||
|
||||
fn as_ptr(&self) -> *const Self::Item {
|
||||
self.xs.as_ptr() as _
|
||||
}
|
||||
|
||||
fn as_mut_ptr(&mut self) -> *mut Self::Item {
|
||||
self.xs.as_mut_ptr() as _
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> DerefMut for ArrayVec<A> {
|
||||
impl<T, const CAP: usize> Deref for ArrayVec<T, CAP> {
|
||||
type Target = [T];
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut [A::Item] {
|
||||
let len = self.len();
|
||||
unsafe {
|
||||
slice::from_raw_parts_mut(self.xs.ptr_mut(), len)
|
||||
}
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.as_slice()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP> {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.as_mut_slice()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Create an `ArrayVec` from an array.
|
||||
///
|
||||
/// ```
|
||||
@@ -673,9 +669,16 @@ impl<A: Array> DerefMut for ArrayVec<A> {
|
||||
/// assert_eq!(array.len(), 3);
|
||||
/// assert_eq!(array.capacity(), 3);
|
||||
/// ```
|
||||
impl<A: Array> From<A> for ArrayVec<A> {
|
||||
fn from(array: A) -> Self {
|
||||
ArrayVec { xs: MaybeUninit::from(array), len: Index::from(A::CAPACITY) }
|
||||
impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP> {
|
||||
fn from(array: [T; CAP]) -> Self {
|
||||
let array = ManuallyDrop::new(array);
|
||||
let mut vec = <ArrayVec<T, CAP>>::new();
|
||||
unsafe {
|
||||
(&*array as *const [T; CAP] as *const [MaybeUninit<T>; CAP])
|
||||
.copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit<T>; CAP], 1);
|
||||
vec.set_len(CAP);
|
||||
}
|
||||
vec
|
||||
}
|
||||
}
|
||||
|
||||
@@ -687,18 +690,17 @@ impl<A: Array> From<A> for ArrayVec<A> {
|
||||
/// use arrayvec::ArrayVec;
|
||||
/// use std::convert::TryInto as _;
|
||||
///
|
||||
/// let array: ArrayVec<[_; 4]> = (&[1, 2, 3] as &[_]).try_into().unwrap();
|
||||
/// let array: ArrayVec<_, 4> = (&[1, 2, 3] as &[_]).try_into().unwrap();
|
||||
/// assert_eq!(array.len(), 3);
|
||||
/// assert_eq!(array.capacity(), 4);
|
||||
/// ```
|
||||
impl<A: Array> std::convert::TryFrom<&[A::Item]> for ArrayVec<A>
|
||||
where
|
||||
A::Item: Clone,
|
||||
impl<T, const CAP: usize> std::convert::TryFrom<&[T]> for ArrayVec<T, CAP>
|
||||
where T: Clone,
|
||||
{
|
||||
type Error = CapacityError;
|
||||
|
||||
fn try_from(slice: &[A::Item]) -> Result<Self, Self::Error> {
|
||||
if A::CAPACITY < slice.len() {
|
||||
fn try_from(slice: &[T]) -> Result<Self, Self::Error> {
|
||||
if Self::CAPACITY < slice.len() {
|
||||
Err(CapacityError::new(()))
|
||||
} else {
|
||||
let mut array = Self::new();
|
||||
@@ -720,9 +722,9 @@ impl<A: Array> std::convert::TryFrom<&[A::Item]> for ArrayVec<A>
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
impl<'a, A: Array> IntoIterator for &'a ArrayVec<A> {
|
||||
type Item = &'a A::Item;
|
||||
type IntoIter = slice::Iter<'a, A::Item>;
|
||||
impl<'a, T: 'a, const CAP: usize> IntoIterator for &'a ArrayVec<T, CAP> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = slice::Iter<'a, T>;
|
||||
fn into_iter(self) -> Self::IntoIter { self.iter() }
|
||||
}
|
||||
|
||||
@@ -737,9 +739,9 @@ impl<'a, A: Array> IntoIterator for &'a ArrayVec<A> {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
impl<'a, A: Array> IntoIterator for &'a mut ArrayVec<A> {
|
||||
type Item = &'a mut A::Item;
|
||||
type IntoIter = slice::IterMut<'a, A::Item>;
|
||||
impl<'a, T: 'a, const CAP: usize> IntoIterator for &'a mut ArrayVec<T, CAP> {
|
||||
type Item = &'a mut T;
|
||||
type IntoIter = slice::IterMut<'a, T>;
|
||||
fn into_iter(self) -> Self::IntoIter { self.iter_mut() }
|
||||
}
|
||||
|
||||
@@ -754,44 +756,44 @@ impl<'a, A: Array> IntoIterator for &'a mut ArrayVec<A> {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
impl<A: Array> IntoIterator for ArrayVec<A> {
|
||||
type Item = A::Item;
|
||||
type IntoIter = IntoIter<A>;
|
||||
fn into_iter(self) -> IntoIter<A> {
|
||||
IntoIter { index: Index::from(0), v: self, }
|
||||
impl<T, const CAP: usize> IntoIterator for ArrayVec<T, CAP> {
|
||||
type Item = T;
|
||||
type IntoIter = IntoIter<T, CAP>;
|
||||
fn into_iter(self) -> IntoIter<T, CAP> {
|
||||
IntoIter { index: 0, v: self, }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// By-value iterator for `ArrayVec`.
|
||||
pub struct IntoIter<A: Array> {
|
||||
index: A::Index,
|
||||
v: ArrayVec<A>,
|
||||
pub struct IntoIter<T, const CAP: usize> {
|
||||
index: usize,
|
||||
v: ArrayVec<T, CAP>,
|
||||
}
|
||||
|
||||
impl<A: Array> Iterator for IntoIter<A> {
|
||||
type Item = A::Item;
|
||||
impl<T, const CAP: usize> Iterator for IntoIter<T, CAP> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<A::Item> {
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.index == self.v.len {
|
||||
None
|
||||
} else {
|
||||
unsafe {
|
||||
let index = self.index.to_usize();
|
||||
self.index = Index::from(index + 1);
|
||||
let index = self.index;
|
||||
self.index = index + 1;
|
||||
Some(ptr::read(self.v.get_unchecked_ptr(index)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let len = self.v.len() - self.index.to_usize();
|
||||
let len = self.v.len() - self.index;
|
||||
(len, Some(len))
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> DoubleEndedIterator for IntoIter<A> {
|
||||
fn next_back(&mut self) -> Option<A::Item> {
|
||||
impl<T, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> {
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
if self.index == self.v.len {
|
||||
None
|
||||
} else {
|
||||
@@ -804,12 +806,12 @@ impl<A: Array> DoubleEndedIterator for IntoIter<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> ExactSizeIterator for IntoIter<A> { }
|
||||
impl<T, const CAP: usize> ExactSizeIterator for IntoIter<T, CAP> { }
|
||||
|
||||
impl<A: Array> Drop for IntoIter<A> {
|
||||
impl<T, const CAP: usize> Drop for IntoIter<T, CAP> {
|
||||
fn drop(&mut self) {
|
||||
// panic safety: Set length to 0 before dropping elements.
|
||||
let index = self.index.to_usize();
|
||||
let index = self.index;
|
||||
let len = self.v.len();
|
||||
unsafe {
|
||||
self.v.set_len(0);
|
||||
@@ -821,51 +823,45 @@ impl<A: Array> Drop for IntoIter<A> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> Clone for IntoIter<A>
|
||||
where
|
||||
A::Item: Clone,
|
||||
impl<T, const CAP: usize> Clone for IntoIter<T, CAP>
|
||||
where T: Clone,
|
||||
{
|
||||
fn clone(&self) -> IntoIter<A> {
|
||||
self.v[self.index.to_usize()..]
|
||||
fn clone(&self) -> IntoIter<T, CAP> {
|
||||
self.v[self.index..]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<ArrayVec<A>>()
|
||||
.collect::<ArrayVec<T, CAP>>()
|
||||
.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> fmt::Debug for IntoIter<A>
|
||||
impl<T, const CAP: usize> fmt::Debug for IntoIter<T, CAP>
|
||||
where
|
||||
A::Item: fmt::Debug,
|
||||
T: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_list()
|
||||
.entries(&self.v[self.index.to_usize()..])
|
||||
.entries(&self.v[self.index..])
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A draining iterator for `ArrayVec`.
|
||||
pub struct Drain<'a, A>
|
||||
where A: Array,
|
||||
A::Item: 'a,
|
||||
{
|
||||
pub struct Drain<'a, T: 'a, const CAP: usize> {
|
||||
/// Index of tail to preserve
|
||||
tail_start: usize,
|
||||
/// Length of tail
|
||||
tail_len: usize,
|
||||
/// Current remaining range to remove
|
||||
iter: slice::Iter<'a, A::Item>,
|
||||
vec: *mut ArrayVec<A>,
|
||||
iter: slice::Iter<'a, T>,
|
||||
vec: *mut ArrayVec<T, CAP>,
|
||||
}
|
||||
|
||||
unsafe impl<'a, A: Array + Sync> Sync for Drain<'a, A> {}
|
||||
unsafe impl<'a, A: Array + Send> Send for Drain<'a, A> {}
|
||||
unsafe impl<'a, T: Sync, const CAP: usize> Sync for Drain<'a, T, CAP> {}
|
||||
unsafe impl<'a, T: Send, const CAP: usize> Send for Drain<'a, T, CAP> {}
|
||||
|
||||
impl<'a, A: Array> Iterator for Drain<'a, A>
|
||||
where A::Item: 'a,
|
||||
{
|
||||
type Item = A::Item;
|
||||
impl<'a, T: 'a, const CAP: usize> Iterator for Drain<'a, T, CAP> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.iter.next().map(|elt|
|
||||
@@ -880,8 +876,7 @@ impl<'a, A: Array> Iterator for Drain<'a, A>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, A: Array> DoubleEndedIterator for Drain<'a, A>
|
||||
where A::Item: 'a,
|
||||
impl<'a, T: 'a, const CAP: usize> DoubleEndedIterator for Drain<'a, T, CAP>
|
||||
{
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
self.iter.next_back().map(|elt|
|
||||
@@ -892,11 +887,9 @@ impl<'a, A: Array> DoubleEndedIterator for Drain<'a, A>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, A: Array> ExactSizeIterator for Drain<'a, A> where A::Item: 'a {}
|
||||
impl<'a, T: 'a, const CAP: usize> ExactSizeIterator for Drain<'a, T, CAP> {}
|
||||
|
||||
impl<'a, A: Array> Drop for Drain<'a, A>
|
||||
where A::Item: 'a
|
||||
{
|
||||
impl<'a, T: 'a, const CAP: usize> Drop for Drain<'a, T, CAP> {
|
||||
fn drop(&mut self) {
|
||||
// len is currently 0 so panicking while dropping will not cause a double drop.
|
||||
|
||||
@@ -940,8 +933,8 @@ impl<T, Data, F> Drop for ScopeExitGuard<T, Data, F>
|
||||
///
|
||||
/// Does not extract more items than there is space for. No error
|
||||
/// occurs if there are more iterator elements.
|
||||
impl<A: Array> Extend<A::Item> for ArrayVec<A> {
|
||||
fn extend<T: IntoIterator<Item=A::Item>>(&mut self, iter: T) {
|
||||
impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP> {
|
||||
fn extend<I: IntoIterator<Item=T>>(&mut self, iter: I) {
|
||||
let take = self.capacity() - self.len();
|
||||
unsafe {
|
||||
let len = self.len();
|
||||
@@ -955,7 +948,7 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
|
||||
value: &mut self.len,
|
||||
data: len,
|
||||
f: move |&len, self_len| {
|
||||
**self_len = Index::from(len);
|
||||
**self_len = len;
|
||||
}
|
||||
};
|
||||
let mut iter = iter.into_iter();
|
||||
@@ -995,16 +988,16 @@ unsafe fn raw_ptr_write<T>(ptr: *mut T, value: T) {
|
||||
///
|
||||
/// Does not extract more items than there is space for. No error
|
||||
/// occurs if there are more iterator elements.
|
||||
impl<A: Array> iter::FromIterator<A::Item> for ArrayVec<A> {
|
||||
fn from_iter<T: IntoIterator<Item=A::Item>>(iter: T) -> Self {
|
||||
impl<T, const CAP: usize> iter::FromIterator<T> for ArrayVec<T, CAP> {
|
||||
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
|
||||
let mut array = ArrayVec::new();
|
||||
array.extend(iter);
|
||||
array
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> Clone for ArrayVec<A>
|
||||
where A::Item: Clone
|
||||
impl<T, const CAP: usize> Clone for ArrayVec<T, CAP>
|
||||
where T: Clone
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
self.iter().cloned().collect()
|
||||
@@ -1027,61 +1020,61 @@ impl<A: Array> Clone for ArrayVec<A>
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> Hash for ArrayVec<A>
|
||||
where A::Item: Hash
|
||||
impl<T, const CAP: usize> Hash for ArrayVec<T, CAP>
|
||||
where T: Hash
|
||||
{
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
Hash::hash(&**self, state)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> PartialEq for ArrayVec<A>
|
||||
where A::Item: PartialEq
|
||||
impl<T, const CAP: usize> PartialEq for ArrayVec<T, CAP>
|
||||
where T: PartialEq
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
**self == **other
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> PartialEq<[A::Item]> for ArrayVec<A>
|
||||
where A::Item: PartialEq
|
||||
impl<T, const CAP: usize> PartialEq<[T]> for ArrayVec<T, CAP>
|
||||
where T: PartialEq
|
||||
{
|
||||
fn eq(&self, other: &[A::Item]) -> bool {
|
||||
fn eq(&self, other: &[T]) -> bool {
|
||||
**self == *other
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> Eq for ArrayVec<A> where A::Item: Eq { }
|
||||
impl<T, const CAP: usize> Eq for ArrayVec<T, CAP> where T: Eq { }
|
||||
|
||||
impl<A: Array> Borrow<[A::Item]> for ArrayVec<A> {
|
||||
fn borrow(&self) -> &[A::Item] { self }
|
||||
impl<T, const CAP: usize> Borrow<[T]> for ArrayVec<T, CAP> {
|
||||
fn borrow(&self) -> &[T] { self }
|
||||
}
|
||||
|
||||
impl<A: Array> BorrowMut<[A::Item]> for ArrayVec<A> {
|
||||
fn borrow_mut(&mut self) -> &mut [A::Item] { self }
|
||||
impl<T, const CAP: usize> BorrowMut<[T]> for ArrayVec<T, CAP> {
|
||||
fn borrow_mut(&mut self) -> &mut [T] { self }
|
||||
}
|
||||
|
||||
impl<A: Array> AsRef<[A::Item]> for ArrayVec<A> {
|
||||
fn as_ref(&self) -> &[A::Item] { self }
|
||||
impl<T, const CAP: usize> AsRef<[T]> for ArrayVec<T, CAP> {
|
||||
fn as_ref(&self) -> &[T] { self }
|
||||
}
|
||||
|
||||
impl<A: Array> AsMut<[A::Item]> for ArrayVec<A> {
|
||||
fn as_mut(&mut self) -> &mut [A::Item] { self }
|
||||
impl<T, const CAP: usize> AsMut<[T]> for ArrayVec<T, CAP> {
|
||||
fn as_mut(&mut self) -> &mut [T] { self }
|
||||
}
|
||||
|
||||
impl<A: Array> fmt::Debug for ArrayVec<A> where A::Item: fmt::Debug {
|
||||
impl<T, const CAP: usize> fmt::Debug for ArrayVec<T, CAP> where T: fmt::Debug {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
|
||||
}
|
||||
|
||||
impl<A: Array> Default for ArrayVec<A> {
|
||||
impl<T, const CAP: usize> Default for ArrayVec<T, CAP> {
|
||||
/// Return an empty array
|
||||
fn default() -> ArrayVec<A> {
|
||||
fn default() -> ArrayVec<T, CAP> {
|
||||
ArrayVec::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> PartialOrd for ArrayVec<A> where A::Item: PartialOrd {
|
||||
fn partial_cmp(&self, other: &ArrayVec<A>) -> Option<cmp::Ordering> {
|
||||
impl<T, const CAP: usize> PartialOrd for ArrayVec<T, CAP> where T: PartialOrd {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
||||
(**self).partial_cmp(other)
|
||||
}
|
||||
|
||||
@@ -1102,8 +1095,8 @@ impl<A: Array> PartialOrd for ArrayVec<A> where A::Item: PartialOrd {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> Ord for ArrayVec<A> where A::Item: Ord {
|
||||
fn cmp(&self, other: &ArrayVec<A>) -> cmp::Ordering {
|
||||
impl<T, const CAP: usize> Ord for ArrayVec<T, CAP> where T: Ord {
|
||||
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
||||
(**self).cmp(other)
|
||||
}
|
||||
}
|
||||
@@ -1112,7 +1105,7 @@ impl<A: Array> Ord for ArrayVec<A> where A::Item: Ord {
|
||||
/// `Write` appends written data to the end of the vector.
|
||||
///
|
||||
/// Requires `features="std"`.
|
||||
impl<A: Array<Item=u8>> io::Write for ArrayVec<A> {
|
||||
impl<const CAP: usize> io::Write for ArrayVec<u8, CAP> {
|
||||
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
|
||||
let len = cmp::min(self.remaining_capacity(), data.len());
|
||||
let _result = self.try_extend_from_slice(&data[..len]);
|
||||
@@ -1124,7 +1117,7 @@ impl<A: Array<Item=u8>> io::Write for ArrayVec<A> {
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
/// Requires crate feature `"serde"`
|
||||
impl<T: Serialize, A: Array<Item=T>> Serialize for ArrayVec<A> {
|
||||
impl<T: Serialize, const CAP: usize> Serialize for ArrayVec<T, CAP> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
@@ -1134,30 +1127,30 @@ impl<T: Serialize, A: Array<Item=T>> Serialize for ArrayVec<A> {
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
/// Requires crate feature `"serde"`
|
||||
impl<'de, T: Deserialize<'de>, A: Array<Item=T>> Deserialize<'de> for ArrayVec<A> {
|
||||
impl<'de, T: Deserialize<'de>, const CAP: usize> Deserialize<'de> for ArrayVec<T, CAP> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where D: Deserializer<'de>
|
||||
{
|
||||
use serde::de::{Visitor, SeqAccess, Error};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
struct ArrayVecVisitor<'de, T: Deserialize<'de>, A: Array<Item=T>>(PhantomData<(&'de (), T, A)>);
|
||||
struct ArrayVecVisitor<'de, T: Deserialize<'de>, const CAP: usize>(PhantomData<(&'de (), [T; CAP])>);
|
||||
|
||||
impl<'de, T: Deserialize<'de>, A: Array<Item=T>> Visitor<'de> for ArrayVecVisitor<'de, T, A> {
|
||||
type Value = ArrayVec<A>;
|
||||
impl<'de, T: Deserialize<'de>, const CAP: usize> Visitor<'de> for ArrayVecVisitor<'de, T, CAP> {
|
||||
type Value = ArrayVec<T, CAP>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "an array with no more than {} items", A::CAPACITY)
|
||||
write!(formatter, "an array with no more than {} items", CAP)
|
||||
}
|
||||
|
||||
fn visit_seq<SA>(self, mut seq: SA) -> Result<Self::Value, SA::Error>
|
||||
where SA: SeqAccess<'de>,
|
||||
{
|
||||
let mut values = ArrayVec::<A>::new();
|
||||
let mut values = ArrayVec::<T, CAP>::new();
|
||||
|
||||
while let Some(value) = seq.next_element()? {
|
||||
if let Err(_) = values.try_push(value) {
|
||||
return Err(SA::Error::invalid_length(A::CAPACITY + 1, &self));
|
||||
return Err(SA::Error::invalid_length(CAP + 1, &self));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1165,6 +1158,6 @@ impl<'de, T: Deserialize<'de>, A: Array<Item=T>> Deserialize<'de> for ArrayVec<A
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_seq(ArrayVecVisitor::<T, A>(PhantomData))
|
||||
deserializer.deserialize_seq(ArrayVecVisitor::<T, CAP>(PhantomData))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
|
||||
use crate::CapacityError;
|
||||
|
||||
/// Implements basic arrayvec methods - based on a few required methods
|
||||
/// for length and element access.
|
||||
pub(crate) trait ArrayVecImpl {
|
||||
type Item;
|
||||
const CAPACITY: usize;
|
||||
|
||||
fn len(&self) -> usize;
|
||||
|
||||
unsafe fn set_len(&mut self, new_len: usize);
|
||||
|
||||
/// Return a slice containing all elements of the vector.
|
||||
fn as_slice(&self) -> &[Self::Item] {
|
||||
let len = self.len();
|
||||
unsafe {
|
||||
slice::from_raw_parts(self.as_ptr(), len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a mutable slice containing all elements of the vector.
|
||||
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
|
||||
let len = self.len();
|
||||
unsafe {
|
||||
std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a raw pointer to the vector's buffer.
|
||||
fn as_ptr(&self) -> *const Self::Item;
|
||||
|
||||
/// Return a raw mutable pointer to the vector's buffer.
|
||||
fn as_mut_ptr(&mut self) -> *mut Self::Item;
|
||||
|
||||
fn push(&mut self, element: Self::Item) {
|
||||
self.try_push(element).unwrap()
|
||||
}
|
||||
|
||||
fn try_push(&mut self, element: Self::Item) -> Result<(), CapacityError<Self::Item>> {
|
||||
if self.len() < Self::CAPACITY {
|
||||
unsafe {
|
||||
self.push_unchecked(element);
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err(CapacityError::new(element))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn push_unchecked(&mut self, element: Self::Item) {
|
||||
let len = self.len();
|
||||
debug_assert!(len < Self::CAPACITY);
|
||||
ptr::write(self.as_mut_ptr().add(len), element);
|
||||
self.set_len(len + 1);
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> Option<Self::Item> {
|
||||
if self.len() == 0 {
|
||||
return None;
|
||||
}
|
||||
unsafe {
|
||||
let new_len = self.len() - 1;
|
||||
self.set_len(new_len);
|
||||
Some(ptr::read(self.as_ptr().add(new_len)))
|
||||
}
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.truncate(0)
|
||||
}
|
||||
|
||||
fn truncate(&mut self, new_len: usize) {
|
||||
unsafe {
|
||||
let len = self.len();
|
||||
if new_len < len {
|
||||
self.set_len(new_len);
|
||||
let tail = slice::from_raw_parts_mut(self.as_mut_ptr().add(new_len), len - new_len);
|
||||
ptr::drop_in_place(tail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-13
@@ -10,9 +10,6 @@
|
||||
//! - `serde`
|
||||
//! - Optional
|
||||
//! - Enable serialization for ArrayVec and ArrayString using serde 1.x
|
||||
//! - `array-sizes-33-128`, `array-sizes-129-255`
|
||||
//! - Optional
|
||||
//! - Enable more array sizes (see [Array] for more information)
|
||||
//!
|
||||
//! - `unstable-const-fn`
|
||||
//! - Optional
|
||||
@@ -22,11 +19,11 @@
|
||||
//!
|
||||
//! ## Rust Version
|
||||
//!
|
||||
//! This version of arrayvec requires Rust 1.36 or later.
|
||||
//! This version of arrayvec requires Rust 1.51 or later.
|
||||
//!
|
||||
#![doc(html_root_url="https://docs.rs/arrayvec/0.5/")]
|
||||
#![cfg_attr(not(feature="std"), no_std)]
|
||||
#![cfg_attr(feature="unstable-const-fn", feature(const_fn))]
|
||||
#![cfg_attr(feature="unstable-const-fn", feature(const_fn, const_maybe_uninit_assume_init))]
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
extern crate serde;
|
||||
@@ -34,19 +31,12 @@ extern crate serde;
|
||||
#[cfg(not(feature="std"))]
|
||||
extern crate core as std;
|
||||
|
||||
mod maybe_uninit;
|
||||
use crate::maybe_uninit::MaybeUninit;
|
||||
|
||||
#[cfg(feature="serde")]
|
||||
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
||||
|
||||
mod array;
|
||||
mod arrayvec_impl;
|
||||
mod arrayvec;
|
||||
mod array_string;
|
||||
mod char;
|
||||
mod errors;
|
||||
|
||||
pub use crate::array::Array;
|
||||
pub use crate::array_string::ArrayString;
|
||||
pub use crate::errors::CapacityError;
|
||||
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
|
||||
|
||||
use crate::array::Array;
|
||||
use std::mem::MaybeUninit as StdMaybeUninit;
|
||||
|
||||
#[derive(Copy)]
|
||||
pub struct MaybeUninit<T> {
|
||||
inner: StdMaybeUninit<T>,
|
||||
}
|
||||
|
||||
impl<T> Clone for MaybeUninit<T>
|
||||
where T: Copy
|
||||
{
|
||||
fn clone(&self) -> Self { *self }
|
||||
}
|
||||
|
||||
impl<T> MaybeUninit<T> {
|
||||
/// Create a new MaybeUninit with uninitialized interior
|
||||
pub const unsafe fn uninitialized() -> Self {
|
||||
MaybeUninit { inner: StdMaybeUninit::uninit() }
|
||||
}
|
||||
|
||||
/// Create a new MaybeUninit from the value `v`.
|
||||
pub fn from(v: T) -> Self {
|
||||
MaybeUninit { inner: StdMaybeUninit::new(v) }
|
||||
}
|
||||
|
||||
// Raw pointer casts written so that we don't reference or access the
|
||||
// uninitialized interior value
|
||||
|
||||
/// Return a raw pointer to the start of the interior array
|
||||
pub fn ptr(&self) -> *const T::Item
|
||||
where T: Array
|
||||
{
|
||||
self.inner.as_ptr() as *const T::Item
|
||||
}
|
||||
|
||||
/// Return a mut raw pointer to the start of the interior array
|
||||
pub fn ptr_mut(&mut self) -> *mut T::Item
|
||||
where T: Array
|
||||
{
|
||||
self.inner.as_mut_ptr() as *mut T::Item
|
||||
}
|
||||
}
|
||||
+6
-6
@@ -9,7 +9,7 @@ mod array_vec {
|
||||
|
||||
#[test]
|
||||
fn test_ser_de_empty() {
|
||||
let vec = ArrayVec::<[u32; 0]>::new();
|
||||
let vec = ArrayVec::<u32, 0>::new();
|
||||
|
||||
assert_tokens(&vec, &[
|
||||
Token::Seq { len: Some(0) },
|
||||
@@ -20,7 +20,7 @@ mod array_vec {
|
||||
|
||||
#[test]
|
||||
fn test_ser_de() {
|
||||
let mut vec = ArrayVec::<[u32; 3]>::new();
|
||||
let mut vec = ArrayVec::<u32, 3>::new();
|
||||
vec.push(20);
|
||||
vec.push(55);
|
||||
vec.push(123);
|
||||
@@ -36,7 +36,7 @@ mod array_vec {
|
||||
|
||||
#[test]
|
||||
fn test_de_too_large() {
|
||||
assert_de_tokens_error::<ArrayVec<[u32; 2]>>(&[
|
||||
assert_de_tokens_error::<ArrayVec<u32, 2>>(&[
|
||||
Token::Seq { len: Some(3) },
|
||||
Token::U32(13),
|
||||
Token::U32(42),
|
||||
@@ -52,7 +52,7 @@ mod array_string {
|
||||
|
||||
#[test]
|
||||
fn test_ser_de_empty() {
|
||||
let string = ArrayString::<[u8; 0]>::new();
|
||||
let string = ArrayString::<0>::new();
|
||||
|
||||
assert_tokens(&string, &[
|
||||
Token::Str(""),
|
||||
@@ -62,7 +62,7 @@ mod array_string {
|
||||
|
||||
#[test]
|
||||
fn test_ser_de() {
|
||||
let string = ArrayString::<[u8; 9]>::from("1234 abcd")
|
||||
let string = ArrayString::<9>::from("1234 abcd")
|
||||
.expect("expected exact specified capacity to be enough");
|
||||
|
||||
assert_tokens(&string, &[
|
||||
@@ -72,7 +72,7 @@ mod array_string {
|
||||
|
||||
#[test]
|
||||
fn test_de_too_large() {
|
||||
assert_de_tokens_error::<ArrayString<[u8; 2]>>(&[
|
||||
assert_de_tokens_error::<ArrayString<2>>(&[
|
||||
Token::Str("afd")
|
||||
], "invalid length 3, expected a string no more than 2 bytes long");
|
||||
}
|
||||
|
||||
+54
-55
@@ -13,7 +13,7 @@ use std::collections::HashMap;
|
||||
fn test_simple() {
|
||||
use std::ops::Add;
|
||||
|
||||
let mut vec: ArrayVec<[Vec<i32>; 3]> = ArrayVec::new();
|
||||
let mut vec: ArrayVec<Vec<i32>, 3> = ArrayVec::new();
|
||||
|
||||
vec.push(vec![1, 2, 3, 4]);
|
||||
vec.push(vec![10]);
|
||||
@@ -29,7 +29,7 @@ fn test_simple() {
|
||||
|
||||
#[test]
|
||||
fn test_capacity_left() {
|
||||
let mut vec: ArrayVec<[usize; 4]> = ArrayVec::new();
|
||||
let mut vec: ArrayVec<usize, 4> = ArrayVec::new();
|
||||
assert_eq!(vec.remaining_capacity(), 4);
|
||||
vec.push(1);
|
||||
assert_eq!(vec.remaining_capacity(), 3);
|
||||
@@ -43,7 +43,7 @@ fn test_capacity_left() {
|
||||
|
||||
#[test]
|
||||
fn test_extend_from_slice() {
|
||||
let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
|
||||
let mut vec: ArrayVec<usize, 10> = ArrayVec::new();
|
||||
|
||||
vec.try_extend_from_slice(&[1, 2, 3]).unwrap();
|
||||
assert_eq!(vec.len(), 3);
|
||||
@@ -54,13 +54,13 @@ fn test_extend_from_slice() {
|
||||
|
||||
#[test]
|
||||
fn test_extend_from_slice_error() {
|
||||
let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
|
||||
let mut vec: ArrayVec<usize, 10> = ArrayVec::new();
|
||||
|
||||
vec.try_extend_from_slice(&[1, 2, 3]).unwrap();
|
||||
let res = vec.try_extend_from_slice(&[0; 8]);
|
||||
assert_matches!(res, Err(_));
|
||||
|
||||
let mut vec: ArrayVec<[usize; 0]> = ArrayVec::new();
|
||||
let mut vec: ArrayVec<usize, 0> = ArrayVec::new();
|
||||
let res = vec.try_extend_from_slice(&[0; 1]);
|
||||
assert_matches!(res, Err(_));
|
||||
}
|
||||
@@ -70,14 +70,14 @@ fn test_try_from_slice_error() {
|
||||
use arrayvec::ArrayVec;
|
||||
use std::convert::TryInto as _;
|
||||
|
||||
let res: Result<ArrayVec<[_; 2]>, _> = (&[1, 2, 3] as &[_]).try_into();
|
||||
let res: Result<ArrayVec<_, 2>, _> = (&[1, 2, 3] as &[_]).try_into();
|
||||
assert_matches!(res, Err(_));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_u16_index() {
|
||||
const N: usize = 4096;
|
||||
let mut vec: ArrayVec<[_; N]> = ArrayVec::new();
|
||||
let mut vec: ArrayVec<_, N> = ArrayVec::new();
|
||||
for _ in 0..N {
|
||||
assert!(vec.try_push(1u8).is_ok());
|
||||
}
|
||||
@@ -113,7 +113,7 @@ fn test_drop() {
|
||||
}
|
||||
|
||||
{
|
||||
let mut array = ArrayVec::<[Bump; 128]>::new();
|
||||
let mut array = ArrayVec::<Bump, 128>::new();
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
}
|
||||
@@ -123,7 +123,7 @@ fn test_drop() {
|
||||
flag.set(0);
|
||||
|
||||
{
|
||||
let mut array = ArrayVec::<[_; 3]>::new();
|
||||
let mut array = ArrayVec::<_, 3>::new();
|
||||
array.push(vec![Bump(flag)]);
|
||||
array.push(vec![Bump(flag), Bump(flag)]);
|
||||
array.push(vec![]);
|
||||
@@ -142,7 +142,7 @@ fn test_drop() {
|
||||
// test into_inner
|
||||
flag.set(0);
|
||||
{
|
||||
let mut array = ArrayVec::<[_; 3]>::new();
|
||||
let mut array = ArrayVec::<_, 3>::new();
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
@@ -156,7 +156,7 @@ fn test_drop() {
|
||||
// test cloning into_iter
|
||||
flag.set(0);
|
||||
{
|
||||
let mut array = ArrayVec::<[_; 3]>::new();
|
||||
let mut array = ArrayVec::<_, 3>::new();
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
@@ -210,7 +210,7 @@ fn test_drop_panics() {
|
||||
|
||||
flag.set(0);
|
||||
{
|
||||
let mut array = ArrayVec::<[Bump; 128]>::new();
|
||||
let mut array = ArrayVec::<Bump, 128>::new();
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
@@ -226,7 +226,7 @@ fn test_drop_panics() {
|
||||
|
||||
flag.set(0);
|
||||
{
|
||||
let mut array = ArrayVec::<[Bump; 16]>::new();
|
||||
let mut array = ArrayVec::<Bump, 16>::new();
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
array.push(Bump(flag));
|
||||
@@ -251,14 +251,14 @@ fn test_drop_panics() {
|
||||
fn test_extend() {
|
||||
let mut range = 0..10;
|
||||
|
||||
let mut array: ArrayVec<[_; 5]> = range.by_ref().collect();
|
||||
let mut array: ArrayVec<_, 5> = range.by_ref().collect();
|
||||
assert_eq!(&array[..], &[0, 1, 2, 3, 4]);
|
||||
assert_eq!(range.next(), Some(5));
|
||||
|
||||
array.extend(range.by_ref());
|
||||
assert_eq!(range.next(), Some(6));
|
||||
|
||||
let mut array: ArrayVec<[_; 10]> = (0..3).collect();
|
||||
let mut array: ArrayVec<_, 10> = (0..3).collect();
|
||||
assert_eq!(&array[..], &[0, 1, 2]);
|
||||
array.extend(3..5);
|
||||
assert_eq!(&array[..], &[0, 1, 2, 3, 4]);
|
||||
@@ -266,33 +266,32 @@ fn test_extend() {
|
||||
|
||||
#[test]
|
||||
fn test_is_send_sync() {
|
||||
let data = ArrayVec::<[Vec<i32>; 5]>::new();
|
||||
let data = ArrayVec::<Vec<i32>, 5>::new();
|
||||
&data as &dyn Send;
|
||||
&data as &dyn Sync;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compact_size() {
|
||||
// Future rust will kill these drop flags!
|
||||
// 4 elements size + 1 len + 1 enum tag + [1 drop flag]
|
||||
type ByteArray = ArrayVec<[u8; 4]>;
|
||||
// 4 bytes + padding + length
|
||||
type ByteArray = ArrayVec<u8, 4>;
|
||||
println!("{}", mem::size_of::<ByteArray>());
|
||||
assert!(mem::size_of::<ByteArray>() <= 8);
|
||||
assert!(mem::size_of::<ByteArray>() <= 2 * mem::size_of::<usize>());
|
||||
|
||||
// 1 enum tag + 1 drop flag
|
||||
type EmptyArray = ArrayVec<[u8; 0]>;
|
||||
// just length
|
||||
type EmptyArray = ArrayVec<u8, 0>;
|
||||
println!("{}", mem::size_of::<EmptyArray>());
|
||||
assert!(mem::size_of::<EmptyArray>() <= 2);
|
||||
assert!(mem::size_of::<EmptyArray>() <= mem::size_of::<usize>());
|
||||
|
||||
// 12 element size + 1 enum tag + 3 padding + 1 len + 1 drop flag + 2 padding
|
||||
type QuadArray = ArrayVec<[u32; 3]>;
|
||||
// 3 elements + padding + length
|
||||
type QuadArray = ArrayVec<u32, 3>;
|
||||
println!("{}", mem::size_of::<QuadArray>());
|
||||
assert!(mem::size_of::<QuadArray>() <= 24);
|
||||
assert!(mem::size_of::<QuadArray>() <= 4 * 4 + mem::size_of::<usize>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_still_works_with_option_arrayvec() {
|
||||
type RefArray = ArrayVec<[&'static i32; 2]>;
|
||||
type RefArray = ArrayVec<&'static i32, 2>;
|
||||
let array = Some(RefArray::new());
|
||||
assert!(array.is_some());
|
||||
println!("{:?}", array);
|
||||
@@ -308,7 +307,7 @@ fn test_drain() {
|
||||
v.extend(0..);
|
||||
v.drain(1..4);
|
||||
assert_eq!(&v[..], &[0, 4, 5, 6, 7]);
|
||||
let u: ArrayVec<[_; 3]> = v.drain(1..4).rev().collect();
|
||||
let u: ArrayVec<_, 3> = v.drain(1..4).rev().collect();
|
||||
assert_eq!(&u[..], &[6, 5, 4]);
|
||||
assert_eq!(&v[..], &[0, 7]);
|
||||
v.drain(..);
|
||||
@@ -324,7 +323,7 @@ fn test_drain_range_inclusive() {
|
||||
v.extend(0..);
|
||||
v.drain(1..=4);
|
||||
assert_eq!(&v[..], &[0, 5, 6, 7]);
|
||||
let u: ArrayVec<[_; 3]> = v.drain(1..=2).rev().collect();
|
||||
let u: ArrayVec<_, 3> = v.drain(1..=2).rev().collect();
|
||||
assert_eq!(&u[..], &[6, 5]);
|
||||
assert_eq!(&v[..], &[0, 7]);
|
||||
v.drain(..);
|
||||
@@ -374,7 +373,7 @@ fn test_drop_panic() {
|
||||
}
|
||||
}
|
||||
|
||||
let mut array = ArrayVec::<[DropPanic; 1]>::new();
|
||||
let mut array = ArrayVec::<DropPanic, 1>::new();
|
||||
array.push(DropPanic);
|
||||
}
|
||||
|
||||
@@ -389,7 +388,7 @@ fn test_drop_panic_into_iter() {
|
||||
}
|
||||
}
|
||||
|
||||
let mut array = ArrayVec::<[DropPanic; 1]>::new();
|
||||
let mut array = ArrayVec::<DropPanic, 1>::new();
|
||||
array.push(DropPanic);
|
||||
array.into_iter();
|
||||
}
|
||||
@@ -399,7 +398,7 @@ fn test_insert() {
|
||||
let mut v = ArrayVec::from([]);
|
||||
assert_matches!(v.try_push(1), Err(_));
|
||||
|
||||
let mut v = ArrayVec::<[_; 3]>::new();
|
||||
let mut v = ArrayVec::<_, 3>::new();
|
||||
v.insert(0, 0);
|
||||
v.insert(1, 1);
|
||||
//let ret1 = v.try_insert(3, 3);
|
||||
@@ -428,7 +427,7 @@ fn test_into_inner_1() {
|
||||
|
||||
#[test]
|
||||
fn test_into_inner_2() {
|
||||
let mut v = ArrayVec::<[String; 4]>::new();
|
||||
let mut v = ArrayVec::<String, 4>::new();
|
||||
v.push("a".into());
|
||||
v.push("b".into());
|
||||
v.push("c".into());
|
||||
@@ -438,7 +437,7 @@ fn test_into_inner_2() {
|
||||
|
||||
#[test]
|
||||
fn test_into_inner_3_() {
|
||||
let mut v = ArrayVec::<[i32; 4]>::new();
|
||||
let mut v = ArrayVec::<i32, 4>::new();
|
||||
v.extend(1..);
|
||||
assert_eq!(v.into_inner().unwrap(), [1, 2, 3, 4]);
|
||||
}
|
||||
@@ -447,7 +446,7 @@ fn test_into_inner_3_() {
|
||||
#[test]
|
||||
fn test_write() {
|
||||
use std::io::Write;
|
||||
let mut v = ArrayVec::<[_; 8]>::new();
|
||||
let mut v = ArrayVec::<_, 8>::new();
|
||||
write!(&mut v, "\x01\x02\x03").unwrap();
|
||||
assert_eq!(&v[..], &[1, 2, 3]);
|
||||
let r = v.write(&[9; 16]).unwrap();
|
||||
@@ -457,16 +456,16 @@ fn test_write() {
|
||||
|
||||
#[test]
|
||||
fn array_clone_from() {
|
||||
let mut v = ArrayVec::<[_; 4]>::new();
|
||||
let mut v = ArrayVec::<_, 4>::new();
|
||||
v.push(vec![1, 2]);
|
||||
v.push(vec![3, 4, 5]);
|
||||
v.push(vec![6]);
|
||||
let reference = v.to_vec();
|
||||
let mut u = ArrayVec::<[_; 4]>::new();
|
||||
let mut u = ArrayVec::<_, 4>::new();
|
||||
u.clone_from(&v);
|
||||
assert_eq!(&u, &reference[..]);
|
||||
|
||||
let mut t = ArrayVec::<[_; 4]>::new();
|
||||
let mut t = ArrayVec::<_, 4>::new();
|
||||
t.push(vec![97]);
|
||||
t.push(vec![]);
|
||||
t.push(vec![5, 6, 2]);
|
||||
@@ -484,7 +483,7 @@ fn test_string() {
|
||||
use std::error::Error;
|
||||
|
||||
let text = "hello world";
|
||||
let mut s = ArrayString::<[_; 16]>::new();
|
||||
let mut s = ArrayString::<16>::new();
|
||||
s.try_push_str(text).unwrap();
|
||||
assert_eq!(&s, text);
|
||||
assert_eq!(text, &s);
|
||||
@@ -494,7 +493,7 @@ fn test_string() {
|
||||
map.insert(s, 1);
|
||||
assert_eq!(map[text], 1);
|
||||
|
||||
let mut t = ArrayString::<[_; 2]>::new();
|
||||
let mut t = ArrayString::<2>::new();
|
||||
assert!(t.try_push_str(text).is_err());
|
||||
assert_eq!(&t, "");
|
||||
|
||||
@@ -505,7 +504,7 @@ fn test_string() {
|
||||
|
||||
// Test Error trait / try
|
||||
let t = || -> Result<(), Box<dyn Error>> {
|
||||
let mut t = ArrayString::<[_; 2]>::new();
|
||||
let mut t = ArrayString::<2>::new();
|
||||
t.try_push_str(text)?;
|
||||
Ok(())
|
||||
}();
|
||||
@@ -516,7 +515,7 @@ fn test_string() {
|
||||
fn test_string_from() {
|
||||
let text = "hello world";
|
||||
// Test `from` constructor
|
||||
let u = ArrayString::<[_; 11]>::from(text).unwrap();
|
||||
let u = ArrayString::<11>::from(text).unwrap();
|
||||
assert_eq!(&u, text);
|
||||
assert_eq!(u.len(), text.len());
|
||||
}
|
||||
@@ -524,7 +523,7 @@ fn test_string_from() {
|
||||
#[test]
|
||||
fn test_string_parse_from_str() {
|
||||
let text = "hello world";
|
||||
let u: ArrayString<[_; 11]> = text.parse().unwrap();
|
||||
let u: ArrayString<11> = text.parse().unwrap();
|
||||
assert_eq!(&u, text);
|
||||
assert_eq!(u.len(), text.len());
|
||||
}
|
||||
@@ -540,9 +539,9 @@ fn test_string_from_bytes() {
|
||||
#[test]
|
||||
fn test_string_clone() {
|
||||
let text = "hi";
|
||||
let mut s = ArrayString::<[_; 4]>::new();
|
||||
let mut s = ArrayString::<4>::new();
|
||||
s.push_str("abcd");
|
||||
let t = ArrayString::<[_; 4]>::from(text).unwrap();
|
||||
let t = ArrayString::<4>::from(text).unwrap();
|
||||
s.clone_from(&t);
|
||||
assert_eq!(&t, &s);
|
||||
}
|
||||
@@ -550,7 +549,7 @@ fn test_string_clone() {
|
||||
#[test]
|
||||
fn test_string_push() {
|
||||
let text = "abcαβγ";
|
||||
let mut s = ArrayString::<[_; 8]>::new();
|
||||
let mut s = ArrayString::<8>::new();
|
||||
for c in text.chars() {
|
||||
if let Err(_) = s.try_push(c) {
|
||||
break;
|
||||
@@ -565,7 +564,7 @@ fn test_string_push() {
|
||||
|
||||
#[test]
|
||||
fn test_insert_at_length() {
|
||||
let mut v = ArrayVec::<[_; 8]>::new();
|
||||
let mut v = ArrayVec::<_, 8>::new();
|
||||
let result1 = v.try_insert(0, "a");
|
||||
let result2 = v.try_insert(1, "b");
|
||||
assert!(result1.is_ok() && result2.is_ok());
|
||||
@@ -575,7 +574,7 @@ fn test_insert_at_length() {
|
||||
#[should_panic]
|
||||
#[test]
|
||||
fn test_insert_out_of_bounds() {
|
||||
let mut v = ArrayVec::<[_; 8]>::new();
|
||||
let mut v = ArrayVec::<_, 8>::new();
|
||||
let _ = v.try_insert(1, "test");
|
||||
}
|
||||
|
||||
@@ -608,7 +607,7 @@ fn test_drop_in_insert() {
|
||||
flag.set(0);
|
||||
|
||||
{
|
||||
let mut array = ArrayVec::<[_; 2]>::new();
|
||||
let mut array = ArrayVec::<_, 2>::new();
|
||||
array.push(Bump(flag));
|
||||
array.insert(0, Bump(flag));
|
||||
assert_eq!(flag.get(), 0);
|
||||
@@ -623,7 +622,7 @@ fn test_drop_in_insert() {
|
||||
|
||||
#[test]
|
||||
fn test_pop_at() {
|
||||
let mut v = ArrayVec::<[String; 4]>::new();
|
||||
let mut v = ArrayVec::<String, 4>::new();
|
||||
let s = String::from;
|
||||
v.push(s("a"));
|
||||
v.push(s("b"));
|
||||
@@ -646,9 +645,9 @@ fn test_sizes() {
|
||||
#[test]
|
||||
fn test_default() {
|
||||
use std::net;
|
||||
let s: ArrayString<[u8; 4]> = Default::default();
|
||||
let s: ArrayString<4> = Default::default();
|
||||
// Something without `Default` implementation.
|
||||
let v: ArrayVec<[net::TcpStream; 4]> = Default::default();
|
||||
let v: ArrayVec<net::TcpStream, 4> = Default::default();
|
||||
assert_eq!(s.len(), 0);
|
||||
assert_eq!(v.len(), 0);
|
||||
}
|
||||
@@ -673,14 +672,14 @@ fn test_extend_zst() {
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
struct Z; // Zero sized type
|
||||
|
||||
let mut array: ArrayVec<[_; 5]> = range.by_ref().map(|_| Z).collect();
|
||||
let mut array: ArrayVec<_, 5> = range.by_ref().map(|_| Z).collect();
|
||||
assert_eq!(&array[..], &[Z; 5]);
|
||||
assert_eq!(range.next(), Some(5));
|
||||
|
||||
array.extend(range.by_ref().map(|_| Z));
|
||||
assert_eq!(range.next(), Some(6));
|
||||
|
||||
let mut array: ArrayVec<[_; 10]> = (0..3).map(|_| Z).collect();
|
||||
let mut array: ArrayVec<_, 10> = (0..3).map(|_| Z).collect();
|
||||
assert_eq!(&array[..], &[Z; 3]);
|
||||
array.extend((3..5).map(|_| Z));
|
||||
assert_eq!(&array[..], &[Z; 5]);
|
||||
@@ -690,6 +689,6 @@ fn test_extend_zst() {
|
||||
#[test]
|
||||
fn test_try_from_argument() {
|
||||
use core::convert::TryFrom;
|
||||
let v = ArrayString::<[u8; 16]>::try_from(format_args!("Hello {}", 123)).unwrap();
|
||||
let v = ArrayString::<16>::try_from(format_args!("Hello {}", 123)).unwrap();
|
||||
assert_eq!(&v, "Hello 123");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user