From: Brendan Hansen Date: Tue, 7 Mar 2023 01:41:28 +0000 (-0600) Subject: bugfix: '??' precedence; added: writer_consumer_error, alloc.on_temp X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=refs%2Fheads%2Ffirst-class-optional;p=onyx.git bugfix: '??' precedence; added: writer_consumer_error, alloc.on_temp --- diff --git a/core/alloc/alloc.onyx b/core/alloc/alloc.onyx index 86854abe..25834be6 100644 --- a/core/alloc/alloc.onyx +++ b/core/alloc/alloc.onyx @@ -32,6 +32,13 @@ on_heap :: macro (v: $V) -> &V { return out; } +on_temp :: macro (v: $V) -> &V { + out := cast(&V) raw_alloc(context.temp_allocator, sizeof V); + core.memory.set(out, 0, sizeof V); + *out = v; + return out; +} + TEMPORARY_ALLOCATOR_SIZE :: 1 << 16; // 16Kb // The global heap allocator, set up upon program intialization. diff --git a/core/container/optional.onyx b/core/container/optional.onyx index 30d458a9..e4d961a3 100644 --- a/core/container/optional.onyx +++ b/core/container/optional.onyx @@ -107,18 +107,18 @@ package core return o1.value == o2.value; } -#operator ?? macro (opt: ?$T, catch: Code) -> T { +#operator ?? macro (opt: ?$T, default: T) -> T { value := opt; if value do return value.value; - #unquote catch; + return default; } -#operator ?? macro (opt: ?$T, default: T) -> T { +#operator ?? macro (opt: ?$T, catch: Code) -> T { value := opt; if value do return value.value; - return default; + #unquote catch; } #operator ? macro (opt: ?$T) -> T { diff --git a/core/io/writer.onyx b/core/io/writer.onyx index 99ef801e..47da1f57 100644 --- a/core/io/writer.onyx +++ b/core/io/writer.onyx @@ -11,6 +11,8 @@ use core {conv, string, memory} Writer :: struct { stream : &Stream; + error : Error; + buffer: [] u8; buffer_filled: u32; } @@ -37,10 +39,19 @@ writer_free :: (w: &Writer) { writer_flush :: (w: &Writer) { if w.buffer_filled == 0 do return; - stream_write(w.stream, w.buffer[0 .. w.buffer_filled]); + err, bytes_wrote := stream_write(w.stream, w.buffer[0 .. w.buffer_filled]); + if err != .None { + w.error = err; + } + w.buffer_filled = 0; } +writer_consume_error :: (w: &Writer) -> Error { + defer w.error = .None; + return w.error; +} + writer_remaining_capacity :: (w: &Writer) -> u32 { return w.buffer.count - w.buffer_filled; } @@ -54,7 +65,10 @@ string_builder :: (allocator := context.allocator) -> (Writer, &BufferStream) { write_byte :: (use writer: &Writer, byte: u8) { if buffer.count == 0 { - stream_write_byte(stream, byte); + if err := stream_write_byte(stream, byte); err != .None { + writer.error = err; + } + } else { if writer_remaining_capacity(writer) == 0 { writer_flush(writer); @@ -67,7 +81,9 @@ write_byte :: (use writer: &Writer, byte: u8) { write_str :: (use writer: &Writer, s: str) { if buffer.count == 0 { - stream_write(stream, s); + if err := stream_write(stream, s); err != .None { + writer.error = err; + } } elseif writer_remaining_capacity(writer) > s.count { memory.copy(&buffer[buffer_filled], s.data, s.count); @@ -75,7 +91,9 @@ write_str :: (use writer: &Writer, s: str) { } else { writer_flush(writer); - stream_write(stream, s); + if err := stream_write(stream, s); err != .None { + writer.error = err; + } } }