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

no-std owned-drop

在对象销毁时实现所有权封装的包装器

2 个版本

0.1.1 2022年12月17日
0.1.0 2022年12月14日

#4 in #no-standard-library

MIT/Apache 协议

6KB

此包允许获取销毁数据的所有权
注意:此包支持 no_std

示例

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>) {
    /*
    ...
     */
}

无运行时依赖