diff --git a/Cargo.toml b/Cargo.toml index bb375dc..bb47a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "static_vector" version = "0.1.0" edition = "2024" -description = "Vector with static allocation" +description = "A no-std, stack-allocated vector with fixed capacity and dynamic length" repository = "https://github.com/andreiavrammsd/static_vector.rs" readme = "README.md" license = "MIT" diff --git a/README.md b/README.md index 759c854..ac6acb0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ The goal is to allocate only when needed. When first constructed, the vector wil ## Features - No heap allocation (`#![no_std]` compatible) -- Constant-time indexed access - Supports iteration, mutable access, clearing, resizing - Compile-time enforced capacity diff --git a/src/lib.rs b/src/lib.rs index 2ec4c7d..c320966 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ //! //! # Features //! - No heap allocation (`#![no_std]` compatible) -//! - Constant-time indexed access //! - Supports iteration, mutable access, clearing, resizing //! - Compile-time enforced capacity //! @@ -35,7 +34,13 @@ use core::{array, mem::MaybeUninit}; /// Error type returned by [`StaticVector`]. #[derive(Debug)] -pub struct Error(pub &'static str); +pub enum Error { + /// Attempted to push to a full vector. + CapacityExceeded, + + /// Attempted to resize the vector to a length greater than its fixed capacity. + LengthTooLarge, +} /// A stack-allocated vector with fixed capacity and dynamic length. /// @@ -85,10 +90,10 @@ impl StaticVector { /// /// # Errors /// - /// Returns [`Error`] if the vector is already at full capacity. + /// Returns [`Error::CapacityExceeded`] if the vector is already at full capacity. pub fn push(&mut self, value: &T) -> Result<(), Error> { if self.length == CAPACITY { - return Err(Error("capacity")); + return Err(Error::CapacityExceeded); } self.data[self.length].write(value.clone()); @@ -112,13 +117,13 @@ impl StaticVector { /// /// # Errors /// - /// Returns [`Error`] if `new_length` exceeds the vector's fixed capacity. + /// Returns [`Error::LengthTooLarge`] if `new_length` exceeds the vector's fixed capacity. pub fn set_len(&mut self, new_length: usize) -> Result<(), Error> where T: Default, { if new_length > CAPACITY { - return Err(Error("new length > capacity")); + return Err(Error::LengthTooLarge); } if new_length > self.length { @@ -269,7 +274,7 @@ mod tests { let mut vec = StaticVector::::new(); assert!(vec.push(&1).is_ok()); assert!(vec.push(&2).is_ok()); - assert!(vec.push(&3).is_err()); + assert!(matches!(vec.push(&3), Err(Error::CapacityExceeded))); assert_eq!(vec.get(0).unwrap(), &1); assert_eq!(vec.get(1).unwrap(), &2); @@ -291,7 +296,7 @@ mod tests { assert_eq!(vec.len(), 1); assert!(!vec.is_empty()); - assert!(vec.set_len(100).is_err()); + assert!(matches!(vec.set_len(100), Err(Error::LengthTooLarge))); vec.clear(); assert_eq!(vec.len(), 0);