std.containers.stack
std.containers.stack
Section titled “std.containers.stack”std.containers.stack provides a generic, fixed-capacity LIFO stack for
:core code. It stores values inline, performs no allocation, requires no
capability, and exposes overflow or empty-state results explicitly.
Import
Section titled “Import”use std.containers.stackContract
Section titled “Contract”The shipped v1 profile is deliberately bounded:
- capacity is
stack.MAX_CAP, currently64 - storage is inline in
Stack[T] pushandtry_pushreturnfalsewhen fullpopandpeekreturnnullwhen emptyresetclears the logical length without zeroing retained storage
There is no silent overflow. A failed push does not mutate the stack.
pub const MAX_CAP: usize = 64
pub struct Stack[T] { data: [MAX_CAP]T, len: usize}
pub func init[T]() -> Stack[T]pub func reset[T](edit self: *Stack[T]) -> voidpub func push[T](edit self: *Stack[T], value: T) -> boolpub func try_push[T](edit self: *Stack[T], value: T) -> boolpub func pop[T](edit self: *Stack[T]) -> ?Tpub func peek[T](view self: *Stack[T]) -> ?Tpub func len[T](view self: *Stack[T]) -> usizepub func size[T](view self: *Stack[T]) -> usizepub func is_empty[T](view self: *Stack[T]) -> boolpub func is_full[T](view self: *Stack[T]) -> boolpub func remaining[T](view self: *Stack[T]) -> usizepub func capacity() -> usizesize is an alias for len.
Example
Section titled “Example”use std.containers.stack
func main() -> i32 do var s = stack.init[u64]()
if stack.push[u64](&s, 10) != true do return 1 end if stack.push[u64](&s, 20) != true do return 2 end
let top = stack.pop[u64](&s) if top == null do return 3 end if top.? != 20 do return 4 end
if stack.pop[u64](&s).? != 10 do return 5 end if stack.pop[u64](&s) != null do return 6 end
return 0endVerification
Section titled “Verification”cd janus./scripts/zb test-containers-stackThe smoke imports the real module and proves LIFO order, overflow status,
empty-state null, reset, and multiple payload instantiations.