lang (drop | const-eval | static)
drop_types_in_const
Allow types with destructors to be used in static
items, const
items, and const
functions.
Some of the collection types do not allocate any memory when constructed empty (most notably Vec
). With the change to make leaking safe, the restriction on static
or const
items with destructors
is no longer required to be a hard error (as it is safe and accepted that these destructors may never run).
Allowing types with destructors to be directly used in const
functions and stored in static
s or const
s will remove the need to have
runtime-initialisation for global variables.
static
or const
items.static
s containing Drop-types will not run the destructor upon program/thread exit.const
s containing Drop-types will run the destructor at the appropriate point in the program.const fn
to return types with destructors.drop(foo)
in a const fn
).Assuming that RwLock
and Vec
have const fn new
methods, the following example is possible and avoids runtime validity checks.
/// Logging output handler
trait LogHandler: Send + Sync {
// ...
}
/// List of registered logging handlers
static S_LOGGERS: RwLock<Vec< Box<LogHandler> >> = RwLock::new( Vec::new() );
/// Just an empty byte vector.
const EMPTY_BYTE_VEC: Vec<u8> = Vec::new();
Disallowed code
static VAL: usize = (Vec::<u8>::new(), 0).1; // The `Vec` would be dropped
const fn sample(_v: Vec<u8>) -> usize {
0 // Discards the input vector, dropping it
}
Destructors do not run on static
items (by design), so this can lead to unexpected behavior when a type's destructor has effects outside the program (e.g. a RAII temporary folder handle, which deletes the folder on drop). However, this can already happen using the lazy_static
crate.
A const
item's destructor will run at each point where the const
item is used. If a const
item is never used, its destructor will never run. These behaviors may be unexpected.
lazy_static
crate currently does on stable)static
and UnsafeCell<Option<T>>
can be used to remove the dynamic allocation..dtors
support