On 5/31/14 2:44 PM, Tommi wrote:
I don't understand that last sentence. How could you use `transmute` in
safe code given that it's an `unsafe` function?
I mean you could *write* transmute in safe code. Look:
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()
}
Just by using two mutable references to the same location, I have
created a function that can cast any clonable type to any other type,
given at least one instance of the two. I didn't use any memory
allocation at all.
This was discussed quite a bit on the mailing list years ago.
The compiler should make sure that you can't deallocate memory that
could potentially be accessible through multiple different variables (be
they references or owning variables). But all other kind of mutation
through different variables in a single-threaded code should be
memory-safe at least in some definition of that word.
No, it is not memory safe. See above.
Patrick
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev