<div dir="ltr"><span style="font-size:12.800000190734863px">Hi there,</span><br><div class="gmail_quote"><div dir="ltr"><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">I&#39;ve written up a <a href="https://github.com/Interfere/swift-evolution/blob/proposal/NNNN-managedbuffertuple/proposals/NNNN-managedbuffertuple.md" target="_blank">proposal</a> and would love to hear your feedback on it!</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">Best Regards,</div><div style="font-size:12.800000190734863px">Alex Komnin</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">P.S. Based on discussion in <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20161212/029415.html" target="_blank">https://lists.swift.org/pip<wbr>ermail/swift-evolution/Week-of<wbr>-Mon-20161212/029415.html</a></div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px"><h1 style="box-sizing:border-box;margin:0px 0px 16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;">Extend API for managing tail allocated buffers</h1><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><li style="box-sizing:border-box">Proposal: <a href="https://github.com/Interfere/swift-evolution/blob/proposal/NNNN-managedbuffertuple/proposals/NNNN-managedbuffertuple.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none">SE-NNNN</a></li><li style="box-sizing:border-box;margin-top:0.25em">Authors: <a href="https://github.com/Interfere" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none">Alexey Komnin</a></li><li style="box-sizing:border-box;margin-top:0.25em">Review Manager: TBD</li><li style="box-sizing:border-box;margin-top:0.25em">Status: <span style="box-sizing:border-box;font-weight:600">Awaiting review</span></li></ul><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-introduction" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#introduction" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Some Swift intrinsics (<code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_{n}</code>) are widely used by Swift standard library to implement tail-allocated buffers. As intrinsics are used only within standard library, they are not available for ordinary developers. Hence, there is no way to effectively implement custom containers like OrderedSet or LRU-cache.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">On the other hand, there is a suitable approach to create and manage single-area tail-allocated buffer. Swift provides a class named <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer</code>. Actually, class method <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer.create(minimumCapacity:makingHeaderWith:)</code> is just a wrapper over <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_1</code> builtin. You may use that class as a storage for elements of contiguous collection: Array or RingBuffer. Unfortunately, Swift doesn&#39;t provde wrappers for <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_2</code>, <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_3</code> etc. builtins.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">This proposal outlines a new API for managing multi-area tail-allocated buffers. It is supposed to extend current implementation of <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer</code> to support 2- and 3-areas tail-allocated buffers.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none">TBD</a></p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-motivation" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#motivation" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Collections in Swift are implemnted using tail-allocated buffers. A handful of intrinsics are available for developers of swift standard library to allocate and manage data stored in them. For example, the buffer used by Array is implemented with <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_1&lt;U&gt;</code> routine, which is able to allocate contiguous buffer for <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">N</code> elements of type <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">U</code>. <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">Set</code> is implemented with <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_3&lt;T, U, V&gt;</code> routine, which is able to allocate contiguous buffer consisted of three areas for elements of types <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">T</code>, <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">U</code> and <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">V</code>.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Application developers can&#39;t use these instrinsics. One is supposed to use <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer</code> instead, which is a simple wrapper over the single-area tail-allocated buffer. It uses <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_1&lt;U&gt;</code> to allocate storage for elements of type <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">U</code> the same way the Array container does. So it may be used to implement containers with contiguous storage: Array or Queue.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Yet there is no API to create or manage multi-area tail-allocated buffers in stdlib. Routines <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_{n}</code> are neither wrapped by <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer_{n}</code> classes nor exposed to be used by developers. Instead, developers are forced to use <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer</code> and do lots of pointer arithmetic.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-proposed-solution" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#proposed-solution" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Proposed solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">This proposal introduces a new class <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBufferTuple</code> and a list of traits for single and multiple areas cases. The traits are to keep all internal information about structure of the areas.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">These changes make it simple to implement multi-area buffers as simple as single-area. Example:</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">class</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">TwoAreasManagedBuffer</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">T</span>, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">U</span>&gt;: <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTuple&lt;CustomHeader, TwoAreasManagedBufferTrait&lt;T, U&gt;&gt;</span> {
    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">static</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">create</span>(<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)"><span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">minimumCapacity</span></span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> TwoAreasManagedBuffer&lt;T, U&gt; {
        <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> p <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">create</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">minimumCapacity1</span>: minimumCapacity, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">minimumCapacity2</span>: minimumCapacity) { buffer <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">in</span>
            <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">CustomHeader</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">capacity1</span>: buffer.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">capacity1</span>, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">capacity2</span>: buffer.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">capacity2</span>)
        }
        <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">unsafeDowncast</span>(p, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span>)
    }
}
</pre></div><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-detailed-design" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#detailed-design" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Detailed Design</h2><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-managedbuffertuple" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#managedbuffertuple" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>ManagedBufferTuple</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">The standard library introduces new class <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBufferTuple</code>. The base interface of the class is simple and minimalistic:</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">open</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">class</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTuple</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Header</span>, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Trait</span> : <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTrait</span>&gt; {
  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Call `body` with an `UnsafeMutablePointer` to the stored</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> `Header`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> - Note: This pointer is valid only for the duration of the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span>   call to `body`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">withUnsafeMutablePointerToHeader</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">R</span>&gt;(
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">body</span>: (<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Header&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> The stored `Header` instance.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> During instance creation, in particular during</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> `ManagedBufferTupleFactory.create`&#39;s call to initialize, </span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> `ManagedBufferTuple`&#39;s `header` property is as-yet uninitialized, </span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> and therefore reading the `header` property during `ManagedBufferTupleFactory.create` </span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> is undefined.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> header<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> Header
}
</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">The class is parametrized with a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">Header</code> and <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">Trait</code> types.</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-traits" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#traits" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Traits</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">The standard library introduces a handful of traits. Each trait consists of type constraint and corresponding implementation. Type constraint is used for extensions of <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBufferTuple</code>.</p><h4 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:16px;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-single-area-managed-buffer-trait" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#single-area-managed-buffer-trait" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Single-area managed buffer trait</h4><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> A trait for instances with a single storage for an array of `Element`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">protocol</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_SingleAreaManagedBufferTrait</span> : <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTrait </span>{
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">associatedtype</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element</span>
}

<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">struct</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">SingleAreaManagedBufferTrait</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">T</span>&gt; : <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">_SingleAreaManagedBufferTrait </span>{
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">typealias</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">Element</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> T
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Extension of <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBufferTuple</code> defines interface for single area buffer.</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTuple</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">where</span> Trait <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">_SingleAreaManagedBufferTrait</span> {
  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> The actual number of elements that can be stored in this object.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> This header may be nontrivial to compute; it is usually a good</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> idea to store this information in the &quot;header&quot; area when</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> an instance is created.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> capacity<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Call `body` with an `UnsafeMutablePointer` to the `Element`</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> storage.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> - Note: This pointer is valid only for the duration of the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span>   call to `body`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">withUnsafeMutablePointerToElements</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">R</span>&gt;(
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">body</span>: (<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Trait.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element</span>&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Call `body` with `UnsafeMutablePointer`s to the stored `Header`</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> and raw `Element` storage.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> - Note: These pointers are valid only for the duration of the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span>   call to `body`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">withUnsafeMutablePointers</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">R</span>&gt;(
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">body</span>: (<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Header&gt;, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Trait.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element</span>&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Method <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">create</code> is the only way to instantiate buffer and is a wrapper around <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_1</code> routine.</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTuple</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">where</span> Trait <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">_SingleAreaManagedBufferTrait</span> {
  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Create a new instance of ManagedBufferTuple with single area trait, </span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> calling `factory` on the partially-constructed object to generate an </span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> initial `Header`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">static</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">create</span>(
    <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)"><span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">minimumCapacity</span></span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>,
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">makingHeaderWith</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">factory</span>: (
      ManagedBufferTuple&lt;Header, Trait&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> Header
  ) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> ManagedBufferTuple&lt;Header, Trait&gt; {

    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> p <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> Builtin.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">allocWithTailElems_1</span>(
         <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span>,
         minimumCapacity.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">_builtinWordValue</span>, 
         <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element</span>.<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">self</span>)

    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> initHeaderVal <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">try</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">factory</span>(p)
    p.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">headerAddress</span>.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">initialize</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: initHeaderVal)
    <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> The _fixLifetime is not really needed, because p is used afterwards.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>    <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> But let&#39;s be conservative and fix the lifetime after we use the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>    <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> headerAddress.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>    <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">_fixLifetime</span>(p) 
    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> p
  }
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">The implementation for single area case follows the current implementation of <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer</code>. Hence, it would be possible to define it as</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">typealias</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBuffer</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Header</span>, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element</span>&gt; <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> ManagedBufferTuple&lt;Header, SingleAreaManagedBufferTrait&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element</span>&gt;&gt;</pre></div><h4 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:16px;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-two-areas-trait" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#two-areas-trait" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Two areas trait</h4><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> A trait for instances with a single storage for an array of `Element`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">protocol</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_TwoAreasManagedBufferTrait</span> : <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTrait </span>{
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">associatedtype</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element1</span>
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">associatedtype</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Element2</span>

  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> count1<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span> { <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">get</span> }
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">init</span>(<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)"><span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">count1</span></span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>)
}

<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">struct</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">TwoAreasManagedBufferTrait</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">T</span>, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">U</span>&gt; : <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">_TwoAreasManagedBufferTrait </span>{
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">typealias</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">Element1</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> T
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">typealias</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">Element2</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> U

  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> count1<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>
  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">init</span>(<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)"><span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">count1</span></span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>) {
    <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span>.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">count1</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> count1
  }
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Corresponding extension:</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTuple</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">where</span> Trait <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">_TwoAreasManagedBufferTrait</span> {

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> The actual number of elements that can be stored in the first buffer.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> capacity1<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> The actual number of elements that can be stored in the second buffer.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> capacity2<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Call `body` with an `UnsafeMutablePointer` to the `Element1`</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> storage.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> - Note: This pointer is valid only for the duration of the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span>   call to `body`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">withUnsafeMutablePointerToFirstBuffer</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">R</span>&gt;(
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">body</span>: (<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Trait.Element1&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Call `body` with an `UnsafeMutablePointer` to the `Element2`</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> storage.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> - Note: This pointer is valid only for the duration of the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span>   call to `body`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">withUnsafeMutablePointerToSecondBuffer</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">R</span>&gt;(
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">body</span>: (<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Trait.Element2&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R

  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Call `body` with `UnsafeMutablePointer`s to the stored `Header`</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> and raw `Element1` and `Element2` storages.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">///</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> - Note: These pointers are valid only for the duration of the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span>   call to `body`.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">final</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">withUnsafeMutablePointers</span>&lt;<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">R</span>&gt;(
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">body</span>: (<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Header&gt;, 
             <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Trait.Element1&gt;, 
             <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">UnsafeMutablePointer</span>&lt;Trait.Element2&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R
  ) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> R
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">And corresponding create method with ability to define minimumCapacity for each area:</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">ManagedBufferTuple</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">where</span> Trait <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)">_TwoAreasManagedBufferTrait</span> {
  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> Create a new instance of ManagedBufferTuple with Pair trait, calling</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> `factory` on the partially-constructed object to generate an initial</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">///</span> `Header`</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>  <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">static</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">create</span>(
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">minimumCapacity1</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">capacity1</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>,
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">minimumCapacity2</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">capacity2</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>,
    <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">makingHeaderWith</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">factory</span>: (
      ManagedBufferTuple&lt;Header, Trait&gt;) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">throws</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> Header
  ) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">rethrows</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> ManagedBufferTuple&lt;Header, Trait&gt; {

    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> p <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> Builtin.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">allocWithTailElems_2</span>(
         <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span>,
         capacity1.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">_builtinWordValue</span>, Element1.<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">self</span>,
         capacity2.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">_builtinWordValue</span>, Element2.<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">self</span>)

    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> trait <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Trait</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">count1</span>: capacity1)
    p.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">traitAddress</span>.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">initialize</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: trait)
    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> initHeaderVal <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">try</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">factory</span>(p)
    p.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">headerAddress</span>.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">initialize</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: initHeaderVal)
    <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> The _fixLifetime is not really needed, because p is used afterwards.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>    <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> But let&#39;s be conservative and fix the lifetime after we use the</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>    <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> headerAddress.</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>    <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">_fixLifetime</span>(p) 
    <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> p
  }
}</pre></div><h4 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:16px;line-height:1.25;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-three-areas-trait" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#three-areas-trait" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Three areas trait</h4><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">All multiple areas traits and extensions are similar. In three areas case create method is a wrapper around <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_3</code> routine. Source code is omitted.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Using traits is a flexible approach. It provides a convenient way of extending tail-allocated buffers with a minimal impact on existing code. In future, if there is a need, it would be easy to add more traits to support 4-, 5-areas.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Moreover, user is free to implement its own traits and extensions for any specific case.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">Proof of concept is available at <a href="https://github.com/Interfere/ManagedBufferTuple/blob/master/ManagedBufferTuple.swift" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none">ManagedBufferTuple.swift</a></p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-source-compatibility" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#source-compatibility" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Source compatibility</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">It&#39;s an additive feature. There is no impact on existing code. No changes in syntax or behavior.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-effect-on-api-resilience" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#effect-on-api-resilience" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Effect on API resilience</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px">The feature can be added without breaking ABI.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a id="gmail-user-content-alternatives-considered" class="gmail-anchor" href="https://github.com/Interfere/swift-evolution/tree/proposal/NNNN-managedbuffertuple#alternatives-considered" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Alternatives considered</h2><ol style="box-sizing:border-box;padding-left:2em;margin-top:0px;color:rgb(36,41,46);font-family:-apple-system,blinkmacsystemfont,&#39;segoe ui&#39;,helvetica,arial,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;margin-bottom:0px"><li style="box-sizing:border-box">Add <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer_2</code> and <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">ManagedBuffer_3</code> classes</li><li style="box-sizing:border-box;margin-top:0.25em">Make <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">allocWithTailElems_{n}</code> routines available for all developers</li></ol></div></div></div></div>