Skip to main content

CDrop

Derive Macro CDrop 

Source
#[derive(CDrop)]
{
    // Attributes available to this derive:
    #[no_drop_impl]
    #[nullable]
}
Expand description

Derive CDrop and (by default) [Drop] for a struct or unit enum.

The generated do_drop releases every owning pointer field by calling RawPointerConverter::drop_raw_pointer, which takes the value back from the raw pointer and lets it drop. Non-pointer fields are left to Rust’s regular drop glue.

Deriving CDrop assumes the struct owns its pointer fields and was initialized via CReprOf::c_repr_of. Derive CReprOf and CDrop together to keep their assumptions in sync.

The default output also emits a Drop impl that calls do_drop, so that letting a value go out of scope releases its pointer fields. A CDrop impl without a matching Drop leaks every pointer field on scope exit.

§Struct-level attributes

  • #[no_drop_impl] — generate only the CDrop impl and skip the blanket Drop impl. Use this when you need a manual Drop; that manual Drop should call do_drop, otherwise the pointer fields leak.

§Field-level attributes

  • #[nullable] — skip the free when the pointer is null. This is the same attribute that CReprOf and AsRust read on the field; annotate the field once and all three derives stay in sync.