SwapVec

Struct SwapVec 

Source
pub struct SwapVec<T>
where for<'a> T: Serialize + Deserialize<'a>,
{ /* private fields */ }
Expand description

An only growing array type which swaps to disk, based on it’s initial configuration.

Create a mutable instance, and then pass iterators or elements to grow it.

let mut bigvec = swapvec::SwapVec::default();
let iterator = (0..9);
bigvec.consume(iterator);
bigvec.push(99);
let new_iterator = bigvec.into_iter();

Implementations§

Source§

impl<T> SwapVec<T>
where for<'a> T: Serialize + Deserialize<'a> + Clone,

Source

pub fn with_config(config: SwapVecConfig) -> Self

Intialize with non-default configuration.

Examples found in repository?
examples/demo.rs (line 13)
5fn main() {
6    let element_count = DATA_MB / 8;
7    let big_iterator = 0..element_count * 1024 * 1024;
8
9    let config = swapvec::SwapVecConfig {
10        batch_size: 8 * 1024,
11        ..SwapVecConfig::default()
12    };
13    let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14    swapvec.consume(big_iterator.into_iter()).unwrap();
15
16    println!("Data size: {}MB", DATA_MB);
17    println!("Done. Batches written: {}", swapvec.batches_written());
18    println!(
19        "Filesize: {}MB",
20        swapvec
21            .file_size()
22            .map(|x| x as f32 / 1024. / 1024.)
23            .unwrap_or(0.)
24    );
25    println!("Read back");
26
27    let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29    println!("Elements read back: {}", read_back.len());
30}
Source

pub fn consume( &mut self, it: impl Iterator<Item = T>, ) -> Result<(), SwapVecError>

Give away an entire iterator for consumption.
Might return an error, due to possibly triggered batch flush (IO).

Examples found in repository?
examples/demo.rs (line 14)
5fn main() {
6    let element_count = DATA_MB / 8;
7    let big_iterator = 0..element_count * 1024 * 1024;
8
9    let config = swapvec::SwapVecConfig {
10        batch_size: 8 * 1024,
11        ..SwapVecConfig::default()
12    };
13    let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14    swapvec.consume(big_iterator.into_iter()).unwrap();
15
16    println!("Data size: {}MB", DATA_MB);
17    println!("Done. Batches written: {}", swapvec.batches_written());
18    println!(
19        "Filesize: {}MB",
20        swapvec
21            .file_size()
22            .map(|x| x as f32 / 1024. / 1024.)
23            .unwrap_or(0.)
24    );
25    println!("Read back");
26
27    let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29    println!("Elements read back: {}", read_back.len());
30}
Source

pub fn push(&mut self, element: T) -> Result<(), SwapVecError>

Push a single element. Might return an error, due to possibly triggered batch flush (IO). Will write at most one batch per insert. If swap_after is bigger than batch_size and a file is created, every insert will write one batch to disk, until the elements in memory have a count smaller than or equal to batch size.

Source

pub fn written_to_file(&self) -> bool

Check if enough items have been pushed so that the temporary file has been created.
Will be false if element count is below swap_after and below batch_size

Source

pub fn file_size(&self) -> Option<usize>

Get the file size in bytes of the temporary file. Might do IO and therefore could return some Result.

Examples found in repository?
examples/demo.rs (line 21)
5fn main() {
6    let element_count = DATA_MB / 8;
7    let big_iterator = 0..element_count * 1024 * 1024;
8
9    let config = swapvec::SwapVecConfig {
10        batch_size: 8 * 1024,
11        ..SwapVecConfig::default()
12    };
13    let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14    swapvec.consume(big_iterator.into_iter()).unwrap();
15
16    println!("Data size: {}MB", DATA_MB);
17    println!("Done. Batches written: {}", swapvec.batches_written());
18    println!(
19        "Filesize: {}MB",
20        swapvec
21            .file_size()
22            .map(|x| x as f32 / 1024. / 1024.)
23            .unwrap_or(0.)
24    );
25    println!("Read back");
26
27    let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29    println!("Elements read back: {}", read_back.len());
30}
Source

pub fn batches_written(&self) -> usize

Basically int(elements pushed / batch size)

Examples found in repository?
examples/demo.rs (line 17)
5fn main() {
6    let element_count = DATA_MB / 8;
7    let big_iterator = 0..element_count * 1024 * 1024;
8
9    let config = swapvec::SwapVecConfig {
10        batch_size: 8 * 1024,
11        ..SwapVecConfig::default()
12    };
13    let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14    swapvec.consume(big_iterator.into_iter()).unwrap();
15
16    println!("Data size: {}MB", DATA_MB);
17    println!("Done. Batches written: {}", swapvec.batches_written());
18    println!(
19        "Filesize: {}MB",
20        swapvec
21            .file_size()
22            .map(|x| x as f32 / 1024. / 1024.)
23            .unwrap_or(0.)
24    );
25    println!("Read back");
26
27    let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29    println!("Elements read back: {}", read_back.len());
30}

Trait Implementations§

Source§

impl<T: Serialize + for<'a> Deserialize<'a>> Debug for SwapVec<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Serialize + for<'a> Deserialize<'a>> Default for SwapVec<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Serialize + for<'a> Deserialize<'a> + Clone> IntoIterator for SwapVec<T>

Source§

type Item = Result<T, SwapVecError>

The type of the elements being iterated over.
Source§

type IntoIter = SwapVecIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SwapVec<T>

§

impl<T> !RefUnwindSafe for SwapVec<T>

§

impl<T> !Send for SwapVec<T>

§

impl<T> !Sync for SwapVec<T>

§

impl<T> Unpin for SwapVec<T>
where T: Unpin,

§

impl<T> !UnwindSafe for SwapVec<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.