From 87c0060e813be6ee9b449058b4288d148706f8a4 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 4 May 2018 23:48:14 +0200 Subject: Made container methods that can be const, const --- std/array_list.zig | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'std/array_list.zig') diff --git a/std/array_list.zig b/std/array_list.zig index bd7e8ea7ed..f1881cd7f3 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -28,11 +28,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{ }; } - pub fn deinit(l: &Self) void { + pub fn deinit(l: &const Self) void { l.allocator.free(l.items); } - pub fn toSlice(l: &Self) []align(A) T { + pub fn toSlice(l: &const Self) []align(A) T { return l.items[0..l.len]; } @@ -150,7 +150,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{ } }; - pub fn iterator(self: &Self) Iterator { + pub fn iterator(self: &const Self) Iterator { return Iterator { .list = self, .count = 0 }; } }; @@ -168,6 +168,14 @@ test "basic ArrayList test" { assert(list.items[i] == i32(i + 1)); }} + for (list.toSlice()) |v, i| { + assert(v == i32(i + 1)); + } + + for (list.toSliceConst()) |v, i| { + assert(v == i32(i + 1)); + } + assert(list.pop() == 10); assert(list.len == 9); @@ -228,4 +236,4 @@ test "insert ArrayList test" { const items = []const i32 { 1 }; try list.insertSlice(0, items[0..0]); assert(list.items[0] == 5); -} \ No newline at end of file +} -- cgit v1.2.3