Which types in Swift are considered trivial types and is there a Protocol, or Protocol composition, to restrict a generate parameter to only trivial types.
Consider a generic function for writing values into a raw buffer:
func write<T>(_ value:T) {
bufferPointer.storeBytes(of:value, toByteOffset:0, as:T.self)
}
The documentation for storeBytes<T>(of: T, toByteOffset: Int, as: T.Type)
states that T
must be a trivial type. However, the compiler doesn't seem to complain if I do something like this:
class Style {
var color = NSColor.red
var font = NSFont.labelFont(ofSize:11)
}
let style = Style()
write(style)
Style
isn't something I'd consider to be a trivial type that .storeBytes
can properly store, yet there's no error.
So what would be an appropriate constraint on write<T>(_ value:T) where T...
such that only trivial types are allowed?