Skip to content

Dropping enum discriminants? #225

@joshlf

Description

@joshlf

Are we guaranteed that enum discriminants have trivial drop impls? In other words:

fn foo<T> {
    assert_eq!(std::mem::needs_drop<T>(), std::mem::needs_drop<Option<T>>());
}

The context is that I have the following code:

match self {
    Ok(...) => ...,
    Err(x) => {
        unsafe {
            let x = ptr::read(x);
            let new = // compute on x
            ptr::write(self, new);
        }
    }
}

Am I guaranteed that ptr::write(self, new) is fine even though it clobbers the enum discriminants, which are technically still initialized?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dropTopic: related to droppingC-supportCategory: Supporting a user to solve a concrete problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions