[][src]Trait drop_take::DropTake

pub trait DropTake {
    type Values;
    fn drop_take(_: Self::Values);
}

Take ownership of members of a type during Drop::drop instead of only having a &mut to them.

This trait can be derived, but doing so does not implement the trait; rather, it implements Drop and compels you to implement DropTake.

Your DropTake implementation receives a tuple of all values marked #[drop_take], with their value extracted from the container by Take.

If you need access to other non-#[drop_take] values, either also #[drop_take] them, or use Take::take (or the functions it is a wrapper for) directly in Drop::drop.

Example

use std::mem::ManuallyDrop;
use drop_take::DropTake;

struct Peach;
struct Banana;
struct Melon;

#[derive(DropTake)]
struct FruitBox {
    #[drop_take]
    peach: ManuallyDrop<Peach>,
    melon: Melon,
    #[drop_take]
    banana: ManuallyDrop<Banana>,
}

impl DropTake for FruitBox {
    type Values = (Peach, Banana);
    fn drop_take((peach, banana): Self::Values) {
        // use `peach` and `banana` by value
        // they're dropped at the end of scope
    }
}

Associated Types

Required Methods

Implementors