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); +}