Skip to content

added ArrayVecCopy #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
Recent Changes (arrayvec)
=========================

## 0.7.2

- Add `.as_mut_str()` to `ArrayString` by @clarfonthey
- Add `remaining_capacity` to `ArrayString` by @bhgomes
- Add `zero_filled` constructor by @c410-f3r
- Optimize `retain` by @TennyZhuang and @niklasf
- Make the following methods `const` by @bhgomes:
- len
- is_empty
- capacity
- is_full
- remaining_capacity
- CapacityError::new

## 0.7.1

- Add new ArrayVec methods `.take()` and `.into_inner_unchecked()` by @conradludgate
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arrayvec"
version = "0.7.2"
version = "0.7.1"
authors = ["bluss"]
license = "MIT OR Apache-2.0"
edition = "2018"
@@ -37,6 +37,7 @@ harness = false
[features]
default = ["std"]
std = []
copy = []

[profile.bench]
debug = true
43 changes: 4 additions & 39 deletions src/array_string.rs
Original file line number Diff line number Diff line change
@@ -82,11 +82,11 @@ impl<const CAP: usize> ArrayString<CAP>

/// Return the length of the string.
#[inline]
pub const fn len(&self) -> usize { self.len as usize }
pub fn len(&self) -> usize { self.len as usize }

/// Returns whether the string is empty.
#[inline]
pub const fn is_empty(&self) -> bool { self.len() == 0 }
pub fn is_empty(&self) -> bool { self.len() == 0 }

/// Create a new `ArrayString` from a `str`.
///
@@ -129,28 +129,6 @@ impl<const CAP: usize> ArrayString<CAP>
Ok(vec)
}

/// Create a new `ArrayString` value fully filled with ASCII NULL characters (`\0`). Useful
/// to be used as a buffer to collect external data or as a buffer for intermediate processing.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let string = ArrayString::<16>::zero_filled();
/// assert_eq!(string.len(), 16);
/// ```
#[inline]
pub fn zero_filled() -> Self {
assert_capacity_limit!(CAP);
// SAFETY: `assert_capacity_limit` asserts that `len` won't overflow and
// `zeroed` fully fills the array with nulls.
unsafe {
ArrayString {
xs: MaybeUninit::zeroed().assume_init(),
len: CAP as _
}
}
}

/// Return the capacity of the `ArrayString`.
///
/// ```
@@ -160,7 +138,7 @@ impl<const CAP: usize> ArrayString<CAP>
/// assert_eq!(string.capacity(), 3);
/// ```
#[inline(always)]
pub const fn capacity(&self) -> usize { CAP }
pub fn capacity(&self) -> usize { CAP }

/// Return if the `ArrayString` is completely filled.
///
@@ -172,20 +150,7 @@ impl<const CAP: usize> ArrayString<CAP>
/// string.push_str("A");
/// assert!(string.is_full());
/// ```
pub const fn is_full(&self) -> bool { self.len() == self.capacity() }

/// Returns the capacity left in the `ArrayString`.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::<3>::from("abc").unwrap();
/// string.pop();
/// assert_eq!(string.remaining_capacity(), 1);
/// ```
pub const fn remaining_capacity(&self) -> usize {
self.capacity() - self.len()
}
pub fn is_full(&self) -> bool { self.len() == self.capacity() }

/// Adds the given char to the end of the string.
///
33 changes: 8 additions & 25 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(array.len(), 2);
/// ```
#[inline(always)]
pub const fn len(&self) -> usize { self.len as usize }
pub fn len(&self) -> usize { self.len as usize }

/// Returns whether the `ArrayVec` is empty.
///
@@ -120,7 +120,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(array.is_empty(), true);
/// ```
#[inline]
pub const fn is_empty(&self) -> bool { self.len() == 0 }
pub fn is_empty(&self) -> bool { self.len() == 0 }

/// Return the capacity of the `ArrayVec`.
///
@@ -131,7 +131,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(array.capacity(), 3);
/// ```
#[inline(always)]
pub const fn capacity(&self) -> usize { CAP }
pub fn capacity(&self) -> usize { CAP }

/// Return true if the `ArrayVec` is completely filled to its capacity, false otherwise.
///
@@ -143,7 +143,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// array.push(1);
/// assert!(array.is_full());
/// ```
pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
pub fn is_full(&self) -> bool { self.len() == self.capacity() }

/// Returns the capacity left in the `ArrayVec`.
///
@@ -154,7 +154,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// array.pop();
/// assert_eq!(array.remaining_capacity(), 1);
/// ```
pub const fn remaining_capacity(&self) -> usize {
pub fn remaining_capacity(&self) -> usize {
self.capacity() - self.len()
}

@@ -493,38 +493,21 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {

let mut g = BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len };

#[inline(always)]
fn process_one<F: FnMut(&mut T) -> bool, T, const CAP: usize, const DELETED: bool>(
f: &mut F,
g: &mut BackshiftOnDrop<'_, T, CAP>
) -> bool {
while g.processed_len < original_len {
let cur = unsafe { g.v.as_mut_ptr().add(g.processed_len) };
if !f(unsafe { &mut *cur }) {
g.processed_len += 1;
g.deleted_cnt += 1;
unsafe { ptr::drop_in_place(cur) };
return false;
continue;
}
if DELETED {
if g.deleted_cnt > 0 {
unsafe {
let hole_slot = g.v.as_mut_ptr().add(g.processed_len - g.deleted_cnt);
ptr::copy_nonoverlapping(cur, hole_slot, 1);
}
}
g.processed_len += 1;
true
}

// Stage 1: Nothing was deleted.
while g.processed_len != original_len {
if !process_one::<F, T, CAP, false>(&mut f, &mut g) {
break;
}
}

// Stage 2: Some elements were deleted.
while g.processed_len != original_len {
process_one::<F, T, CAP, true>(&mut f, &mut g);
}

drop(g);
Loading