Debian Codegraph @

third_party/rust/objc/src/rc/autorelease.rs

raw
1 use std::os::raw::c_void;
2 use runtime::{objc_autoreleasePoolPush, objc_autoreleasePoolPop};
3
4 // we use a struct to ensure that objc_autoreleasePoolPop during unwinding.
5 struct AutoReleaseHelper {
6 context: *mut c_void,
7 }
8
9 impl AutoReleaseHelper {
10 unsafe fn new() -> Self {
11 AutoReleaseHelper { context: objc_autoreleasePoolPush() }
12 }
13 }
14
15 impl Drop for AutoReleaseHelper {
16 fn drop(&mut self) {
17 unsafe { objc_autoreleasePoolPop(self.context) }
18 }
19 }
20
21 /**
22 Execute `f` in the context of a new autorelease pool. The pool is drained
23 after the execution of `f` completes.
24
25 This corresponds to `@autoreleasepool` blocks in Objective-C and Swift.
26 */
27 pub fn autoreleasepool<T, F: FnOnce() -> T>(f: F) -> T {
28 let _context = unsafe { AutoReleaseHelper::new() };
29 f()
30 }