SSheldon / rust-objc

Objective-C Runtime bindings and wrapper for Rust.
https://crates.io/crates/objc
MIT License
393 stars 57 forks source link

mem::swap doesn't work with Objects #6

Open SSheldon opened 10 years ago

SSheldon commented 10 years ago

For example, the following code:

let mut a: Id<NSString> = INSString::from_str("a");
let mut b: Id<NSString> = INSString::from_str("b");
println!("{} {}", a, b);

mem::swap(a.deref_mut(), b.deref_mut());
println!("{} {}", a, b);

Expected output:

a b
b a

Actual output:

a b
a b

We cannot have the expected output happen, because an NSObject's memory address cannot change after it has been constructed.

SSheldon commented 9 years ago

The ideal fix for this would be to make NSObjects not Sized; then the example code wouldn't even compile. Unfortunately, it doesn't seem that there's a way to make a struct not Sized without the struct using "fat" (two-word) references, which would be incorrect for an NSObject.

SSheldon commented 9 years ago

If rust-lang/rfcs#709 were accepted, this could be resolved by marking NSObjects as NotSized.

SSheldon commented 9 years ago

The unsized types RFC was postponed as rust-lang/rfcs#813.

SSheldon commented 7 years ago

If rust-lang/rfcs#1861 is accepted, we could fix this by making Object an opaque extern type.

SSheldon commented 6 years ago

The extern types RFC was accepted and implemented! Stabilization tracked in rust-lang/rust#43467.

nox commented 6 years ago

Getting mutable references to void types is undefined behaviour to begin with, will file an issue.