[swift-evolution] [Pitch] Normalize Slice Types for Unsafe Buffers

Ben Cohen ben_cohen at apple.com
Thu Dec 8 22:08:52 CST 2016


Probably needs an argument label since it's performing an explicit purpose, not just a vanilla conversion initializer. So maybe UnsafeRawBufferPointer.init(rebasing:)

Or since we have same-type constrained extensions now on master maybe you could do it as a property: 

extension RandomAccessSlice where Base == UnsafeRawBufferPointer {
    var rebased: UnsafeRawBufferPointer {
        return UnsafeRawBufferPointer(start: base.baseAddress, count: count)
    }
}

(written by hand without a compiler so unlikely to be correct :)

> On Dec 8, 2016, at 18:07, Andrew Trick <atrick at apple.com> wrote:
> 
> 
>> On Dec 8, 2016, at 5:44 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>>> On Thu, Dec 8, 2016 at 6:53 PM, Ben Cohen via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>>> On Dec 8, 2016, at 4:35 PM, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
>>>> 
>>>> Um, Sequence doesn’t have a subscript (or indexes). Sequences are single-pass. So if this is important, it needs to stay a Collection.
>>> 
>>> Just because something fulfills one of the requirements of a Collection does not mean it should be one. It needs to tick all the boxes before its allowed to be elevated.
>>> 
>>> But it’s still allowed to have subscripts (UnsafePointer has subscripting but isn’t a collection) or be multi-pass (strides are multiples but are only sequences). That’s OK
>>> 
>>> In this case, yes it’s multi-pass, yes it has a subscript, but no it isn’t a collection because it doesn’t meet the requirements for slicing i.e. that indices of the slice be indices of the parent.
>>> (relatedly… it appears this requirement is documented on the concrete Slice type rather than on Collection… which is a documentation bug we should fix).
>> 
>> If this is indeed a requirement for Collection, then my vote would be for Nate's option #1 and Andy's option #2, to give UnsafeRawBufferPointer a Slice type that fulfills the requirement. It's the smallest change, preserves the use of integer indices, and preserves what Andy stated as the desired use case of making it easy for users to switch out code written for [UInt8].
> 
> Ok, but there needs to be an easy way in a nongeneric context to convert from a Slice<URBP> into an URBP (with normalized byte offsets).
> 
> Does anyone object to adding an initializer for this? Any suggestions on naming? Do we need an argument label? etc?
> 
> UnsafeRawBufferPointer(_ : Slice<UnsafeRawBufferPointer>)
> 
> as in:
> 
> let region = UnsafeRawBufferPointer(buffer[i..<j])
> 
> -Andy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161208/8012385a/attachment.html>


More information about the swift-evolution mailing list