copyspan

Crates.iocopyspan
lib.rscopyspan
version0.1.0
created_at2025-05-29 23:54:45.720002+00
updated_at2025-05-29 23:54:45.720002+00
descriptionAn alternative to `Range` that has a defined memory layout and implements `Copy`
homepage
repositoryhttps://github.com/wr7/copyspan
max_upload_size
id1694529
size8,856
(wr7)

documentation

README

A an alternative to Range<T> that has a defined memory layout, implements [std::marker::Copy], and has some convenience methods.

use copyspan::Span;

let text = "hello world";
let s = Span::from(6..11);

for i in s {
    dbg!(i);
}

// Because `Span` is copyable, we can reuse it without calling `clone`
assert_eq!(&text[s], "world");
assert_eq!(&text[s.with_len(2)], "wo");

This is also useful for making copyable datastructures that contain ranges.

use copyspan::Span;
use std::ops::Range;

#[derive(Clone, Copy, Default)]
struct HoldsSpan {
    x: Span<usize>,
}

fn expects_range(_: Range<usize>) {}
fn takes_val(_: HoldsSpan) {}

let val = HoldsSpan::default();
takes_val(val); // If `HoldSpan` wasn't `Copy`, `val` would be moved into this function

expects_range(val.x.range());
Commit count: 1

cargo fmt