Made File.Handle a non-copyable struct in the library to adopt Swift 6.4.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user