Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SSheldon opened this issue Sep 3, 2014 · 6 comments
Open

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

SSheldon opened this issue Sep 3, 2014 · 6 comments

Comments

@SSheldon
Copy link
Owner

SSheldon commented Sep 3, 2014

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
Copy link
Owner Author

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
Copy link
Owner Author

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

@SSheldon
Copy link
Owner Author

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

@SSheldon SSheldon changed the title mem::swap doesn't work with NSObjects mem::swap doesn't work with Objects Mar 23, 2015
@SSheldon
Copy link
Owner Author

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

@SSheldon
Copy link
Owner Author

SSheldon commented Dec 6, 2017

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

@nox
Copy link

nox commented Dec 11, 2017

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

madsmtm added a commit to madsmtm/rust-objc that referenced this issue Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants