> On Jul 9, 2017, at 12:23 AM, Jens Persson <[email protected]> wrote:
> 
> 
> On Sat, Jul 8, 2017 at 6:28 PM, Chris Lattner via swift-evolution 
> <[email protected] <mailto:[email protected]>> wrote:
> Hi Susan,
> 
> Swift does not currently specify a layout for Swift structs.  You shouldn’t 
> be using them for memory mapped i/o or writing to a file, because their 
> layout can change.  When ABI stability for fragile structs lands, you will be 
> able to count on it, but until then something like this is probably a bad 
> idea.
> 
> -Chris
> 
> Does this imply that you should never use Swift structs to eg interact with 
> Metal?

No.

> This seems to be a very common practice. Here is a typical example (from a 
> Metal tutorial at raywenderlich.com <http://raywenderlich.com/>):
> 
> struct Vertex {
>   var x,y,z: Float     // position data
>   var r,g,b,a: Float   // color data
> 
>   func floatBuffer() -> [Float] {
>     return [x,y,z,r,g,b,a]
>   }
> }

This doesn’t appear to expose the layout of the struct.
> Also, does it imply that we cannot use structs (of only primitive types) like:
> 
> struct RgbaFloatsLinearGamma {
>     var r, g, b, a: Float
>     …
> }
> struct BgraBytesSrgbGamma {
>     var b, g, r, a: UInt8
> }
> 
> for manipulating raster image data?

I don’t see why that would be a problem.

> I vaguely remember a swift evo discussion where it was concluded that such 
> usage was considered OK provided the stored properties of the structs was 
> only primitive types, but I can't find it now.
> 
> Perhaps it could be considered OK at least when the intended platforms are 
> known to be only iOS devices?

I think you’re misunderstanding what I’m saying.  It isn’t correct to take 
(e.g.) an unsafepointer to the beginning of a struct, and serialize that out to 
disk, and expect that the fields are emitted in some order with some specific 
padding between them.  None of the uses above try to do this.

-Chris


_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to