I opened an old project and got these warnings that weren’t there before
Forming ‘UnsafeRawPointer’ to a variable of type ‘[T]’; this is likely incorrect because ‘T’ may contain an object reference
extension MTLDevice {
func makeEZBuffer<T>(array: [T]) -> MTLBuffer? {
self.makeBuffer(
bytes: array, //<<<< warning
length: MemoryLayout<T>.stride * array.count,
options: [.storageModeShared])
}
func makeEZBuffer<T>(single: T) -> MTLBuffer? {
var v = single
return self.makeBuffer(
bytes: &v, //<<<< warning
length: MemoryLayout<T>.stride,
options: [.storageModeShared])
}
}
//makeBuffer signature
func makeBuffer(
bytes pointer: UnsafeRawPointer,
length: Int,
options: MTLResourceOptions = []
) -> MTLBuffer?
I get the warning and it seems I could fix it by constraining the generic T to value types, but how? I could constrain to classes with <T: AnyClass> but there’s no <T: AnyValue> as far as I know. What should I do?
I could make a marker protocol that’s added to all my structs used with those functions, is that the solution or is there a more flexible way?