From aa4f14760e80efa703f12cff0eca6f3d23103ec7 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Tue, 23 Jan 2024 11:16:53 -0800 Subject: [PATCH] opslice: handle pointer to array --- src/opslice.zig | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/opslice.zig b/src/opslice.zig index 8f46d11e2d6545e74c3fcccbe204dc688664532d..657332b196aa2dac6c742ceebe87f3902ce7e2e6 100644 --- a/src/opslice.zig +++ b/src/opslice.zig @@ -2,7 +2,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); -pub fn opslice(slice: anytype, index: usize) ?std.meta.Child(@TypeOf(slice)) { +pub fn opslice(slice: anytype, index: usize) ?std.meta.Elem(@TypeOf(slice)) { if (slice.len <= index) return null; return slice[index]; } @@ -19,3 +19,16 @@ test { try std.testing.expect(opslice(slice, 5) == 5); try std.testing.expect(opslice(slice, 6) == null); } + +test { + const array = [_]u8{ 0, 1, 2, 3, 4, 5 }; + const slice = &array; + + try std.testing.expect(opslice(slice, 0) == 0); + try std.testing.expect(opslice(slice, 1) == 1); + try std.testing.expect(opslice(slice, 2) == 2); + try std.testing.expect(opslice(slice, 3) == 3); + try std.testing.expect(opslice(slice, 4) == 4); + try std.testing.expect(opslice(slice, 5) == 5); + try std.testing.expect(opslice(slice, 6) == null); +} -- 2.54.0