[英]Extend Optional and Non-Optional Type
我正在與 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. 在這個例子中我只是使用Foo
類型作為替代,這顯然不是真正的類型。
現在,當我需要調用 C API 時,我在類型上有這兩種方法,以生成 C 指針,我可以使用:
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)
}
}
如您所見,我還需要擴展可選項以促進這一點。 否則對foo?.withUnsafePointer(...)
的調用將不會被執行。
我不覺得這種模式特別漂亮。 有沒有更好的選擇而不是兩次實施該方法?
重點是,C object 應該只有有限的生命周期(這里在塊內)。
您可以通過聲明協議來避免重復使用withUnsafePointer
方法,同時使Foo
和Foo?
符合它,然后擴展協議。 這不一定像extension Foo, Optional<Foo>
那樣漂亮,但是在撰寫本文時,AFAIK 目前還沒有辦法在 Swift 中做到這一點。
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)")
}
印刷:
Foo: 0x00007fade3c05750
Bar: nil
Baz: Optional(0x00007fade3c00220)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.