#![allow(unused)]fnmain() {
/// Inhibits compiler from automatically calling `T`’s destructor.#[lang = "manually_drop"]#[unstable(feature = "manually_drop", reason = "recently added", issue = "0")]#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]pubstructManuallyDrop<T> {
value: T,
}
impl<T> ManuallyDrop<T> {
/// Wraps a value to be manually dropped.#[unstable(feature = "manually_drop", reason = "recently added", issue = "0")]pubfnnew(value: T) -> ManuallyDrop<T> {
ManuallyDrop { value }
}
/// Extracts the value from the ManuallyDrop container.#[unstable(feature = "manually_drop", reason = "recently added", issue = "0")]pubfninto_inner(slot: ManuallyDrop<T>) -> T {
slot.value
}
/// Manually drops the contained value.////// # Unsafety////// This function runs the destructor of the contained value and thus makes any further action/// with the value within invalid. The fact that this function does not consume the wrapper/// does not statically prevent further reuse.#[unstable(feature = "manually_drop", reason = "recently added", issue = "0")]pubunsafefndrop(slot: &mut ManuallyDrop<T>) {
ptr::drop_in_place(&mut slot.value)
}
}
impl<T> Deref for ManuallyDrop<T> {
typeTarget = T;
// ...
}
impl<T> DerefMut for ManuallyDrop<T> {
// ...
}
}
#![allow(unused)]fnmain() {
structFruitBox {
// Immediately clear there’s something non-trivial going on with these fields.
peach: ManuallyDrop<Peach>,
melon: Melon, // Field that’s independent of the other two.
banana: ManuallyDrop<Banana>,
}
implDropfor FruitBox {
fndrop(&mutself) {
unsafe {
// Explicit ordering in which field destructors are run specified in the intuitive// location – the destructor of the structure containing the fields.// Moreover, one can now reorder fields within the struct however much they want.
ManuallyDrop::drop(&mutself.peach);
ManuallyDrop::drop(&mutself.banana);
}
// After destructor for `FruitBox` runs (this function), the destructor for Melon gets// invoked in the usual manner, as it is not wrapped in `ManuallyDrop`.
}
}
}