You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RFC: zero initializer / undefined intrinsics for POD types #3471
that basically fills T with zeros. This is perfectly safe for POD types. Of course, we don't have a POD kind yet, but that's a separate issue I guess. This is particularly useful in C interop, where you often have functions with out-params that otherwise need to be initialized by hand.
We could also add an intrinsic like
fn undefined<T: POD>() -> T
which would just leave the data uninitialized. This would be more efficient but less safe in some sense. Though restricting it to POD at least preserves memory safety, more-or-less.
The idea is you'd write something like:
struct FooResult { /*something POD*/ }
extern mod c_library { fn foo(p: &mut FooResult) -> bool; }
fn foo() -> FooResult {
let result = defined();
if !c_library::foo(&mut result) { fail; }
return result;
}
I'd prefer to declare the C function as taking a &write FooResult, so the type-checker knows it can be uninitialized before the call and will be initialized after the call.
Activity
nikomatsakis commentedon Sep 12, 2012
Here is an example that came up on IRC, but it comes up regularly: http://pastebin.com/v534PrDW. Sscroll to the final function.
jruderman commentedon Sep 12, 2012
In C++, POD structs can contain pointers.
jruderman commentedon Sep 12, 2012
SpiderMonkey contains a similar
PodZerofunction:https://bugzilla.mozilla.org/show_bug.cgi?id=551276
http://hg.mozilla.org/mozilla-central/annotate/87f6f50a0207/js/src/jsutil.h#l177
jruderman commentedon Sep 12, 2012
I'd prefer to declare the C function as taking a
&write FooResult, so the type-checker knows it can be uninitialized before the call and will be initialized after the call.nikomatsakis commentedon Sep 12, 2012
Yeah, I wrote "more-or-less" because I figured
*Twould be POD, but not@Tor~T, but of course we don't make any guarantees about unsafe ptrs.nikomatsakis commentedon Nov 5, 2012
So, it turns out that this exists! (the intrinsic
init()) However, it does not check for POD and nor is it considered unsafe. Bad. See https://github.com/mozilla/rust/blob/master/src/libcore/comm.rs#L297. I'm gonna repurpose this issue to address its shortcomings.nikomatsakis commentedon Nov 5, 2012
Oh, never mind,
init()seems to fill with zero. It should still be unsafe.Aatch commentedon May 10, 2013
Closed by #6354
Auto merge of rust-lang#3471 - RalfJung:blocked, r=RalfJung
Merge rust-lang#3471