From 48ba29e5b479f5a5ba13cfe91459d535ddcc9936 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 19 May 2023 14:19:04 -0500 Subject: [PATCH] changed: `os.with_file` implementation --- core/os/file.onyx | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/core/os/file.onyx b/core/os/file.onyx index 79bb717b..db2077ea 100644 --- a/core/os/file.onyx +++ b/core/os/file.onyx @@ -103,31 +103,22 @@ get_contents :: #match { } with_file :: (path: str, mode := OpenMode.Read) -> Iterator(&File) { - Context :: struct { - valid_file_stream := false; - - file_stream: File; - } - - c := new(Context); - if file_stream := open(path, mode); file_stream { - c.file_stream = file_stream->unwrap(); - c.valid_file_stream = true; + file_stream: ? File; + if fs := open(path, mode); fs { + file_stream = fs->ok(); } - next :: (use c: &Context) -> (&File, bool) { - if !valid_file_stream do return null, false; + if file_stream { + return iter.single( + file_stream->unwrap() |> alloc.on_heap(), + file => { + close(file); + cfree(file); + }); - defer valid_file_stream = false; - return &file_stream, true; - } - - close_context :: (use c: &Context) { - close(&file_stream); - cfree(c); + } else { + return iter.empty(&File); } - - return .{ c, next, close_context }; } is_file :: (path: str) -> bool { -- 2.25.1