# \[Pitch\] Safe Access to Contiguous Storage

**URL:** <https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888>\
**Category:** Pitches\
**Created:** [February 6, 2024, 11:03pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888 "2024-02-06T23:03:46Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [February 13, 2024, 3:43am UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/21 "2024-02-13T03:43:18Z")

</div>

> [@Jumhyn](#):
>
> feels quite wordy to me

still better than the full name of, say, UMRBP.

---

<div class="post-metadata">

**Author:** ![lorentey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorentey/32/1299_2.png) [@lorentey](https://forums.swift.org/u/lorentey)\
**Post date:** [February 13, 2024, 3:57am UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/22 "2024-02-13T03:57:48Z")

</div>

Very nice!

Minor notes follow below.

- In the `ContiguousStorage` protocol, I recommend dropping the `View` suffix from the name of the `storageView` property:

- I suggest moving `StorageViewIndex` to a nested type, `StorageView.Index`. Given that the index is also generic over the same `Element`, it seems preferable to have only one way to spell it. Nesting the type under `StorageView` will also help keep the top-level namespace clean of auxiliary constructs.

- Similarly, `StorageViewIterator` ought to be defined directly as `StorageView.Iterator`. (But see below.)

- The attributes of borrowing iterators are not defined yet; however, I think it is unlikely we'd want to reuse the name `Iterator` for borrowing iteration, as it would lead to clashes. (The existing `Iterator` name in `Sequence` is semantically a consuming iterator, which cannot be implemented by this construct.) Additionally, I suspect we may not want the iterator to be (implicitly) copyable.

- The `MyResilientType` example uses a `withStorageView` method that does not exist. More importantly, it may be a good idea to recommend making the core interface public (as opposed to `@usableFromInline`):

- The `withUnsafeBufferPointer ` escape hatch that gets scoped access to an UBP has far more utility than simply enabling C interop -- I think it should be fully embraced as primary API. (It pairs with and goes hand in hand with the unsafe initializers.)

- We'll probably want `StorageView` to implement this missing method from the existing Collection protocol:

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [February 14, 2024, 5:39pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/23 "2024-02-14T17:39:56Z")

</div>

> [@Jumhyn](#):
>
> I feel like `UnsafeBufferPointer` provides good precedent for 'buffer' as a simpler way to refer to this

The reason I suggested "Array" was actually to avoid linking those types 😅

I don't terribly mind it, but we should consider carefully whether we want the names to suggest a relationship to unsafe buffers, or whether we would rather encourage a conceptual relationship to Array.

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [February 14, 2024, 5:56pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/24 "2024-02-14T17:56:41Z")

</div>

> [@Karl](#):
>
> I don't terribly mind it, but we should consider carefully whether we want the names to suggest a relationship to unsafe buffers, or whether we would rather encourage a conceptual relationship to Array.

This is effectively the safe replacement for (most uses of) UnsafeBufferPointer, so the name seems appropriate to me.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [February 14, 2024, 6:05pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/25 "2024-02-14T18:05:26Z")

</div>

Yeah I'm not suggesting that it's _inappropriate_, I'm suggesting that there are multiple suitable candidates.

For instance, what is an Array in Swift? It's a safe, owned buffer with value semantics. This would be a safe, borrowed buffer (and I believe the intention is that we use exclusivity to suppress the spooky-modifications-at-a-distance which are the hallmark of reference semantics). So `BorrowedArray` also wouldn't be inappropriate IMO.

The thread about enabling bounds-checking in UBP has shown that some people habitually drop the "unsafe" prefix when discussing that type (and just call it a "buffer pointer"), leading to _severe_ misunderstandings about how that type behaves. That's why I'm apprehensive about calling anything else a "buffer".

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [February 14, 2024, 6:33pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/26 "2024-02-14T18:33:33Z")

</div>

For me, using `Array` in the type name would hit the ear wrong for a couple reasons:

- If I'm getting it from something that isn't explicitly an `Array`, then invoking the term "array" feels misleading. Today, there are places in Swift where—because something takes or returns an `Array` specifically—you end up making copies of things to fit it into a certain API. If I go from a `String.UTF8View` to a `BorrowedArray`, a reader might think "why is an 'array' involved here; did it somewhere make a copy of the `UTF8View` into an `Array` and I'm borrowing _that?_" On the other hand, `BorrowedBuffer/StorageView/BufferView` all feel more clearly like they're providing a view over that underlying storage directly.

- If I'm working specifically with `Array` types, then `BorrowedArray` feels sort of too similar to `ArraySlice`, which I think could also lead to confusion about which one to use in each circumstance.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [February 14, 2024, 6:43pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/27 "2024-02-14T18:43:27Z")

</div>

> [@Jumhyn](#):
>
> So `BorrowedBuffer` SGTM.

+1, that leads my mind in the right direction conceptually too.

A separate reflection is that perhaps the swift programming language book would need to get appendixes over time on concepts such as memory ownership etc more in depth, there seems to be a little tension now between keeping approachable to new users while covering “everything” - looking at memory ownership, concurrency details, macros and more - I often end up with a hunt across various sources now (pitch threads, actual proposals, discussion about the proposals, etc - as there often are critical nuggets sprinkled around…). Looking forward to when the LLM:s catch up with the latest discussions…. 🙂

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [February 14, 2024, 6:47pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/28 "2024-02-14T18:47:09Z")

</div>

Related to this point, it's worth considering whether we might want "borrowed" versions of other standard library types.

For instance, a [recent pitch](https://forums.swift.org/t/pitch-unicode-processing-apis/69294) suggested that we might add a type called `Unicode.UTF8.ValidBufferView` for text processing over non-owned storage. What if we instead called that a `BorrowedString`?

There would be nice some symmetry to having both `BorrowedArray` and `BorrowedString`, and I think having that as a more established pattern could minimise some of your concerns.

Anyway, I don't want to labour the point too much because my actual opinion is that `BorrowedBuffer` is also fine. But it's worth thinking about these things.

---

<div class="post-metadata">

**Author:** ![nixberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nixberg/32/7961_2.png) [@nixberg](https://forums.swift.org/u/nixberg)\
**Post date:** [February 22, 2024, 4:40pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/29 "2024-02-22T16:40:34Z")

</div>

```swift
protocol ContiguousStorage<Element>: ~Escapable

```

So a noncopyable type could not conform to `ContiguousStorage`? Is this a necessary restriction?

Also, this pitch and its [sister proposal](https://forums.swift.org/t/pitch-non-escapable-types-and-lifetime-dependency/69865) seem to imply a [lifting of restrictions around generics and suppressed constraints](https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md#generics-support-for-noncopyable-types).

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [February 22, 2024, 5:25pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/30 "2024-02-22T17:25:39Z")

</div>

> [@nixberg](#):
>
> Also, this pitch and its [sister proposal](https://forums.swift.org/t/pitch-non-escapable-types-and-lifetime-dependency/69865) seem to imply a [lifting of restrictions around generics and suppressed constraints](https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md#generics-support-for-noncopyable-types).

Yes, there's a forthcoming proposal that covers that.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [February 22, 2024, 5:31pm UTC](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888/31 "2024-02-22T17:31:35Z")

</div>

> [@nixberg](#):
>
> So a noncopyable type could not conform to `ContiguousStorage`? Is this a necessary restriction?

No, this should also allow noncopyable types. I forgot to include the annotation in the pitch. Thanks for pointing it out!

[Previous page](https://forums.swift.org/t/pitch-safe-access-to-contiguous-storage/69888.md?page=1)
