mirror of
https://github.com/mii443/tokenizers.git
synced 2025-12-09 22:28:29 +00:00
Container - Add ability to execute
This commit is contained in:
@@ -40,4 +40,20 @@ where
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute<F, U>(&self, closure: F) -> U
|
||||
where
|
||||
F: FnOnce(&Box<T>) -> U,
|
||||
{
|
||||
match self {
|
||||
Container::Owned(val) => closure(val),
|
||||
Container::Pointer(ptr) => unsafe {
|
||||
let val = Box::from_raw(*ptr);
|
||||
let res = closure(&val);
|
||||
// We call this to make sure we don't drop the Box
|
||||
Box::into_raw(val);
|
||||
res
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user