Skip to content

RFC: zero initializer / undefined intrinsics for POD types #3471

Closed
@nikomatsakis

Description

@nikomatsakis

Perhaps we should add an intrinsic like:

fn zero<T: POD>() -> T

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions