Skip to content

Commit

Permalink
Make ArrayVecCopy functions non-const
Browse files Browse the repository at this point in the history
They don't work on Rust 1.51

```
error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
  --> src/arrayvec_copy.rs:59:6
   |
59 | impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
   |      ^
   |
   = note: see issue #57563 <rust-lang/rust#57563> for more information
   = help: add `#![feature(const_fn)]` to the crate attributes to enable
```
  • Loading branch information
tbu- committed Oct 17, 2024
1 parent 4f348df commit abf40d5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions generate_arrayvec_copy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sed \
-e "s/struct \\([A-Za-z_]*\\)<\\('[A-Za-z_]*, \\)\\?T,/struct \\1<\\2T: Copy,/g" \
-e "s/fn \\([A-Za-z_]*\\)<\\('[A-Za-z_]*, \\)\\?T:/fn \\1<\\2T: Copy +/g" \
-e "s/fn \\([A-Za-z_]*\\)<\\('[A-Za-z_]*, \\)\\?T,/fn \\1<\\2T: Copy,/g" \
-e "s/const fn/fn/" \
src/arrayvec.rs \
> src/arrayvec_copy_generated.rs

Expand Down
25 changes: 23 additions & 2 deletions src/arrayvec_copy.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/src/arrayvec_copy_generated.rs b/src/arrayvec_copy.rs
index b12aa9e..5597af9 100644
index b12aa9e..8e4c3a9 100644
--- a/src/arrayvec_copy_generated.rs
+++ b/src/arrayvec_copy.rs
@@ -27,6 +27,9 @@ use crate::utils::MakeMaybeUninit;
Expand Down Expand Up @@ -27,7 +27,28 @@ index b12aa9e..5597af9 100644
macro_rules! panic_oob {
($method_name:expr, $index:expr, $len:expr) => {
panic!(concat!("ArrayVecCopy::", $method_name, ": index {} is out of bounds in vector of length {}"),
@@ -964,21 +959,6 @@ impl<T: Copy, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> {
@@ -87,20 +82,6 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
}
}

- /// Create a new empty `ArrayVecCopy` (const fn).
- ///
- /// The maximum capacity is given by the generic parameter `CAP`.
- ///
- /// ```
- /// use arrayvec::ArrayVecCopy;
- ///
- /// static ARRAY: ArrayVecCopy<u8, 1024> = ArrayVecCopy::new_const();
- /// ```
- pub const fn new_const() -> ArrayVecCopy<T, CAP> {
- assert_capacity_limit_const!(CAP);
- ArrayVecCopy { xs: MakeMaybeUninit::ARRAY, len: 0 }
- }
-
/// Return the number of elements in the `ArrayVecCopy`.
///
/// ```
@@ -964,21 +945,6 @@ impl<T: Copy, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> {

impl<T: Copy, const CAP: usize> ExactSizeIterator for IntoIter<T, CAP> { }

Expand Down
24 changes: 5 additions & 19 deletions src/arrayvec_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,6 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
}
}

/// Create a new empty `ArrayVecCopy` (const fn).
///
/// The maximum capacity is given by the generic parameter `CAP`.
///
/// ```
/// use arrayvec::ArrayVecCopy;
///
/// static ARRAY: ArrayVecCopy<u8, 1024> = ArrayVecCopy::new_const();
/// ```
pub const fn new_const() -> ArrayVecCopy<T, CAP> {
assert_capacity_limit_const!(CAP);
ArrayVecCopy { xs: MakeMaybeUninit::ARRAY, len: 0 }
}

/// Return the number of elements in the `ArrayVecCopy`.
///
/// ```
Expand All @@ -106,7 +92,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<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 `ArrayVecCopy` is empty.
///
Expand All @@ -118,7 +104,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<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 `ArrayVecCopy`.
///
Expand All @@ -129,7 +115,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<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 `ArrayVecCopy` is completely filled to its capacity, false otherwise.
///
Expand All @@ -141,7 +127,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<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 `ArrayVecCopy`.
///
Expand All @@ -152,7 +138,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<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()
}

Expand Down

0 comments on commit abf40d5

Please sign in to comment.