apple-apis 0.1.0

System bindings for Apple APIs
public typealias Ptr = UnsafeMutableRawPointer

public protocol RawPtrExt: AnyObject {
	func unsafeIntoPtr() -> Ptr
	static func unsafeFromPtr(_ ptr: Ptr) -> Self
	static func unsafeDropPtr(_ ptr: Ptr)
}

public extension RawPtrExt {
	/// Equivalent in rust is leaking an Rc value and taking the pointer.
	func unsafeIntoPtr() -> Ptr {
		Unmanaged.passRetained(self).toOpaque()
	}

	/// Equivalent in rust is Rc::clone but with a raw pointer instead of another
	/// Rc. This function does not "take ownership" of the passed in pointer.
	static func unsafeFromPtr(_ ptr: Ptr) -> Self {
		Unmanaged<Self>.fromOpaque(ptr).takeUnretainedValue()
	}

	/// Equivalent in rust is creating an Rc from a raw pointer then dropping it,
	/// decrementing reference count by one
	static func unsafeDropPtr(_ ptr: Ptr) {
		Unmanaged<Self>.fromOpaque(ptr).release()
	}
}