Tightened the source code and README documentations in the library.

This commit is contained in:
2026-09-18 12:45:39 +02:00
parent c9f887bacb
commit 9ae10590cc
97 changed files with 854 additions and 1142 deletions
+12 -17
View File
@@ -1,15 +1,12 @@
internal import CPlaydate
extension File {
/// An open file. Wraps `SDFile`. The file is closed when the handle goes
/// out of scope, unless it was closed explicitly with `close()`.
///
/// The handle is non-copyable: it has a single owner, so it cannot be
/// used after `close()` and no heap allocation backs it.
/// An open file. Wraps `SDFile`. Non-copyable: closes when the handle goes out of
/// scope, or earlier via consuming `close()`. At most 64 files may be open.
public struct Handle: ~Copyable {
let pointer: UnsafeMutableRawPointer
/// Opens the file at `path`.
/// Opens the file at `path` in `mode`.
public init(path: String, mode: Options) throws(PlaydateError) {
let pointer = path.withCString {
fileAPI.pointee.open.unsafelyUnwrapped($0, mode.cValue)
@@ -23,8 +20,7 @@ extension File {
}
/// Closes the file, consuming the handle.
// `@export(interface)` lets `discard` compile in Embedded Swift with
// the 6.4 release toolchain; later toolchains accept it without.
// `@export(interface)` lets `discard` compile in Embedded Swift on the 6.4 toolchain.
@export(interface)
public consuming func close() throws(PlaydateError) {
let pointer = self.pointer
@@ -32,8 +28,7 @@ extension File {
if fileAPI.pointee.close.unsafelyUnwrapped(pointer) != 0 { throw lastFileError() }
}
/// Reads up to `buffer.count` bytes into `buffer`. Returns the number
/// of bytes read; 0 indicates end of file.
/// Reads up to `buffer.count` bytes; returns the count read, 0 at end of file.
public func read(into buffer: inout MutableSpan<UInt8>) throws(PlaydateError) -> Int {
let result = buffer.withUnsafeMutableBufferPointer { buffer in
fileAPI.pointee.read.unsafelyUnwrapped(pointer, buffer.baseAddress, UInt32(buffer.count))
@@ -42,7 +37,7 @@ extension File {
return Int(result)
}
/// Reads up to `length` bytes and returns them.
/// Reads up to `length` bytes; shorter near end of file, empty at it.
public func read(length: Int) throws(PlaydateError) -> [UInt8] {
try [UInt8](capacity: length) { output throws(PlaydateError) in
let result = output.withUnsafeMutableBufferPointer { buffer, initializedCount in
@@ -55,7 +50,7 @@ extension File {
}
}
/// Writes the bytes to the file. Returns the number of bytes written.
/// Writes `bytes`; returns the count written.
@discardableResult
public func write(_ bytes: Span<UInt8>) throws(PlaydateError) -> Int {
let result = bytes.withUnsafeBufferPointer { buffer in
@@ -65,7 +60,7 @@ extension File {
return Int(result)
}
/// Writes the bytes to the file. Returns the number of bytes written.
/// Writes `bytes`; returns the count written.
@discardableResult
public func write(_ bytes: [UInt8]) throws(PlaydateError) -> Int {
try bytes.withUnsafeBufferPointer { buffer throws(PlaydateError) in
@@ -73,7 +68,7 @@ extension File {
}
}
/// Writes the string's UTF-8 to the file. Returns the bytes written.
/// Writes `string` as UTF-8, without a NUL terminator; returns the count written.
@discardableResult
public func write(_ string: String) throws(PlaydateError) -> Int {
let result = string.withCString { cString in
@@ -83,7 +78,7 @@ extension File {
return Int(result)
}
/// Flushes buffered writes to disk. Returns the bytes written.
/// Flushes buffered writes; returns the count written.
@discardableResult
public func flush() throws(PlaydateError) -> Int {
let result = fileAPI.pointee.flush.unsafelyUnwrapped(pointer)
@@ -91,14 +86,14 @@ extension File {
return Int(result)
}
/// The current read/write offset.
/// The current read/write offset, in bytes.
public func tell() throws(PlaydateError) -> Int {
let result = fileAPI.pointee.tell.unsafelyUnwrapped(pointer)
if result < 0 { throw lastFileError() }
return Int(result)
}
/// Moves the read/write offset to `offset` relative to `origin`.
/// Moves the read/write offset to `offset` bytes from `origin`.
public func seek(to offset: Int, from origin: SeekOrigin = .start) throws(PlaydateError) {
if fileAPI.pointee.seek.unsafelyUnwrapped(pointer, Int32(offset), origin.rawValue) != 0 {
throw lastFileError()