-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add scope guards to libstd #9842
Conversation
This adds a struct which runs a block when it falls out of scope, similar to `scope` in D or `defer` in Go. Using this module directly requires code to assign the ScopeGuard struct to a variable manually which is unnecessary in many situations.
This seems similar to |
It is similar, but provides more functionality. |
I remember @nikomatsakis mentioning something about preventing access to borrowed things in destructors, so it might not be possible to implement this safely without more work on closures (once destructors are sound). |
Destructors should be safe as implemented, but too limiting to express many useful cases. There is a plan to generalize them which I do not think has been fully described anywhere outside the minutes (I'll add it to my todo list). Regardless, I think the plan is to move towards RAII vs closures as much as possible, so as to minimize compilation overhead and expose the control-flow more directly to the compiler for analysis. |
I'll note that with hygiene, I do not believe it is necessary to mangle the name of the variable. |
Should this hold off until those issues are resolved? |
Closing this for now. |
This adds a struct which runs a block when it falls out of scope,
similar to
scope
in D ordefer
in Go. Using this module directlyrequires code to assign the ScopeGuard struct to a variable manually
which is unnecessary in many situations.
It'd be nice to add a syntax extension to automatically expand
to something like
but I don't think that syntax extensions can currently expand to statements as opposed to expressions or items and I'm not familiar enough with libsyntax to add it easily. I put this in libstd instead of libextra since it'll need to be there for the syntax extension to work reasonably.
cc #9835