diff --git a/src/stack.zig b/src/stack.zig index 3f8284a..c4b5963 100755 --- a/src/stack.zig +++ b/src/stack.zig @@ -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();