Last active
August 19, 2020 20:37
-
-
Save kastiglione/6e1531ebf5a61e47d8b8c98ed7642e59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given an imported C function, that uses a context pointer and a C callback function: | |
// | |
// func for_each(_ x: X, context: UnsafeMutableRawPointer!, callback: @convention(c) (UnsafeMutableRawPointer?, Element) -> Void) | |
// | |
// When is it safe to use this pattern? | |
// Example with two captured variables. | |
typealias Context = (Type1, Type2) | |
var context = (capture1, capture2) | |
for_each(x, context: &context) { context, element in | |
let (capture1, capture2) = context!.assumingMemoryBound(to: Context.self).pointee | |
// Perform operations with captures and `element`… | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment