sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use libc::c_void;

use crate::sys::ffi;

/// Calls the provided closure in the context of a new autorelease pool that is drained before returning.
pub(super) unsafe fn with_autorelease<T, F: FnOnce() -> T>(call: F) -> T {
    // Note: This struct prevents a memory leak if `call` panics.
    struct DrainPool {
        ctx: *mut c_void,
    }

    impl Drop for DrainPool {
        fn drop(&mut self) {
            // SAFETY: `pool_ctx` has not been manipulated since it was received from the corresponding pool push call.
            unsafe { ffi::objc_autoreleasePoolPop(self.ctx) }
        }
    }

    // SAFETY: Creating a new pool is safe in any context.
    let _pool_ctx = DrainPool {
        ctx: unsafe { ffi::objc_autoreleasePoolPush() },
    };
    call()
    // The pool is drained here before returning.
}