I am interacting with a C API. For memory safety I chose to not keep the C types internally and just produce them when another C api needs them, so I can work with plain Swift structs instead. In this example I just use the Foo
type as a stand-in, this is obviously not the real type.
Now when I need to call a C API, I have those 2 methods on the type, to produce a C pointer I can work with:
struct Foo {
let bar: Int
}
extension Optional where Wrapped == Foo {
func withUnsafePointer<T>(_ block: (Foo?) throws -> T) rethrows -> T {
guard let self = self else {
return try block(nil)
}
return try block(self)
}
}
extension Foo {
func withUnsafePointer<T>(_ block: (Foo) throws -> T) rethrows -> T {
return try block(self)
}
}
As you can see I need to extend the optional as well to facilitate this. Otherwise a call to foo?.withUnsafePointer(...)
would just not be executed.
I don't find this pattern particularly pretty. Is there a better option instead of implementing the method twice?
The whole point of this is, that the C object should only have a limited lifetime (here inside the block).
You can avoid duplicating the withUnsafePointer
method by declaring a protocol, making both Foo
and Foo?
conform to it, and then extending the protocol. This isn't necessarily as pretty as something like extension Foo, Optional<Foo>
would be, but AFAIK there's not currently a way to do that in Swift as of the time of this writing.
struct Foo {
let myCPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
}
protocol HasMyCPointer {
associatedtype MyCPointerType
var myCPointer: MyCPointerType { get }
}
extension Foo: HasMyCPointer {}
extension Optional: HasMyCPointer where Wrapped == Foo {
var myCPointer: UnsafeMutablePointer<UInt8>? { self?.myCPointer }
}
extension HasMyCPointer {
func withUnsafePointer<T>(_ block: (Self.MyCPointerType) throws -> T) rethrows -> T {
return try block(self.myCPointer)
}
}
let foo = Foo()
let bar: Foo? = nil
let baz: Foo? = Foo()
foo.withUnsafePointer {
print("Foo: \($0)")
}
bar.withUnsafePointer {
print("Bar: \($0 as Any)")
}
baz.withUnsafePointer {
print("Baz: \($0 as Any)")
}
prints:
Foo: 0x00007fade3c05750
Bar: nil
Baz: Optional(0x00007fade3c00220)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.