So now that I look at it, it appears UnsafeRawBufferPointer(copyBytes:) has the same problems we are trying to solve on initialize(as:from) i.e. it is completely at the mercy of the passed-in collection's count being accurate, and if it isn't it'll scribble over memory. We should probably apply similar fixes and change it to take a sequence.
I realize it's a sticking plaster on this particular issue though, so still doesn't answer whether it's better for UnsafeRawBufferPointer to be a collection, just created more work... > On Dec 8, 2016, at 17:06, Andrew Trick <atr...@apple.com> wrote: > > >>> On Dec 8, 2016, at 4:54 PM, Jordan Rose <jordan_r...@apple.com> wrote: >>> >>> >>>> On Dec 8, 2016, at 16:53, Ben Cohen <ben_co...@apple.com> wrote: >>>> >>>> >>>> On Dec 8, 2016, at 4:35 PM, Jordan Rose via swift-evolution >>>> <swift-evolution@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). >> >> Ah, right, thank you. Retracted. > > Let me restate, because I think Jordan's question was valid given my > statement. > > It would be *nice* for raw buffers to be Collection<UInt8> because they’re > meant to be a replacement for code that is typically written for [UInt8], and > anything you can do with an array that applies to raw buffers is covered by > Collection<UInt8>. > > However, I don’t expect the raw buffer to be used in a generic context except > being passed to utilities that copy the bytes out. That will either be done > by directly iterating over the collection or invoking some other API that > could take a Sequence. The most important is probably > Array.append(contentsOf:), which is moving over to Sequence. However, we > would also need to change UnsafeRawBufferPointer(copyBytes:), > NSData(replaceSubrange:), and whatever else I haven't thought of. That's a > small disadvantage to this solution. > > I'm also a little concerned that Sequence is immutable, so generic code has > no way to copy bytes into the buffer. > > My bigger concern is still that the range subscript’s inconsistent behavior > may still lead to bugs in practice in nongeneric code. > > -Andy
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution