p-x9/swift-fileio
A Swift library for reading and writing files.
Supported Platforms
| Platform | MemoryMappedFile | StreamedFile | Concatenated* | |---|---|---|---| | Apple platforms | ✅ | ✅ | ✅ | | Linux (glibc) | ✅ | ✅ | ✅ | | Linux (musl / Static Linux SDK) | ✅ | ✅ | ✅ | | Android | ✅ | ✅ | ✅ | | Windows | ✅ | ✅ | ✅ |
All of these are built and tested in CI, except Android and the Static Linux SDK, which are cross-compiled and so build-only.
Features
- MemoryMappedFile: using mmap
- StreamedFile: using FileHandle (syscall)
- ConcatenatedMemoryMappedFile: using mmap. Treats multiple files as one continuous virtual file. Each file is mapped independently and the concatenation is logical, so there is no whole-file pointer and no constraint on the individual file sizes. Zero-copy access goes through
unsafeRegion(at:). - StreamedFile: using FileHandle (syscall). Treats multiple files as one continuous virtual file.
Usage
MemoryMappedFile/StreamedFile have the same API available for both.
Available methods are defined in the FileIOProtocol
Design Overview
This library separates file I/O into three orthogonal concerns:
- Capability: what operations are supported (
_FileIOProtocol) - Role: how the object is used (
FileIOProtocol,FileIOSiliceProtocol) - Implementation strategy: how I/O is performed (memory-mapped or streamed)
Protocol Relationships
The following diagram illustrates the relationships between the core protocols in this library.
graph TD
_FileIOProtocol --> FileIOProtocol
_FileIOProtocol --> FileIOSiliceProtocol
_FileIOProtocol --> _MemoryMappedFileIOProtocol
_FileIOProtocol --> _StreamedFileIOProtocol
FileIOProtocol --> MemoryMappedFileIOProtocol
FileIOProtocol --> StreamedFileIOProtocol
_MemoryMappedFileIOProtocol --> _SingleMemoryMappedFileIOProtocol
_MemoryMappedFileIOProtocol --> MemoryMappedFileIOProtocol
_StreamedFileIOProtocol --> StreamedFileIOProtocol
FileIOProtocol --> FileSlice[associatedtype FileSlice]
FileSlice --> FileIOSiliceProtocol
_FileIOProtocol --> ResizableFileIOProtocol_FileIOProtocoldefines the fundamental read/write and synchronization operations.FileIOProtocolextends it with file-opening and slicing capabilities.FileIOSiliceProtocolrepresents a logical view into a file with abaseOffset.ResizableFileIOProtocoladds structural mutation operations such as insert and delete. Only whole-file types adopt it; slices do not.- Changing a file's length invalidates every slice taken from it beforehand — all of them, not only the ones whose bytes moved, since nothing records which ranges a mutation shifted.
isValidreports this, and the remedy is to take the slice again. - A stale slice throws
FileIOError.staleSlicefrom everything that can throw.sync()andrefresh()have no failure channel and do nothing at all, andMemoryMappedFileSlice.ptris unchecked and can still hand back a pointer outside the mapping — useunsafeRegion(at:)for the checked form. _MemoryMappedFileIOProtocoland_StreamedFileIOProtocoldescribe low-level implementation traits._MemoryMappedFileIOProtocolrequiresunsafeRegion(at:), which returns a pointer and how far the memory stays contiguous from it. Both mapping strategies can satisfy this, so generic code written against it works with either._SingleMemoryMappedFileIOProtocoladdsptr, a base pointer for the whole file.MemoryMappedFilehas one;ConcatenatedMemoryMappedFilemaps each of its files separately and genuinely does not, so it adopts only the former.MemoryMappedFileIOProtocolandStreamedFileIOProtocolcombine implementation traits withFileIOProtocol.
License
FileIO is released under the MIT License. See LICENSE
Package Metadata
Repository: p-x9/swift-fileio
Default branch: main
README: README.md