Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

ReverseIterator

Returned by reverseIterator.

mem.ReverseIterator
pub fn ReverseIterator(comptime T: type) type

File

lib/std/mem.zig:3958

Code

pub fn ReverseIterator(comptime T: type) type {
    const ptr = switch (@typeInfo(T)) {
        .pointer => |ptr| ptr,
        else => @compileError("expected slice or pointer to array, found '" ++ @typeName(T) ++ "'"),
    };
    switch (ptr.size) {
        .slice => {},
        .one => if (@typeInfo(ptr.child) != .array) @compileError("expected slice or pointer to array, found '" ++ @typeName(T) ++ "'"),
        .many, .c => @compileError("expected slice or pointer to array, found '" ++ @typeName(T) ++ "'"),
    }
    const Element = std.meta.Elem(T);
    const Pointer = @Pointer(.many, ptr.attrs, Element, std.meta.sentinel(T));
    const ElementPointer = @Pointer(.one, ptr.attrs, Element, null);
    return struct {
        ptr: Pointer,
        index: usize,
        pub fn next(self: *@This()) ?Element {
            if (self.index == 0) return null;
            self.index -= 1;
            return self.ptr[self.index];
        }
        pub fn nextPtr(self: *@This()) ?ElementPointer {
            if (self.index == 0) return null;
            self.index -= 1;
            return &self.ptr[self.index];
        }
    };
}