# Pencil-Box
A simple and efficient Rust utility to split slices into fixed-size chunks, returning owned `Vec<T>` chunks.
---
## ๐ฆ Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
pencil-box = "0.1.1"
```
> Replace `"0.1.0"` with the latest version from [crates.io](https://crates.io/crates/pencil-box)
---
## ๐ Usage
```rust
use pencil_box::chunk;
fn main() {
let data = vec![1, 2, 3, 4, 5];
let result = chunk(&data, 2).unwrap();
assert_eq!(result, vec![vec![1, 2], vec![3, 4], vec![5]]);
}
```
---
## ๐งฉ Function Behavior
```rust
pub fn chunk<T: Clone>(array: &[T], chunk_size: usize) -> Result<Vec<Vec<T>>, &'static str>
```
The `chunk` function behaves as follows:
- โ
Returns an error if `chunk_size == 0`
- โ
Returns an empty vector if the input slice is empty
- โ
Returns a single chunk containing all elements if `chunk_size >= array.len()`
- โ
Returns multiple chunks of up to `chunk_size` elements otherwise
Each chunk is a cloned `Vec<T>`, preserving data integrity and independence.
---
## ๐งช Test Coverage
The implementation is covered by comprehensive unit tests, including:
- โ
Primitive types: `i32`, `bool`, etc.
- โ
Strings and owned `String`
- โ
Structs (`Clone + PartialEq`)
- โ
Enums (e.g., `Status::Ok`, `Status::Error`)
- โ
Nested collections (e.g., `Vec<Vec<T>>`)
- โ
Edge cases:
- Empty input
- `chunk_size == 0` (returns error)
- `chunk_size >= input.len()` (returns single chunk)
To run tests:
```bash
cargo test
```
---
## ๐ Safety
- 100% safe Rust (`#![forbid(unsafe_code)]`)
- No `unsafe` blocks used
- Pure functional logic
---
## ๐ License
This project is dual-licensed under either:
- [MIT](LICENSE-MIT) OR
- [Apache 2.0](LICENSE-APACHE)
You may freely choose either license.
---
## ๐ค Contributing
Contributions, bug reports, and feature requests are welcome.
Please open an issue or submit a pull request.
---
## ๐ Links
- [Crates.io](https://github.com/rocketnozzle/pencil-box)
- [Repository](https://github.com/rocketnozzle/pencil-box)