9 unstable releases (3 breaking)

0.4.2 Nov 29, 2023
0.4.1 Nov 14, 2023
0.3.0 May 15, 2023
0.2.0 Apr 23, 2023
0.1.3 Apr 14, 2023

#1806 in Filesystem

Download history 586/week @ 2025-08-18 479/week @ 2025-08-25 716/week @ 2025-09-01 1113/week @ 2025-09-08 969/week @ 2025-09-15 799/week @ 2025-09-22 2128/week @ 2025-09-29 1256/week @ 2025-10-06 2860/week @ 2025-10-13 2203/week @ 2025-10-20 1091/week @ 2025-10-27 920/week @ 2025-11-03 770/week @ 2025-11-10 1234/week @ 2025-11-17 464/week @ 2025-11-24 248/week @ 2025-12-01

2,745 downloads per month
Used in 5 crates (2 directly)

MIT license

25KB
463 lines

SwapVec

A vector which swaps to disk when exceeding a certain length.

Useful if you do not want to use a queue, but first collecting all data and then consuming it.

Imagine multiple threads slowly producing giant vectors of data, passing it to a single consumer later on.

Or a CSV upload of multiple gigabytes to an HTTP server, in which you want to validate every line while uploading, without directly starting a Database transaction or keeping everything in memory.

Features

  • Multiplatform (Linux, Windows, MacOS)
  • Creates temporary file only after exceeding threshold
  • Works on T: Serialize + Deserialize + Clone
  • Temporary file removed even when terminating the program
  • Checksums to guarantee integrity
  • Can be moved across threads

Limitations

  • Due to potentially doing IO, most actions are wrapped in a Result
  • Currently, no "start swapping after n MiB" is implemented
    • Would need element wise space calculation due to heap elements (e.g. String)
  • Compression currently does not compress. It is there to keep the API stable.
  • No async support (yet)
  • When pushing elements or consuming iterators, SwapVec is "write only"
  • Only forwards iterations
    • Can be reset though

Examples

Basic Usage

use swapvec::SwapVec;
let iterator = (0..9).into_iter();
let mut much_data = SwapVec::default();
// Starts using disk for big iterators
much_data.consume(iterator).unwrap();
for value in much_data.into_iter() {
    println!("Read back: {}", value.unwrap());
}

Examples

Currently there is only one simple example, doing some basic operations and getting metrics like getting the batches/bytes written to file. . Run it with

cargo run --example demo

Dependencies

~2.7–7MB
~158K SLoC