Type Function Elem [src]

Given a "memory span" type (array, slice, vector, or pointer to such), returns the "element type".

Prototype

pub fn Elem(comptime T: type) type

Parameters

T: type

Example

test Elem { try testing.expect(Elem([1]u8) == u8); try testing.expect(Elem([*]u8) == u8); try testing.expect(Elem([]u8) == u8); try testing.expect(Elem(*[10]u8) == u8); try testing.expect(Elem(@Vector(2, u8)) == u8); try testing.expect(Elem(*@Vector(2, u8)) == u8); try testing.expect(Elem(?[*]u8) == u8); }

Source

pub fn Elem(comptime T: type) type { switch (@typeInfo(T)) { .array => |info| return info.child, .vector => |info| return info.child, .pointer => |info| switch (info.size) { .one => switch (@typeInfo(info.child)) { .array => |array_info| return array_info.child, .vector => |vector_info| return vector_info.child, else => {}, }, .many, .c, .slice => return info.child, }, .optional => |info| return Elem(info.child), else => {}, } @compileError("Expected pointer, slice, array or vector type, found '" ++ @typeName(T) ++ "'"); }