#no-standard-library #utility #no-std

no-std owned-drop

A wrapper that enables ownership on drop

2 releases

0.1.1 Dec 17, 2022
0.1.0 Dec 14, 2022

#5 in #no-standard-library

MIT/Apache

6KB

This crate allows one to get ownership of dropped data
Note: this crate supports no_std

Example

use owned_drop::OwnedDroppable;
use owned_drop::DropOwned;

fn do_stuff(owner: OwnerOfVec) {
    let mut drop = DropOwned::new(owner);
    
    // ...
    
    // `drop` gets dropped here and `drop_owned` gets called.
}

struct OwnerOfVec {
    data: Vec<String>,
}

impl OwnedDroppable for OwnerOfVec {
    fn drop_owned(self) {
        // This avoids a clone call one would normally have to do
        // if one only had `&mut self` instead if `self`
        function_requiring_ownership(self.data);
    }
}

fn function_requiring_ownership(data: Vec<String>) {
    /*
    ...
     */
}

No runtime deps