简体   繁体   English

扩展可选和非可选类型

[英]Extend Optional and Non-Optional Type

I am interacting with a C API.我正在与 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. 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.在这个例子中我只是使用Foo类型作为替代,这显然不是真正的类型。

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:现在,当我需要调用 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)
    }
}

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.否则对foo?.withUnsafePointer(...)的调用将不会被执行。

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).重点是,C object 应该只有有限的生命周期(这里在块内)。

You can avoid duplicating the withUnsafePointer method by declaring a protocol, making both Foo and Foo?您可以通过声明协议来避免重复使用withUnsafePointer方法,同时使FooFoo? 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.这不一定像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)")
}

prints:印刷:

Foo: 0x00007fade3c05750
Bar: nil
Baz: Optional(0x00007fade3c00220)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM