diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock index f265cd8ce..0209f1dbb 100644 --- a/contracts/Cargo.lock +++ b/contracts/Cargo.lock @@ -7,7 +7,7 @@ name = "access-controller" version = "1.0.1" dependencies = [ "anchor-lang", - "arrayvec 1.0.0", + "arrayvec 1.0.1", "bytemuck", "static_assertions", ] @@ -379,7 +379,7 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" [[package]] name = "arrayvec" -version = "1.0.0" +version = "1.0.1" [[package]] name = "assert_matches" @@ -1374,7 +1374,7 @@ dependencies = [ "anchor-lang", "anchor-spl", "arrayref", - "arrayvec 1.0.0", + "arrayvec 1.0.1", "bytemuck", "solana-program", "static_assertions", diff --git a/contracts/crates/arrayvec/Cargo.toml b/contracts/crates/arrayvec/Cargo.toml index 4e9ad51cc..5570a5636 100644 --- a/contracts/crates/arrayvec/Cargo.toml +++ b/contracts/crates/arrayvec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "arrayvec" -version = "1.0.0" +version = "1.0.1" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/contracts/crates/arrayvec/src/lib.rs b/contracts/crates/arrayvec/src/lib.rs index dfc8a12e3..9312c2338 100644 --- a/contracts/crates/arrayvec/src/lib.rs +++ b/contracts/crates/arrayvec/src/lib.rs @@ -90,6 +90,17 @@ macro_rules! arrayvec { self.xs[offset..offset + len].copy_from_slice(&data); self.len += len as $capacity_ty; } + + /// Removes the last element from the array and returns it. + /// Returns `None` if the array is empty. + pub fn pop(&mut self) -> Option<$ty> { + if self.len > 0 { + self.len -= 1; + Some(self.xs[self.len as usize]) + } else { + None + } + } } impl std::ops::Deref for $name { @@ -126,6 +137,38 @@ mod tests { } arrayvec!(ArrayVec, u8, u32); + #[test] + fn push_pop() { + let mut vec = ArrayVec::new(); + assert!(vec.is_empty()); + assert_eq!(vec.pop(), None); + + vec.push(10); + assert_eq!(vec.len(), 1); + assert_eq!(vec.as_slice(), &[10]); + + vec.push(20); + vec.push(30); + assert_eq!(vec.len(), 3); + assert_eq!(vec.as_slice(), &[10, 20, 30]); + + // Popping elements + assert_eq!(vec.pop(), Some(30)); + assert_eq!(vec.len(), 2); + assert_eq!(vec.as_slice(), &[10, 20]); + + assert_eq!(vec.pop(), Some(20)); + assert_eq!(vec.len(), 1); + assert_eq!(vec.as_slice(), &[10]); + + assert_eq!(vec.pop(), Some(10)); + assert_eq!(vec.len(), 0); + assert!(vec.is_empty()); + + // Popping from empty vec + assert_eq!(vec.pop(), None); + } + #[test] fn remove() { let mut vec = ArrayVec::new();