Proposal Sketch: Bring flexible array members in as Array|UnsafeReference


(TJ Usiyan) #1

MIDIMetaEvent and MIDIEventUserData are both examples of framework provided
types which rely on flexible member arrays. Their last member is
(officially) an array of UInt8 with 1 element. In practice, developers are
meant to allocate enough space for an array of the necessary length along
with the rest of the struct.

These members come into swift as single element tuples. I propose either
automatic import of these as Array or UnsafeReference.

Example of the current situation:

/*!

@struct MIDIMetaEvent

@discussion The parameters to specify a MIDI meta event

*/

public struct MIDIMetaEvent {

    public var metaEventType: UInt8

    public var unused1: UInt8

    public var unused2: UInt8

    public var unused3: UInt8

    public var dataLength: UInt32

    public var data: (UInt8)

    public init()

    public init(metaEventType: UInt8, unused1: UInt8, unused2: UInt8,
unused3: UInt8, dataLength: UInt32, data: (UInt8))

}

/*!

@struct MusicEventUserData

@discussion Provides a general struct for specifying a user defined event.

@field length

the size in bytes of the data

@field data

size bytes of user defined event data

*/

public struct MusicEventUserData {

    public var length: UInt32

    public var data: (UInt8)

    public init()

    public init(length: UInt32, data: (UInt8))

}


(Dmitri Gribenko) #2

It is important to sketch out an implementation strategy for this proposal.

Dmitri

···

On Tue, Dec 22, 2015 at 2:29 PM, T.J. Usiyan via swift-evolution < swift-evolution@swift.org> wrote:

MIDIMetaEvent and MIDIEventUserData are both examples of framework
provided types which rely on flexible member arrays. Their last member is
(officially) an array of UInt8 with 1 element. In practice, developers are
meant to allocate enough space for an array of the necessary length along
with the rest of the struct.

These members come into swift as single element tuples. I propose either
automatic import of these as Array or UnsafeReference.

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(Félix Cloutier) #3

In general, I find that importing C arrays is problematic. I don't have any nice solution to offer, though.

For now, you might be able to get away with withUnsafePointer(&event.data) { ... }.

Félix

···

Le 22 déc. 2015 à 17:29:10, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> a écrit :

MIDIMetaEvent and MIDIEventUserData are both examples of framework provided types which rely on flexible member arrays. Their last member is (officially) an array of UInt8 with 1 element. In practice, developers are meant to allocate enough space for an array of the necessary length along with the rest of the struct.

These members come into swift as single element tuples. I propose either automatic import of these as Array or UnsafeReference.

Example of the current situation:
/*!
  @struct MIDIMetaEvent
  @discussion The parameters to specify a MIDI meta event
*/
public struct MIDIMetaEvent {
    
    public var metaEventType: UInt8
    public var unused1: UInt8
    public var unused2: UInt8
    public var unused3: UInt8
    public var dataLength: UInt32
    public var data: (UInt8)
    public init()
    public init(metaEventType: UInt8, unused1: UInt8, unused2: UInt8, unused3: UInt8, dataLength: UInt32, data: (UInt8))
}

/*!
  @struct MusicEventUserData
  @discussion Provides a general struct for specifying a user defined event.
  @field length
          the size in bytes of the data
  @field data
          size bytes of user defined event data
*/
public struct MusicEventUserData {
    
    public var length: UInt32
    public var data: (UInt8)
    public init()
    public init(length: UInt32, data: (UInt8))
}

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution