Skip to content

Handle error cases by type #3

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

Merged
merged 1 commit into from
May 18, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand All @@ -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.
///
Expand Down Expand Up @@ -85,10 +90,10 @@ impl<T: Clone, const CAPACITY: usize> StaticVector<T, CAPACITY> {
///
/// # 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());
Expand All @@ -112,13 +117,13 @@ impl<T: Clone, const CAPACITY: usize> StaticVector<T, CAPACITY> {
///
/// # 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 {
Expand Down Expand Up @@ -269,7 +274,7 @@ mod tests {
let mut vec = StaticVector::<i32, 2>::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);
Expand All @@ -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);
Expand Down