From: Brendan Hansen Date: Fri, 19 May 2023 19:19:04 +0000 (-0500) Subject: changed: `os.with_file` implementation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=48ba29e5b479f5a5ba13cfe91459d535ddcc9936;p=onyx.git changed: `os.with_file` implementation --- 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 {