References (&T) are Copy.

Huon

On 01/06/14 09:42, Tommi wrote:
On 2014-06-01, at 1:02, Patrick Walton <[email protected] <mailto:[email protected]>> wrote:

   fn my_transmute<T:Clone,U>(value: T, other: U) -> U {
       let mut x = Left(other);
       let y = match x {
           Left(ref mut y) => y,
           Right(_) => fail!()
       };
       *x = Right(value);
       (*y).clone()
   }

If `U` implements `Copy`, then I don't see a (memory-safety) issue here. And if `U` doesn't implement `Copy`, then it's same situation as it was in the earlier example given by Matthieu, where there was an assignment to an `Option<Box<str>>` variable while a different reference pointing to that variable existed. The compiler shouldn't allow that assignment just as in your example the compiler shouldn't allow the assignment `x = Right(value);` (after a separate reference pointing to the contents of `x` has been created) if `U` is not a `Copy` type.

But, like I said in an earlier post, even though I don't see this (transmuting a `Copy` type in safe code) as a memory-safety issue, it is a code correctness issue. So it's a compromise between preventing logic bugs (in safe code) and the convenience of more liberal mutation.



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



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

Reply via email to