Crates.io | circular-array |
lib.rs | circular-array |
version | 0.2.2 |
source | src |
created_at | 2024-04-03 04:26:05.297043 |
updated_at | 2024-04-03 13:43:47.968562 |
description | A circular array that allows infinite pushes into a fixed-size array. |
homepage | |
repository | https://github.com/boojongmin/circular-array |
max_upload_size | |
id | 1194742 |
size | 12,090 |
Welcome to the CircularArray
documentation. CircularArray
is a Rust module that implements a circular array, allowing for infinite pushes into a fixed-size array. This document will guide you through using CircularArray
, including how to manipulate array indices directly and how to convert the circular array into a standard fixed-size array.
Index
and IndexMut
traits.To use CircularArray
, include it in your Rust project and follow the examples below to understand how to interact with it.
This example demonstrates how to manipulate array elements using indices. It shows how to modify elements directly and verify their values using assertions.
#[test]
#[allow(non_snake_case)]
fn test_Index_and_IndexMut() {
let mut arr = CircularArray::<3, u32>::new();
arr.push(0);
arr.push(0);
arr.push(0);
arr.push(0);
arr.push(0);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
assert_eq!(arr[0], 1);
assert_eq!(arr[1], 2);
assert_eq!(arr[2], 3);
}
This example illustrates how to convert the state of the CircularArray
into a fixed-size array. It also shows how the circular nature of the array works with the push
method, where new elements replace the oldest ones in the array.
#[test]
fn test_to_array() {
let mut arr = CircularArray::<3, u32>::new();
arr.push(1);
arr.push(2);
arr.push(3);
assert_eq!(arr.to_array(), [1, 2, 3]);
arr.push(4);
assert_eq!(arr.to_array(), [2, 3, 4]);
}
CircularArray
provides a powerful and flexible way to work with fixed-size arrays in Rust, especially when dealing with data streams that require overwriting old data with new entries. Through the use of indexing traits and conversion methods, it offers an intuitive API for managing and interacting with circular arrays.