<div dir="ltr">I’ve asked similar questions in the past and everyone said this is not valid Swift, not the least because the compiler can pad structs and rearrange its layout. For example if there were only 3 UInt8s, the stride should be 15, not 16 as they need to be packed densely. since they are misaligned you also can’t use load and store (they will trap)<br><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 10, 2018 at 4:27 PM, Jens Persson <span dir="ltr"><<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I'm not sure what you mean by "I need the buffer to be a vector /.../" but perhaps this may be of some help:<div><br></div><div><div>struct S {</div><div> var x: Float</div><div> var y: Float</div><div> var z: Float</div><div> var r: UInt8</div><div> var g: UInt8</div><div> var b: UInt8</div><div> var a: UInt8</div><div>}</div><div>var buf = S(x: 0.1, y: 1.2, z: 2.3, r: 11, g: 22, b: 33, a: 44)</div><div>print(MemoryLayout<S>.stride) // 16</div><div>withUnsafeMutableBytes(of: &buf) { ptr in</div><div> print("x:", ptr.load(fromByteOffset: 0, as: Float.self)) // 0.1</div><div> print("y:", ptr.load(fromByteOffset: 4, as: Float.self)) // 1.2</div><div> print("z:", ptr.load(fromByteOffset: 8, as: Float.self)) // 2.3</div><div> print("r:", ptr.load(fromByteOffset: 12, as: UInt8.self)) // 11</div><div> print("g:", ptr.load(fromByteOffset: 13, as: UInt8.self)) // 22</div><div> print("b:", ptr.load(fromByteOffset: 14, as: UInt8.self)) // 33</div><div> print("a:", ptr.load(fromByteOffset: 15, as: UInt8.self)) // 44</div><div>}</div></div><div><br></div><div>NOTE however that the memory layout of Swift-structs is not guaranteed to remain like this and is thus not future-proof, although I think that if/when things changes, there will be some way to tell the compiler that you want the memory to be this "expected" "C-like" layout.</div><div><br></div><div>/Jens</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Wed, Jan 10, 2018 at 10:03 PM, Kelvin Ma via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div><div>I want to create a buffer with the layout <br></div><div><br></div><span style="font-family:monospace,monospace">0 4 8 12 13 14 15 16<br>[ x:Float |</span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"> y:Float </span>|</span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"> z:Float </span>| r:UInt8 | g:UInt8 | b:UInt8 | _:UInt8 ]</span><br></div><div><br></div>Normally, I’d use <span style="font-family:monospace,monospace">UnsafeRawBufferPointer</span> for this, but I need the buffer to be a vector (i.e. with <span style="font-family:monospace,monospace">append(_:)</span>), and for it to return a Swift-managed <span style="font-family:monospace,monospace">Array<UInt8></span>. How should I do this? The project does not use Foundation.<br></div>
<br></div></div>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div></div></div>