-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Tying the knot with @ #7082
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
Comments
I presented a toy example on irc of how one might still be able to construct an @ graph with a cycle and huonw thought it might be relevant. Note that the example has to use an struct Foo {
name: ~str,
other: Option<@Foo>
}
fn circle(s:~str) -> @Foo {
unsafe {
let f = @Foo{ name:s, other:None};
let x = Some(f);
std::ptr::copy_nonoverlapping_memory::<Option<@Foo>>(std::unstable::intrinsics::transmute(&f.other), &x, 1);
f
}
}
fn main() {
let f = ~circle(~"Hi");
println(f.name);
println(f.other.get().name);
println(f.other.get().other.get().name);
} The program prints 'Hi' three times. Exciting. |
Eventually we may permit |
@nikomatsakis I'm not sure how that would allow a cycle to be created? (It would address the problem of doing complex initialisation of an |
@huonw Hmm, maybe you're right, it doesn't solve it as neatly as I thought, though I think it may still be an ingredient of a solution. My reasoning is that in order to convert from mutable to immutable soundly, you need |
I could imagine an unsafe-under-the-hood library function to help with this. It would take a list of the structs you want to link together (with |
I agree that library solutions are usually preferable, though it seems hard to approach the same level of generality. A language feature that could solve the problem without being special-cased for it would be delayed initialization of struct fields, as with
How do you safety-check that? Seems nontrivial. |
Yeah, generally seems low priority. I imagine you'd have an easier time making the graph owned at the top, allocating nodes from an arena, and making edges out of |
If you're able to make a cyclic graph with |
Nominating for the backwards compatible milestone, although I don't really think we should do this. |
I agree that having the guarantee that |
wontfix |
At the moment there doesn't seem to be a way to create an @ graph that has a cycle in it, without an
@mut
or&mut
somewhere in its ownership tree, and so one can't write something that does, for example:without using
@mut
, even though construction is the only time mutability is required.(Examples of where it would be useful: part 1, part 2, one from IRC: log, code)
The text was updated successfully, but these errors were encountered: