Module fs/file
Async file I/O operations with Exception-based error handling.
Wraps a file descriptor with typed async I/O operations.
Uses the Exception effect for error handling.
Example
{ File, read_string, write_file, OpenMode } :: import "std/fs/file";
main :: (fn(using(io : IO)) -> unit)({
given(exn) := Exception(
throw : (fn(forall(T : Type), error: AnyError) -> T)(
{ println(error); panic("Exception found."); }
)
);
// Write a file
io.await(write_file(Path.new(`test.txt`), `hello world`));
// Read it back
content := io.await(read_string(Path.new(`test.txt`)));
println(content);
// Open with OpenMode
f := io.await(File.open(Path.new(`test.txt`), .Read));
});
Types
An open file handle with async read/write operations.
Provides methods for reading, writing, seeking, and querying file metadata. Files are automatically closed when disposed.
Fields
| Name | Type | Description |
|---|---|---|
_fd | i32 | |
_path | Path | |
_is_closed | bool |
Trait Implementations
impl(File, ...)
open_with : (File) fn(path : Path, mode : OpenMode, perm : FilePermission, using(io : IO)) -> Impl(Future(File, IO, Exception))open_with_str : (File) fn(path : str, mode : OpenMode, perm : FilePermission, using(io : IO)) -> Impl(Future(File, IO, Exception))Open a file from a str path with the given mode and custom permissions.
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | str | Get the path this file was opened with. | |
mode | OpenMode | ||
perm | FilePermission |
open_with_cstr : (File) fn(path : *(u8), mode : OpenMode, perm : FilePermission, using(io : IO)) -> Impl(Future(File, IO, Exception))Open a file from a C string path with the given mode and custom permissions.
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | *(u8) | Get the path this file was opened with. | |
mode | OpenMode | ||
perm | FilePermission |
open : (File) fn(path : Path, mode : OpenMode, using(io : IO)) -> Impl(Future(File, IO, Exception))open_str : (File) fn(path : str, mode : OpenMode, using(io : IO)) -> Impl(Future(File, IO, Exception))open_cstr : (File) fn(path : *(u8), mode : OpenMode, using(io : IO)) -> Impl(Future(File, IO, Exception))read : (File) fn(self : File, buf : *(u8), size : u32, using(io : IO)) -> Impl(Future(i32, IO, Exception))write_string : (File) fn(self : File, data : String, using(io : IO)) -> Impl(Future(i32, IO, Exception))write_bytes : (File) fn(self : File, data : ArrayList(u8), using(io : IO)) -> Impl(Future(i32, IO, Exception))read_bytes : (File) fn(self : File, using(io : IO)) -> Impl(Future(ArrayList(u8), IO, Exception))read_string : (File) fn(self : File, using(io : IO)) -> Impl(Future(String, IO, Exception))flush : (File) fn(self : File, using(io : IO)) -> Impl(Future(unit, IO, Exception))seek : (File) fn(self : File, offset : i64, from : SeekFrom, using(exn : Exception)) -> i64position : (File) fn(self : File) -> i64Get the current file position (byte offset from the beginning).
Parameters
| Name | Type | Notes |
|---|---|---|
self | File |
Returns: i64
size : (File) fn(self : File) -> i64close : (File) fn(self : File, using(io : IO)) -> Impl(Future(unit, IO, Exception))fd : (File) fn(self : File) -> i32path : (File) fn(self : File) -> Pathimpl(File, Dispose(...))
dispose : (fn(self: Self) -> unit)Returns: unit
Variants
| Variant | Fields | Description |
|---|---|---|
Read | ||
Write | ||
Append | ||
ReadWrite | ||
CreateNew |
Fields
| Name | Type | Description |
|---|---|---|
mode | u32 |
Methods
default : (FilePermission) fn() -> FilePermissionReturns: FilePermission
executable : (FilePermission) fn() -> FilePermissionReturns: FilePermission
readonly : (FilePermission) fn() -> FilePermissionReturns: FilePermission
private : (FilePermission) fn() -> FilePermissionReturns: FilePermission
Variants
| Variant | Fields | Description |
|---|---|---|
Start | ||
Current | ||
End |
Functions
Read an entire file into a String (C string path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | *(u8) | Get the path this file was opened with. |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Read an entire file into bytes (str path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | str | Get the path this file was opened with. |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Read an entire file into bytes (C string path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | *(u8) | Get the path this file was opened with. |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Write a str to a file, creating or truncating it (str path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | str | Get the path this file was opened with. | |
data | str |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Write a str to a file, creating or truncating it (C string path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | *(u8) | Get the path this file was opened with. | |
data | str |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Append a str to a file (str path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | str | Get the path this file was opened with. | |
data | str |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Check if a path exists. Returns false for any error including file not found.
Does not use the Exception effect.
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | Path | Get the path this file was opened with. |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Returns: Impl(Future(bool, IO))
Resolve a path to its canonical absolute form (resolves symlinks, . and ..).
Throws if the path does not exist.
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | Path | Get the path this file was opened with. |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Resolve a path to its canonical absolute form (str path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | str | Get the path this file was opened with. |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |
Resolve a path to its canonical absolute form (C string path variant).
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
path | *(u8) | Get the path this file was opened with. |
Effects
| Name | Type | Notes |
|---|---|---|
io | IO | comptime, implicit |