1. Bit shift is not part of the IntegerType protocol, when it should be (although the author could avoid the issue by accumulating the bytes in a UIntMax instead of the generic type).
2. Construction from (and conversion to) a UIntMax bit pattern is not part of the IntegerType protocol, when it should be (done correctly, this addresses the author's sign and construction complaints)
The author incorrectly claims/implies that these are problems with generics or protocols or the diversity of integer types in Swift. They're really a problem of omissions in the standard library protocols that are forcing some very cumbersome workarounds. The necessary functionality exists, it just isn't part of the relevant protocols. Submit these as bugs to Apple.
Edit:
As a follow up, here's a version that gets around the standard library limitations using an unsafe pointer...
func integerWithBytes<T: IntegerType>(bytes:[UInt8]) -> T? {
if (bytes.count < sizeof(T)) {
return nil
}
var i:UIntMax = 0
for (var j = 0; j < sizeof(T); j++) {
i = i | (UIntMax(bytes[j]) << UIntMax(j * 8))
}
return withUnsafePointer(&i) { ip -> T in
return UnsafePointer<T>(ip).memory
}
}
Of course, at that point, why not simply reinterpret the array buffer directly... func integerWithBytes<T: IntegerType>(bytes:[UInt8]) -> T? {
if (bytes.count < sizeof(T)) {
return nil
}
return bytes.withUnsafeBufferPointer() { bp -> T in
return UnsafePointer<T>(bp.baseAddress).memory
}
}