Crate sealingslice

source ·
Expand description

This crate impements a slice that can seal parts of itself off from mutability, and hand out the sealed parts as immutable references.

A typical application is message processing in constrained environments where some normalization is done in-place (because no message copy can be allocated), and the message is sent off piecewise for further processing.

A stand-alone usage example that capitalizes text in place and passes it out word by word:

let mut text = b"lorem ipsum dolor sit amet".to_vec();
let mut s = sealingslice::SealingSlice::new(&mut text[..]);
let mut capitalized = vec![];
let mut lastword = 0;
loop {
    let n;
    {
        let tail = s.mutable();
         
        if tail.len() == 0 {
            break;
        }
         
        // 'a' to 'z'
        if tail[0] >= 0x61 && tail[0] <= 0x7a {
            // ASCII shift to capital letter
            tail[0] -= 0x20;
        }
         
        let space = tail.iter().position(|&c| c == 0x20);
        let space = space.unwrap_or(tail.len());
        n = std::cmp::min(space + 1, tail.len());
    }
    s.seal(n);

    capitalized.push(std::str::from_utf8(&s.sealed()[lastword..]).unwrap());
    lastword = s.sealed().len();
}
assert_eq!(capitalized, &["Lorem ", "Ipsum ", "Dolor ", "Sit ", "Amet"]);

Structs

A slice that can seal parts of itself off from mutability, and hand out the sealed parts as immutable references.