Application Context Implementation #4

Closed
kayomn wants to merge 93 commits from event-loop-dev into main
1 changed files with 14 additions and 14 deletions
Showing only changes of commit b8517d3b22 - Show all commits

View File

@ -1,11 +1,17 @@
const io = @import("./io.zig");
const std = @import("std");
///
/// Returns a fixed-size stack type of `Element`s.
///
pub fn Fixed(comptime Element: type) type {
return struct {
filled: usize = 0,
buffer: []Element,
///
/// Stack type.
///
const Self = @This();
///
@ -57,8 +63,8 @@ pub fn Fixed(comptime Element: type) type {
///
/// Attempts to push `element` into `self`, returning a [FixedPushError] if it failed.
///
pub fn push(self: *Self, element: Element) FixedPushError!void {
if (self.filled == self.buffer.len) return error.Overflow;
pub fn push(self: *Self, element: Element) PushError!void {
if (self.filled == self.buffer.len) return error.OutOfMemory;
self.buffer[self.filled] = element;
self.filled += 1;
@ -68,10 +74,10 @@ pub fn Fixed(comptime Element: type) type {
/// Attempts to push all of `elements` into `self`, returning a [FixedPushError] if it
/// failed.
///
pub fn pushAll(self: *Self, elements: []const u8) FixedPushError!void {
pub fn pushAll(self: *Self, elements: []const u8) PushError!void {
const filled = (self.filled + elements.len);
if (filled > self.buffer.len) return error.Overflow;
if (filled > self.buffer.len) return error.OutOfMemory;
std.mem.copy(u8, self.buffer[self.filled ..], elements);
@ -81,15 +87,9 @@ pub fn Fixed(comptime Element: type) type {
}
///
/// Potential errors that may occur while trying to push one or more elements into a stack of a
/// known maximum size.
/// Potential errors that may occur while trying to push one or more elements into a stack.
///
/// [FinitePushError.Overflow] is returned if the stack does not have sufficient capacity to hold a
/// given set of elements.
///
pub const FixedPushError = error {
Overflow,
};
pub const PushError = std.mem.Allocator.Error;
test {
const testing = std.testing;
@ -103,8 +103,8 @@ test {
try testing.expectEqual(stack.pop(), 69);
try stack.pushAll(&.{42, 10, 95, 0});
try testing.expectEqual(stack.count(), 4);
try testing.expectError(FixedPushError.Overflow, stack.push(1));
try testing.expectError(FixedPushError.Overflow, stack.pushAll(&.{1, 11, 11}));
try testing.expectError(PushError.OutOfMemory, stack.push(1));
try testing.expectError(PushError.OutOfMemory, stack.pushAll(&.{1, 11, 11}));
stack.clear();