# \[Proposal\] Change (Dispatch)Data.withUnsafeBytes to use UnsafeMutableBufferPointer

**URL:** <https://forums.swift.org/t/proposal-change-dispatch-data-withunsafebytes-to-use-unsafemutablebufferpointer/4778>\
**Category:** Discussion\
**Created:** [December 27, 2016, 8:16am UTC](https://forums.swift.org/t/proposal-change-dispatch-data-withunsafebytes-to-use-unsafemutablebufferpointer/4778 "2016-12-27T08:16:39Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [December 27, 2016, 8:16am UTC](https://forums.swift.org/t/proposal-change-dispatch-data-withunsafebytes-to-use-unsafemutablebufferpointer/4778/1 "2016-12-27T08:16:39Z")

</div>

Looking for feedback before submitting a PR: [https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md)

> **···**
>
> —
> 
> Change (Dispatch)Data.withUnsafeBytes to use UnsafeMutableBufferPointer
> 
> Proposal: SE-NNNN \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt);  
> Authors: Karl Wagner \<[https://github.com/karwa&gt](https://github.com/karwa&gt);  
> Review Manager: TBD  
> Status: Awaiting review  
> During the review process, add the following fields as needed:
> 
> Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> Previous Proposal: SE-XXXX \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt);  
> \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt;Introduction](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt;Introduction)
> 
> The standard library's Array and ContiguousArray types expose the method withUnsafeBytes, which allows you to view their contents as a contiguous collection of bytes. The core libraries Foundation and Dispatch contain types which wrap some allocated data, but their withUnsafeBytes method only allows you to view the contents as a pointer to a contiguous memory location of a given type.
> 
> Swift-evolution thread: Discussion thread topic for that proposal \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt;Motivation](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt;Motivation)
> 
> The current situation makes it awkward to write generic code. Personally, I use the following extension in my projects to sort the naming confusion out:
> 
> protocol ContiguousByteCollection {  
> &nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T  
> }
> 
> // stdlib types are fine.  
> extension Array: ContiguousByteCollection {}  
> extension ArraySlice: ContiguousByteCollection {}  
> extension ContiguousArray: ContiguousByteCollection {}
> 
> // corelibs types give us a pointer\<T\>, should be: { pointer\<char\>, count }  
> #if canImport(Dispatch)  
> &nbsp;&nbsp;import Dispatch
> 
> &nbsp;&nbsp;extension DispatchData : ContiguousByteCollection {  
> &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;}  
> #endif
> 
> #if canImport(Foundation)  
> &nbsp;&nbsp;import Foundation
> 
> &nbsp;&nbsp;extension Data : ContiguousByteCollection {  
> &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;}  
> #endif  
> Conceptually, the corelibs types are untyped regions of memory, and it would make sense for them to adopt the UnsafeRawBufferPointer model.
> 
> \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed) solution
> 
> The proposed solution would be to deprecate the current methods on (Dispatch)Data (with 2 generic parameters), and replace them with methods with identical signatures to Array (with 1 generic parameter).
> 
> To be deprecated:
> 
> public func withUnsafeBytes\<ResultType, ContentType\>(\_ body: (UnsafePointer\<ContentType\>) throws -\> ResultType) rethrows -\> ResultType  
> Replaced with:
> 
> public func withUnsafeBytes\<R\>(\_ body: (UnsafeRawBufferPointer) throws -\> R) rethrows -\> R  
> \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt;Source](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt;Source) compatibility
> 
> Source-breaking. Users binding a (Dispatch)Data to an UnsafePointer would instead have to call:
> 
> buffer.baseAddress!.assumingMemoryBound(to: T.self)  
> Which is a bit more to type, although maybe the deprecation of the old function could provide this replacement as a fix-it.
> 
> \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#effect-on-api-resilience&gt;Effect](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#effect-on-api-resilience&gt;Effect) on API resilience
> 
> Source-breaking change to corelibs APIs.
> 
> \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#alternatives-considered&gt;Alternatives](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#alternatives-considered&gt;Alternatives) considered
> 
> A different method on Data and DispatchData, providing an UnsafeRawBufferPointer? There would still be a naming discrepency between the stdlib and corelibs types

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 27, 2016, 4:57pm UTC](https://forums.swift.org/t/proposal-change-dispatch-data-withunsafebytes-to-use-unsafemutablebufferpointer/4778/2 "2016-12-27T16:57:57Z")

</div>

+1, I have run into this awkwardness myself and wished the API were  
different, exactly as you outline.

> **···**
>
> On Tue, Dec 27, 2016 at 03:16 Karl via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > Looking for feedback before submitting a PR:  
> > [https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md)
> > 
> > —
> > 
> > Change (Dispatch)Data.withUnsafeBytes to use UnsafeMutableBufferPointer
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: SE-NNNN  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;- Authors: Karl Wagner \<[https://github.com/karwa&gt](https://github.com/karwa&gt);  
> > &nbsp;&nbsp;&nbsp;- Review Manager: TBD  
> > &nbsp;&nbsp;&nbsp;- Status: Awaiting review
> > 
> > \*During the review process, add the following fields as needed:\*
> > 
> > &nbsp;&nbsp;&nbsp;- Decision Notes: Rationale  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional  
> > &nbsp;&nbsp;&nbsp;Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > &nbsp;&nbsp;&nbsp;- Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM  
> > &nbsp;&nbsp;&nbsp;\<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > &nbsp;&nbsp;&nbsp;- Previous Revision: 1  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;- Previous Proposal: SE-XXXX  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt);
> > 
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt);  
> > Introduction
> > 
> > The standard library's Array and ContiguousArray types expose the method  
> > withUnsafeBytes, which allows you to view their contents as a contiguous  
> > collection of bytes. The core libraries Foundation and Dispatch contain  
> > types which wrap some allocated data, but their withUnsafeBytes method  
> > only allows you to view the contents as a pointer to a contiguous memory  
> > location of a given type.
> > 
> > Swift-evolution thread: Discussion thread topic for that proposal  
> > \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);
> > 
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt);  
> > Motivation
> > 
> > The current situation makes it awkward to write generic code. Personally,  
> > I use the following extension in my projects to sort the naming confusion  
> > out:
> > 
> > protocol ContiguousByteCollection {  
> > &nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T  
> > }  
> > // stdlib types are fine.extension Array: ContiguousByteCollection {}extension ArraySlice: ContiguousByteCollection {}extension ContiguousArray: ContiguousByteCollection {}  
> > // corelibs types give us a pointer\<T\>, should be: { pointer\<char\>, count }#if canImport(Dispatch)  
> > &nbsp;&nbsp;import Dispatch
> > 
> > &nbsp;&nbsp;extension DispatchData : ContiguousByteCollection {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;}  
> > #endif
> > 
> > #if canImport(Foundation)  
> > &nbsp;&nbsp;import Foundation
> > 
> > &nbsp;&nbsp;extension Data : ContiguousByteCollection {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;}  
> > #endif
> > 
> > Conceptually, the corelibs types \*are\* untyped regions of memory, and it  
> > would make sense for them to adopt the UnsafeRawBufferPointer model.
> > 
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed)  
> > solution
> > 
> > The proposed solution would be to deprecate the current methods on  
> > (Dispatch)Data (with 2 generic parameters), and replace them with methods  
> > with identical signatures to Array (with 1 generic parameter).
> > 
> > To be deprecated:
> > 
> > public func withUnsafeBytes\<ResultType, ContentType\>(\_ body: (UnsafePointer\<ContentType\>) throws -\> ResultType) rethrows -\> ResultType
> > 
> > Replaced with:
> > 
> > public func withUnsafeBytes\<R\>(\_ body: (UnsafeRawBufferPointer) throws -\> R) rethrows -\> R
> > 
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt;Source](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt;Source)  
> > compatibility
> > 
> > Source-breaking. Users binding a (Dispatch)Data to an UnsafePointer would  
> > instead have to call:
> > 
> > buffer.baseAddress!.assumingMemoryBound(to: T.self)
> > 
> > Which is a bit more to type, although maybe the deprecation of the old  
> > function could provide this replacement as a fix-it.
> > 
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#effect-on-api-resilience&gt;Effect](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#effect-on-api-resilience&gt;Effect)  
> > on API resilience
> > 
> > Source-breaking change to corelibs APIs.
> > 
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#alternatives-considered&gt;Alternatives](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#alternatives-considered&gt;Alternatives)  
> > considered
> > 
> > &nbsp;&nbsp;&nbsp;- A different method on Data and DispatchData, providing an  
> > &nbsp;&nbsp;&nbsp;UnsafeRawBufferPointer? There would still be a naming discrepency  
> > &nbsp;&nbsp;&nbsp;between the stdlib and corelibs types
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![gmosx](https://avatars.discourse-cdn.com/v4/letter/g/fbc32d/32.png) [@gmosx](https://forums.swift.org/u/gmosx)\
**Post date:** [December 28, 2016, 9:54am UTC](https://forums.swift.org/t/proposal-change-dispatch-data-withunsafebytes-to-use-unsafemutablebufferpointer/4778/3 "2016-12-28T09:54:31Z")

</div>

+1

Always in favour of consistency/uniformity (even if source-breaking changes are needed)

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [January 4, 2017, 10:12pm UTC](https://forums.swift.org/t/proposal-change-dispatch-data-withunsafebytes-to-use-unsafemutablebufferpointer/4778/4 "2017-01-04T22:12:46Z")

</div>

Thanks Karl. Good observation.

I proposed exactly this API along with a few other UnsafeRawBufferPointer compatibility API’s during SE-0183. Look for Tony to follow up on this.

-Andy

> **···**
>
> > On Dec 27, 2016, at 12:16 AM, Karl via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Looking for feedback before submitting a PR: [https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md)
> > 
> > —
> > 
> > Change (Dispatch)Data.withUnsafeBytes to use UnsafeMutableBufferPointer
> > 
> > Proposal: SE-NNNN \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt);  
> > Authors: Karl Wagner \<[https://github.com/karwa&gt](https://github.com/karwa&gt);  
> > Review Manager: TBD  
> > Status: Awaiting review  
> > During the review process, add the following fields as needed:
> > 
> > Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > Previous Proposal: SE-XXXX \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt);  
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt;Introduction](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt;Introduction)
> > 
> > The standard library's Array and ContiguousArray types expose the method withUnsafeBytes, which allows you to view their contents as a contiguous collection of bytes. The core libraries Foundation and Dispatch contain types which wrap some allocated data, but their withUnsafeBytes method only allows you to view the contents as a pointer to a contiguous memory location of a given type.
> > 
> > Swift-evolution thread: Discussion thread topic for that proposal \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt;Motivation](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt;Motivation)
> > 
> > The current situation makes it awkward to write generic code. Personally, I use the following extension in my projects to sort the naming confusion out:
> > 
> > protocol ContiguousByteCollection {  
> > &nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T  
> > }
> > 
> > // stdlib types are fine.  
> > extension Array: ContiguousByteCollection {}  
> > extension ArraySlice: ContiguousByteCollection {}  
> > extension ContiguousArray: ContiguousByteCollection {}
> > 
> > // corelibs types give us a pointer\<T\>, should be: { pointer\<char\>, count }  
> > #if canImport(Dispatch)  
> > &nbsp;&nbsp;import Dispatch
> > 
> > &nbsp;&nbsp;extension DispatchData : ContiguousByteCollection {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;}  
> > #endif
> > 
> > #if canImport(Foundation)  
> > &nbsp;&nbsp;import Foundation
> > 
> > &nbsp;&nbsp;extension Data : ContiguousByteCollection {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;}  
> > #endif  
> > Conceptually, the corelibs types are untyped regions of memory, and it would make sense for them to adopt the UnsafeRawBufferPointer model.
> > 
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed) solution
> > 
> > The proposed solution would be to deprecate the current methods on (Dispatch)Data (with 2 generic parameters), and replace them with methods with identical signatures to Array (with 1 generic parameter).
> > 
> > To be deprecated:
> > 
> > public func withUnsafeBytes\<ResultType, ContentType\>(\_ body: (UnsafePointer\<ContentType\>) throws -\> ResultType) rethrows -\> ResultType  
> > Replaced with:
> > 
> > public func withUnsafeBytes\<R\>(\_ body: (UnsafeRawBufferPointer) throws -\> R) rethrows -\> R  
> > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt);

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [January 4, 2017, 10:21pm UTC](https://forums.swift.org/t/proposal-change-dispatch-data-withunsafebytes-to-use-unsafemutablebufferpointer/4778/5 "2017-01-04T22:21:39Z")

</div>

Hi Andy, everyone,

The short answer is that fixing struct Data requires a source breaking change (the enum for the deallocator has to change to use the raw buffer type), so I do not want to introduce it until Swift 4 provides a better transition path for this kind of change.

- Tony

> **···**
>
> > On Jan 4, 2017, at 2:12 PM, Andrew Trick \<atrick@apple.com\> wrote:
> > 
> > > On Dec 27, 2016, at 12:16 AM, Karl via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > Looking for feedback before submitting a PR: [https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md)
> > > 
> > > —
> > > 
> > > Change (Dispatch)Data.withUnsafeBytes to use UnsafeMutableBufferPointer
> > > 
> > > Proposal: SE-NNNN \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/NNNN-filename.md&gt);  
> > > Authors: Karl Wagner \<[https://github.com/karwa&gt](https://github.com/karwa&gt);  
> > > Review Manager: TBD  
> > > Status: Awaiting review  
> > > During the review process, add the following fields as needed:
> > > 
> > > Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > > Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > > Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > > Previous Proposal: SE-XXXX \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/XXXX-filename.md&gt);  
> > > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt;Introduction](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#introduction&gt;Introduction)
> > > 
> > > The standard library's Array and ContiguousArray types expose the method withUnsafeBytes, which allows you to view their contents as a contiguous collection of bytes. The core libraries Foundation and Dispatch contain types which wrap some allocated data, but their withUnsafeBytes method only allows you to view the contents as a pointer to a contiguous memory location of a given type.
> > > 
> > > Swift-evolution thread: Discussion thread topic for that proposal \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt;Motivation](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#motivation&gt;Motivation)
> > > 
> > > The current situation makes it awkward to write generic code. Personally, I use the following extension in my projects to sort the naming confusion out:
> > > 
> > > protocol ContiguousByteCollection {  
> > > &nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T  
> > > }
> > > 
> > > // stdlib types are fine.  
> > > extension Array: ContiguousByteCollection {}  
> > > extension ArraySlice: ContiguousByteCollection {}  
> > > extension ContiguousArray: ContiguousByteCollection {}
> > > 
> > > // corelibs types give us a pointer\<T\>, should be: { pointer\<char\>, count }  
> > > #if canImport(Dispatch)  
> > > &nbsp;&nbsp;import Dispatch
> > > 
> > > &nbsp;&nbsp;extension DispatchData : ContiguousByteCollection {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;}  
> > > #endif
> > > 
> > > #if canImport(Foundation)  
> > > &nbsp;&nbsp;import Foundation
> > > 
> > > &nbsp;&nbsp;extension Data : ContiguousByteCollection {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;func withUnsafeBytes\<T\>(\_ body: (UnsafeRawBufferPointer) throws -\> T) rethrows -\> T {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try withUnsafeBytes { try body(UnsafeRawBufferPointer(start: $0, count: count)) }  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;}  
> > > #endif  
> > > Conceptually, the corelibs types are untyped regions of memory, and it would make sense for them to adopt the UnsafeRawBufferPointer model.
> > > 
> > > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#proposed-solution&gt;Proposed) solution
> > > 
> > > The proposed solution would be to deprecate the current methods on (Dispatch)Data (with 2 generic parameters), and replace them with methods with identical signatures to Array (with 1 generic parameter).
> > > 
> > > To be deprecated:
> > > 
> > > public func withUnsafeBytes\<ResultType, ContentType\>(\_ body: (UnsafePointer\<ContentType\>) throws -\> ResultType) rethrows -\> ResultType  
> > > Replaced with:
> > > 
> > > public func withUnsafeBytes\<R\>(\_ body: (UnsafeRawBufferPointer) throws -\> R) rethrows -\> R  
> > > \<[https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt](https://github.com/karwa/swift-evolution/blob/corelibs-unsafebytes/proposals/xxxx-corelibs-unsafebytes.md#source-compatibility&gt);
> > 
> > Thanks Karl. Good observation.
> > 
> > I proposed exactly this API along with a few other UnsafeRawBufferPointer compatibility API’s during SE-0183. Look for Tony to follow up on this.
> > 
> > -Andy
