From aa6698e515a815086255a7abccb2d9212870fbd7 Mon Sep 17 00:00:00 2001 From: Andrei Avram <6795248+andreiavrammsd@users.noreply.github.com> Date: Sun, 18 May 2025 09:46:47 +0300 Subject: [PATCH 1/4] Use more complex example --- README.md | 32 ++++++++++++++++++++++++-------- src/lib.rs | 30 +----------------------------- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index ac6acb0..9427533 100644 --- a/README.md +++ b/README.md @@ -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. @@ -21,8 +21,8 @@ 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` ## Example @@ -30,12 +30,28 @@ The goal is to allocate only when needed. When first constructed, the vector wil ```rust use static_vector::StaticVector; -let mut vec = StaticVector::::new(); -vec.push(&1).unwrap(); -vec.push(&2).unwrap(); -assert_eq!(vec.len(), 2); +let mut vec = StaticVector::::new(); + +vec.push(&4).unwrap(); +vec.push(&5).unwrap(); +vec.push(&6).unwrap(); +assert_eq!(vec.len(), 3); + +let sum_of_even_numbers = vec.iter().filter(|n| *n % 2 == 0).sum::(); +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_eq!(vec.len(), 0); ``` ## Development on Linux -See [Makefile](Makefile). +See [Makefile](https://github.com/andreiavrammsd/static_vector.rs/blob/master/Makefile). diff --git a/src/lib.rs b/src/lib.rs index 763ccde..0f90248 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::::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}; From 720e9341178c63b4c265dd8e7c6f51cd0d25ae89 Mon Sep 17 00:00:00 2001 From: Andrei Avram <6795248+andreiavrammsd@users.noreply.github.com> Date: Sun, 18 May 2025 10:01:07 +0300 Subject: [PATCH 2/4] Add complexity analysis --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 9427533..ff49727 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ The goal is to allocate only when needed. When first constructed, the vector wil - `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 From 7db339e405019ea042899fde33a0793ef51b8d73 Mon Sep 17 00:00:00 2001 From: Andrei Avram <6795248+andreiavrammsd@users.noreply.github.com> Date: Sun, 18 May 2025 11:04:31 +0300 Subject: [PATCH 3/4] Add getter example --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ff49727..c4109ee 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ 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::(); assert_eq!(sum_of_even_numbers, 10); From ed3d9c2aba94b7395f782801a046bf7fda4edd16 Mon Sep 17 00:00:00 2001 From: Andrei Avram <6795248+andreiavrammsd@users.noreply.github.com> Date: Sun, 18 May 2025 11:09:24 +0300 Subject: [PATCH 4/4] Use is_empty --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c4109ee..6d30a20 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ match vec.set_len(1) { } vec.clear(); -assert_eq!(vec.len(), 0); +assert!(vec.is_empty()); ``` ## Development on Linux