Made File.Handle a non-copyable struct in the library to adopt Swift 6.4.

This commit is contained in:
2026-09-18 12:18:05 +02:00
parent 95ae01f97a
commit 07c4fce37b
5 changed files with 66 additions and 30 deletions
+7 -13
View File
@@ -118,27 +118,21 @@ extension JSON {
}
/// Decodes JSON read from an open file into a `Value` tree.
public static func decode(file: File.Handle) throws(PlaydateError) -> Value {
public static func decode(file: borrowing File.Handle) throws(PlaydateError) -> Value {
let context = DecodeContext()
var decoder = makeDecoder(context: Unmanaged.passUnretained(context))
var reader = json_reader()
reader.userdata = Unmanaged.passUnretained(file).toOpaque()
// The handle is borrowed for the whole call, so its `SDFile` stays
// open while the decoder reads through it.
reader.userdata = file.pointer
reader.read = { userdata, buffer, size in
guard let userdata, let buffer else { return -1 }
let file = Unmanaged<File.Handle>.fromOpaque(userdata).takeUnretainedValue()
var destination = UnsafeMutableBufferPointer(start: buffer, count: Int(size)).mutableSpan
do {
let count = try file.read(into: &destination)
return count > 0 ? Int32(count) : -1
} catch {
return -1
}
let count = fileAPI.pointee.read.unsafelyUnwrapped(userdata, buffer, UInt32(size))
return count > 0 ? count : -1
}
var outval = json_value()
let ok = withExtendedLifetime(context) {
withExtendedLifetime(file) {
jsonAPI.pointee.decode.unsafelyUnwrapped(&decoder, reader, &outval) != 0
}
jsonAPI.pointee.decode.unsafelyUnwrapped(&decoder, reader, &outval) != 0
}
guard ok else {
// A completed root container may already have been written to