rep_move 0.1.1

Replicating iterators: N-1 clones plus one final move of the original value
Documentation
  • Coverage
  • 94.74%
    18 out of 19 items documented5 out of 19 items with examples
  • Size
  • Source code size: 57.3 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.2 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • wisdgod/rep_move
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • wisdgod

rep_move

crates.io docs.rs

Replicating iterators: build N values from one source with N−1 clones — the original is moved out on the final iteration, saving one clone for heap-backed types like Vec, String, or Arc-wrapped data.

#![no_std], zero dependencies.

Example

use rep_move::RepMove;

let original = vec![1, 2, 3];
let copies: Vec<_> = RepMove::new(original, Vec::clone, 4).collect();
// 3 heap allocations instead of 4 — the last one was moved for free.
assert_eq!(copies.len(), 4);

Types

Type Output Length known up front?
RepMove T Yes — ExactSizeIterator
RepMap U Yes — ExactSizeIterator
DynamicRepMove T No — the closure controls the count
DynamicRepMap U No — the closure controls the count

What std can't express: RepMap

core::iter::repeat_n covers the symmetric clone-then-move case. The *RepMap types go further: the first N−1 items are built from &T and the final one consumes T, both producing some other type U — the last conversion reuses the source's resources instead of cloning them first:

use rep_move::RepMap;

let s = String::from("hello");
let boxes: Vec<Box<str>> = RepMap::new(
    s,
    |s: &String| s.as_str().into(), // N−1 items: one allocation each
    String::into_boxed_str,         // final item: zero allocations
    3,
).collect();

The std route would have to clone the String first and convert afterwards, paying an extra allocation for every item.

O(1) skips

last(), count() and nth() on RepMove / RepMap never materialize the skipped replicas — iter.last() is just the moved source, with zero clones, exactly like core::iter::repeat_n.

Full API documentation lives in the source docs: https://docs.rs/rep_move

Feature flags

  • nightly (disabled by default) — makes the constructors const fn and Replicator a const trait; additionally implements the unstable TrustedLen contract and specializes the unstable Iterator::advance_by for RepMove / RepMap. Requires a nightly compiler.

License

Licensed under the MIT license.