On 9/29/11 11:00 AM, Graydon Hoare wrote:
You're introducing either require-copy or unsafe-alias problems.
Closure-passing style tells the alias-analysis system what it needs to
keep reference-based iteration safe. You can't touch the vec while
you're pointing into it. Statically. Even if you make copies, it's very
likely nothing you return *from* the iterator can be a reference either
since they'll be hidden state inside the iterator; the alias-analysis
system can't make unsafe code safe.

For some reason the alias analysis doesn't allow me to write vec::eachi the way I'd like to:

fn eachi<@T>(f: block(T, uint) -> (), v: [mutable? T]) {
    let i = 0u;
    let l = len(v);
    while (i < l) {
        f(v[i], i);     // fails
        i += 1u;
    }
}

../src/lib/vec.rs:340:8: 340:9 error: function may alias with argument 0, which is not immutably rooted
../src/lib/vec.rs:340         f(v[i], i);

Instead I have to write:

fn eachi<@T>(f: block(T, uint) -> (), v: [mutable? T]) {
    let i = 0u;
    let l = len(v);
    while (i < l) {
        let elem = v[i];    // Satisfy alias checker
        f(elem, i);
        i += 1u;
    }
}

I guess the alias analysis is being defensive against the block here?

Patrick
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to