Skip to content

Use more complex example #5

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 4 commits 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
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A no-std, stack-allocated vector with fixed capacity and dynamic length.

`StaticVector` stores elements on the stack using a fixed-size array without heap allocations.
[`StaticVector`] stores elements on the stack using a fixed-size array without heap allocations.

Aims to be suitable for low-level projects and to have an API as safe and explicit as possible.
The goal is to allocate only when needed. When first constructed, the vector will not allocate.
Expand All @@ -21,21 +21,47 @@ The goal is to allocate only when needed. When first constructed, the vector wil

## Requirements

- `T: Clone` for insertion: `push`
- `T: Default` only if `set_len` is used
- `T: Clone` for insertion: [`StaticVector::push()`]
- `T: Default` only if [`StaticVector::set_len()`] is used
- `CAPACITY > 0`

## Complexity

All operations are O(1) except:

| Method | Time Complexity | Space Complexity |
|-------------|----------------------------------|---------------------------------|
| `clear` | O(current length) | O(1) |
| `set_len` | O(new length - current length) | O(new length - current length) |

## Example

```rust
use static_vector::StaticVector;

let mut vec = StaticVector::<i32, 4>::new();
vec.push(&1).unwrap();
vec.push(&2).unwrap();
assert_eq!(vec.len(), 2);
let mut vec = StaticVector::<i32, 3>::new();

vec.push(&4).unwrap();
vec.push(&5).unwrap();
vec.push(&6).unwrap();
assert_eq!(vec.len(), 3);
assert_eq!(vec.first(), Some(&4));

let sum_of_even_numbers = vec.iter().filter(|n| *n % 2 == 0).sum::<i32>();
assert_eq!(sum_of_even_numbers, 10);

vec.push(&2).unwrap_err();
assert_eq!(vec.len(), 3);

match vec.set_len(1) {
Ok(()) => assert_eq!(vec.len(), 1),
Err(err) => eprintln!("{:?}", err),
}

vec.clear();
assert!(vec.is_empty());
```

## Development on Linux

See [Makefile](Makefile).
See [Makefile](https://github.com/andreiavrammsd/static_vector.rs/blob/master/Makefile).
30 changes: 1 addition & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
//! A no-std, stack-allocated vector with fixed capacity and dynamic length.
//!
//! [`StaticVector`] stores elements on the stack using a fixed-size array without heap allocations.
//!
//! Aims to be suitable for low-level projects and to have an API as safe and explicit as possible.
//! The goal is to allocate only when needed. When first constructed, the vector will not allocate.
//!
//! It's a learning project, so there are no guarantees.
//!
//! # Features
//! - No heap allocation (`#![no_std]` compatible)
//! - Supports iteration, mutable access, clearing, resizing
//! - Compile-time enforced capacity
//!
//! # Requirements
//! - `T: Clone` for insertion: [`StaticVector::push()`]
//! - `T: Default` only if [`StaticVector::set_len()`] is used
//! - `CAPACITY > 0`
//!
//! # Example
//! ```rust
//! use static_vector::StaticVector;
//!
//! let mut vec = StaticVector::<i32, 4>::new();
//! vec.push(&1).unwrap();
//! vec.push(&2).unwrap();
//! assert_eq!(vec.len(), 2);
//! ```

#![no_std]
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]

use core::{array, mem::MaybeUninit};

Expand Down