⬆️ upgrade async and replace native file FFI

升级 async 依赖,迁移到 MoonBit 原生 async/fs 文件接口,移除项目自有 C FFI,并固定文本文件为 LF。CI 全部通过。
This commit is contained in:
Nanaloveyuki
2026-08-25 16:06:37 +08:00
committed by GitHub
parent aab7a94ae2
commit b4d69675fd
24 changed files with 860 additions and 201 deletions
+65
View File
@@ -0,0 +1,65 @@
---
name: file-sink-async
group: api
category: sink
update-time: 20260825
description: Create a file sink from an async context using the native async filesystem interface.
key-word:
- file
- sink
- async
- public
---
## File-sink-async
Create a `FileSink` from an async context. The constructor opens the file with the native `moonbitlang/async/fs` interface and keeps the same rotation, policy, and failure-counter surface as `file_sink(...)`.
### Interface
```moonbit
pub async fn file_sink_async(
path : String,
append~ : Bool = true,
auto_flush~ : Bool = true,
rotation~ : FileRotation? = None,
formatter~ : RecordFormatter = fn(rec) { format_text(rec) },
) -> FileSink {}
```
#### input
- `path : String` - Destination file path.
- `append : Bool` - Whether writes append rather than truncate on the first open.
- `auto_flush : Bool` - Whether each async write requests an async file sync.
- `rotation : FileRotation?` - Optional size-based rotation policy.
- `formatter : RecordFormatter` - Formatter used to render each record.
#### output
- `FileSink` - A file sink ready for use by an async logger or direct async sink writes.
### Explanation
- Use this constructor when the caller is already running inside an async event loop.
- File writes, flushes, file-size checks, renames, removals, and rotation reopening use the async filesystem path.
- The returned sink implements `Sink::write_async(...)`; the synchronous `write(...)` surface remains available for synchronous callers.
- On non-native targets, the sink follows the existing unavailable-backend behavior.
### How to Use
```moonbit
let sink = file_sink_async("app.log", auto_flush=false)
let logger = async_logger(sink)
```
### Error Case
- If the initial async open fails, the returned sink is unavailable and increments `open_failures()`.
- Later async writes record write and rotation failures through the existing sink counters.
### Notes
1. Use `file_sink(...)` for synchronous construction outside an async event loop.
2. Pair this API with `native_files_supported()` when code must also compile for non-native targets.
+2
View File
@@ -98,3 +98,5 @@ e.g.:
3. Non-native targets can still compile code referencing this API, but callers should treat actual file availability and successful writes as target-sensitive runtime behavior.
4. See [target-verification.md](./target-verification.md) for the current verification boundary between design intent and locally re-checked targets.
5. Use [`file_sink_async(...)`](./file-sink-async.md) when constructing a file sink from an async event loop.
+1
View File
@@ -153,6 +153,7 @@ BitLogger API navigation.
- [patch-sink.md](./patch-sink.md)
- [patch-sink-type.md](./patch-sink-type.md)
- [file-sink.md](./file-sink.md)
- [file-sink-async.md](./file-sink-async.md)
- [file-sink-available.md](./file-sink-available.md)
- [file-sink-flush.md](./file-sink-flush.md)
- [file-sink-close.md](./file-sink-close.md)