I'm updating my code for Swift 5, and I'm having trouble transitioning to the new version of Data.withUnsafeBytes()
.
A case that seemed to work (the git_
calls are C functions from libgit2, full context here):
let result = data.withUnsafeBytes {
(bytes: UnsafePointer<Int8>) -> Int32 in
var entry = git_index_entry()
return path.withCString {
(path) in
entry.path = path
entry.mode = GIT_FILEMODE_BLOB.rawValue
return git_index_add_frombuffer(index, &entry, bytes, data.count)
}
}
I changed the type of the bytes
parameter to UnsafeRawBufferPointer
, and then passed bytes.baseAddress
instead of just bytes
to git_index_add_frombuffer
and the compiler was happy with that.
Then I hit some cases where it was harder to make the compiler happy. I started with, for example, this (full context here):
newData.withUnsafeBytes {
(bytes) in
result = git_diff_blob_to_buffer(oldGitBlob, nil,
bytes, newData.count, nil, nil,
git_diff_delta.fileCallback,
nil, nil, nil, &self)
}
If I change bytes
to explicitly (bytes: UnsafeRawBufferPointer)
like I did before, then the compiler says:
'UnsafeRawBufferPointer' is not convertible to 'UnsafePointer<_>'
Why is it trying to do that conversion? I thought I was doing the same fix here.