Should slice.swap() require unpin

For various reasons, I've been writing some sorting methods out, and slice.swap() allows me to avoid requiring "clone"

pub fn insert_sort<T: PartialOrd>(v: &mut [T]) {
    //fewest swaps
    for start in 0..v.len() {
        let mut best = start;
        for i in start..v.len() {
            if v[i] < v[best] {
                best = i;
            }
        }
        v.swap(start, best);
    }
}

But as far as I can tell, if that slice contained pinned data, it would swap it without blinking.

Is there something I'm missing that makes this OK?

I'm also aware that as a stable feature changing this would e tricky, but would it make sense to tweak for the next edition?

Pinning does not project "through" types like Vec automatically: https://doc.rust-lang.org/std/pin/index.html#projections-and-structural-pinning.

Thanks

I think I understand my mistake. I was treating Pin/Unpin as traits, instead of Pin as a pointer type.

You actually can swap the locations of the pointers, but not the locations of the data pointed to.

Correct me if I'm wrong, Pin has been confusing me for some time.

Not just you, pin has very precise semantics, and is rather easy to use incorrectly.

4 Likes