Skip to content

Commit

Permalink
Towards v0.1.0 (#5)
Browse files Browse the repository at this point in the history
* README updated and image added

* README and manifest updated

* StackVec replaced by LocalVec

* diagram updated

* travis badge added
  • Loading branch information
m-rinaldi authored Aug 23, 2021
1 parent 33066a2 commit 9cac680
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "stack_vec"
name = "local_vec"
version = "0.1.0"
edition = "2018"
authors = ["Jorge Rinaldi <jrg.rinaldi@gmail.com>"]
license = "MIT"
repository = "https://github.com/m-rinaldi/stack_vec"
repository = "https://github.com/m-rinaldi/local_vec"
description = "fixed-capacity vector allocated on the stack"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
# `stack_vec`
# `local_vec`

A *fixed-capacity* vector allocated on the stack.

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Build Status](https://app.travis-ci.com/m-rinaldi/stack_vec.svg?branch=main)](https://app.travis-ci.com/m-rinaldi/stack_vec)

[![Build Status](https://app.travis-ci.com/m-rinaldi/local_vec.svg?branch=main)](https://app.travis-ci.com/m-rinaldi/local_vec)

---

`stack_vec::StackVec` is a *fixed-capacity* vector, i.e., its *size* or *length* increases and decreases as elements are pushed into and popped from the vector, respectively. However, its *capacity* remains always the same.
`local_vec::LocalVec` is a *fixed-capacity* vector, i.e., its *size* or *length* increases and decreases as elements are pushed into and popped from the vector, respectively. However, its *capacity* remains always the same.

`StackVec`'s elements reside inside it:
`LocalVec`'s elements reside inside it:

use stack_vec::StackVec;
let mut vec = StackVec::<_, 4>::new();
use local_vec::LocalVec;
let mut vec = LocalVec::<_, 4>::new();
vec.push(3);
vec.push(7);

`vec` contents in the code above are graphically represented as:

<p align="center">
<img src="img/StackVec.png">
<img src="img/LocalVec.png">
</p>


In contrast, [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html) allocates a buffer on the heap and contains a pointer to that buffer instead of the buffer itself.


The capacity of a `StackVec` must be determined at compile-time as a constant argument.
The capacity of a `LocalVec` must be determined at compile-time as a constant argument.

Binary file added img/LocalVec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/drop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::StackVec;
use crate::LocalVec;

impl <T, const N: usize> Drop for StackVec<T, N> {
impl <T, const N: usize> Drop for LocalVec<T, N> {
fn drop(&mut self) {
self.clear();
}
Expand All @@ -25,12 +25,12 @@ mod tests {
}
}

use crate::StackVec;
use crate::LocalVec;

#[test]
fn test_drop() {
let mut cnt = 0u8;
let mut buf = StackVec::<_, 3>::new();
let mut buf = LocalVec::<_, 3>::new();

assert_eq!(cnt, 0);

Expand Down
12 changes: 6 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::StackVec;
use crate::LocalVec;

impl<T, const N: usize> std::ops::Index<usize> for StackVec<T, N> {
impl<T, const N: usize> std::ops::Index<usize> for LocalVec<T, N> {
type Output = T;

fn index(&self, idx: usize) -> &Self::Output {
Expand All @@ -16,7 +16,7 @@ impl<T, const N: usize> std::ops::Index<usize> for StackVec<T, N> {
}
}

impl<T, const N: usize> std::ops::IndexMut<usize> for StackVec<T, N> {
impl<T, const N: usize> std::ops::IndexMut<usize> for LocalVec<T, N> {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
if idx >= self.len {
panic!("out of bounds access");
Expand All @@ -32,11 +32,11 @@ impl<T, const N: usize> std::ops::IndexMut<usize> for StackVec<T, N> {

#[cfg(test)]
mod tests {
use crate::StackVec;
use crate::LocalVec;

#[test]
fn test_index() {
let mut vec = StackVec::<_, 3>::new();
let mut vec = LocalVec::<_, 3>::new();

vec.push(0);
vec.push(1);
Expand All @@ -49,7 +49,7 @@ mod tests {

#[test]
fn test_index_mut() {
let mut vec = StackVec::<_, 3>::new();
let mut vec = LocalVec::<_, 3>::new();

vec.push(0);
vec.push(1);
Expand Down
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ mod index;

#[derive(Debug)]
/// A fixed-capacity vector that directly stores its elements
pub struct StackVec<T, const N: usize> {
pub struct LocalVec<T, const N: usize> {
buf: [MaybeUninit<T>; N],
len: usize,
}

impl<T, const N: usize> StackVec<T, N> {
impl<T, const N: usize> LocalVec<T, N> {
pub fn new() -> Self {
let buf: [MaybeUninit<T>; N] = unsafe {
MaybeUninit::uninit().assume_init()
};

StackVec {
LocalVec {
buf,
len: 0,
}
Expand All @@ -38,7 +38,7 @@ impl<T, const N: usize> StackVec<T, N> {
N
}

/// It panics if the vector is full
/// panics if the vector is full
pub fn push(&mut self, val: T) {
if self.len >= N {
panic!("capacity excedeed");
Expand Down Expand Up @@ -70,19 +70,19 @@ impl<T, const N: usize> StackVec<T, N> {

#[cfg(test)]
mod tests {
use super::StackVec;
use super::LocalVec;

#[test]
fn test_new() {
let vec = StackVec::<u32, 4>::new();
let vec = LocalVec::<u32, 4>::new();
assert_eq!(vec.len(), 0);
assert_eq!(vec.capacity(), 4);
}

#[test]
#[should_panic]
fn test_push_on_full() {
let mut vec = StackVec::<_, 1>::new();
let mut vec = LocalVec::<_, 1>::new();
vec.push(0);

assert!(vec.is_full());
Expand All @@ -92,7 +92,7 @@ mod tests {

#[test]
fn test_push() {
let mut vec = StackVec::<_, 3>::new();
let mut vec = LocalVec::<_, 3>::new();

assert!(vec.is_empty());
assert_eq!(vec.len(), 0);
Expand All @@ -110,7 +110,7 @@ mod tests {

#[test]
fn test_pop_on_empty() {
let mut vec = StackVec::<_, 1>::new();
let mut vec = LocalVec::<_, 1>::new();
assert!(vec.is_empty());
matches!(vec.pop(), None);

Expand All @@ -124,7 +124,7 @@ mod tests {

#[test]
fn test_push_and_pop() {
let mut vec = StackVec::<_, 4>::new();
let mut vec = LocalVec::<_, 4>::new();
assert!(vec.is_empty());
matches!(vec.pop(), None);

Expand All @@ -141,7 +141,7 @@ mod tests {

#[test]
fn test_clear() {
let mut vec = StackVec::<_,3>::new();
let mut vec = LocalVec::<_,3>::new();
vec.clear();
assert!(vec.is_empty());

Expand Down

0 comments on commit 9cac680

Please sign in to comment.