Skip to content

Instantly share code, notes, and snippets.

@kmuralidharan91
Created April 10, 2020 15:27
Show Gist options
  • Save kmuralidharan91/c404c5181b700004fead2856e8ad2db4 to your computer and use it in GitHub Desktop.
Save kmuralidharan91/c404c5181b700004fead2856e8ad2db4 to your computer and use it in GitHub Desktop.
public extension ArraySlice {
private func array() -> [Element] {
return Array(self)
}
func withAlignedBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
if startIndex == 0 || startIndex % MemoryLayout<R>.alignment == 0 {
return try self.withUnsafeBytes(body)
} else {
return try self.array().withUnsafeBytes(body)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment