ideasman42
ideasman42

Reputation: 48128

How to detect when calling 'drop' is needed for a type?

When types such as Vec run truncate, they call ptr::drop_in_place on every member of the vector.

This turns out not to be a performance problem for primitive types such as int's, float's etc... because the drop calls are optimized out.

For more involved container types (ones I've written for my own use), iterating over elements may not be so trivial (and may not always get optimized away).

Is there a way to check if a type has a drop trait so visiting all members can be avoided in that case?

Upvotes: 8

Views: 1052

Answers (2)

user395760
user395760

Reputation:

Use std::mem::needs_drop.

As per the description, this function is safe, and therefore not calling the destructor should be sound.

Prior to 1.21.0

The std::intrinsics::needs_drop could be used, though requiring a (perma-unstable) feature to be activated.

It is still usable, and still unstable.

Upvotes: 6

oli_obk
oli_obk

Reputation: 31253

You cannot reason about Drop in generic bounds. Types which don't have an explicit Drop impl, don't fulfill a Drop bound:

fn foo<T: Drop>(t: T) {}

foo(5);
foo("5".to_owned());

Only types with an explicit Drop impl fulfill this bound:

foo(vec![42]); // works

Drop is silently handled by the compiler in the background (otherwise you'd have to specify it everywhere). This means, that not even specialization or OIBITs can be used as a workaround.

That said, you can detect whether any (even a generated) drop impl exists (but you shouldn't) by turning a value of the type into a trait object and checking whether the vtable's drop entry is NULL or not.

Upvotes: 3

Related Questions