On Sun, Feb 10, 2013 at 12:22 AM, Niko Matsakis <[email protected]> wrote:

>
>
> Steven Blenkinsop wrote:
>
>> If either of these is the case, then the update/replace function wouldn't
>> be safe either, since f could read v.l indirectly after v.l has been moved
>> into f, breaking uniqueness guarantees.
>>
>
> There are other ways to have aliases as well, for example as part of a
> closure.  But you're correct, the function I proposed wouldn't be safe.
>  Not sure what I was thinking.  In any case, the other technique I
> mentioned works just fine (swapping with None or what have you)


Sure. And you can get rid of the allocation by moving the ~ into Cons, so
that MT variants don't require allocation:

extern mod std;

// a list that uses owned pointers
pub enum IntList {
MT,
Cons(~(int, IntList))
}

// a structure containing a list
pub struct HasList {
a : ~str,
l : IntList
}

// add a '3' at the head of the contained list
fn extendH(v: &mut HasList) {
let w = util::replace(&mut v.l, MT);
v.l = Cons(~(3, w));
}

#[test] fn t1() {
let h = ~mut HasList {a: ~"bogus", l: MT};
extendH(h);
}
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to