扩展为通用类型`不安全可变指针<UInt8>` [英] Extension for Generic Type `UnsafeMutablePointer&lt;UInt8&gt;`

查看:219
本文介绍了扩展为通用类型`不安全可变指针<UInt8>`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 UnsafeMutablePointer 创建一个扩展,它只影响 UnsafeMutablePointer< UInt8> ...



我明白这些说明是相关的,但我不确定如何:


扩展泛型时,不提供类型参数列表作为扩展定义的一部分。相反,原始类型定义中的类型参数列表在扩展的主体中可用,原始类型参数名称用于引用原始定义中的类型参数。




基本上,我试图使用这种方法:

  func toSwift长度:Int) - > [Int] {
var retVal:[Int] = []
for i in 0 ..< length {
retVal.append(Int(self [i]))

return retVal
}

self 没有 UnsafeMutablePointer< UInt8> 作为参数...这是可能的吗?



从Swift 3.1开始(随Xcode 8.3 beta一起提供),现在具体的相同类型需求支持的。你现在可以说:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' - > [Int] {
return UnsafeBufferPointer(start:self,count:length).map(Int.init)
}
}






Pre Swift 3.1



You can 这样做 - 尽管它不是特别好。您必须创建一个新协议才能'标记' UInt8 类型,然后将您的扩展限制为该协议。它也不允许您轻松指定 Int(...)初始化程序可以使用 _UInt8Type 输入 - 你必须实现一个hacky'阴影'方法来做到这一点。

  protocol _UInt8Type {
func _asInt() - > Int
}
扩展UInt8:_UInt8Type {
func _asInt() - > Int {
return Int(self)
}
}

//将'Pointee'更改为'Memory'for Swift 2
extension UnsafeMutablePointer where Pointee :_UInt8Type {
func asArray(withLength length:Int) - > [Int] {
return UnsafeBufferPointer(start:self,count:length).map {$ 0._asInt()}
}
}

总而言之,我倾向于保持这种完全通用的方式,并使用 @ AMomchilov的解决方案



尽管值得注意的是,对于扩展名( extension type)具有相同类型的要求其中Generic == SomeType )已被提出作为Swift Generics Manifesto的一部分 - 所以希望这可以在未来的Swift版本中实现。


I'd like to create an extension for UnsafeMutablePointer that only affects UnsafeMutablePointer<UInt8>...

I understand that these instructions are pertinent, but I'm not sure how:

When you extend a generic type, you do not provide a type parameter list as part of the extension’s definition. Instead, the type parameter list from the original type definition is available within the body of the extension, and the original type parameter names are used to refer to the type parameters from the original definition.

Basically, I'm trying to use this method:

func toSwift(length: Int) -> [Int] {
    var retVal : [Int] = []
    for i in 0..<length {
        retVal.append(Int(self[i]))
    }
    return retVal
}

to act on self without the UnsafeMutablePointer<UInt8> as a parameter... is this possible?

解决方案

Swift 3.1 Update

As of Swift 3.1 (available with Xcode 8.3 beta), concrete same-type requirements are now supported. You can now just say:

extension UnsafeMutablePointer where Pointee == UInt8 {
    func asArray(withLength length: Int) -> [Int] {
        return UnsafeBufferPointer(start: self, count: length).map(Int.init)
    }
}


Pre Swift 3.1

You can do this – although it's not particularly nice. You'll have to create a new protocol in order to 'tag' the UInt8 type, and then constrain your extension to that protocol. It also doesn't allow you to easily specify that the Int(...) initialiser can take a _UInt8Type input – you have to implement a hacky 'shadow' method to do that.

protocol _UInt8Type {
    func _asInt() -> Int
}
extension UInt8 : _UInt8Type {
    func _asInt() -> Int {
        return Int(self)
    }
}

// Change 'Pointee' to 'Memory' for Swift 2
extension UnsafeMutablePointer where Pointee : _UInt8Type {
    func asArray(withLength length:Int) -> [Int] {
        return UnsafeBufferPointer(start: self, count: length).map{$0._asInt()}
    }
}

All in all I'd prefer to keep this fully generic and go with @AMomchilov's solution. I'm only really adding this for the sake of completion.

Although it's worth noting that having concrete same-type requirements for extensions (extension Type where Generic == SomeType) has been proposed as a part of the Swift Generics Manifesto – so hopefully this will be possible in a future version of Swift.

这篇关于扩展为通用类型`不安全可变指针<UInt8>`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆