Function sizeof [src]

Given a value returns its size as C's sizeof operator would.

Prototype

pub fn sizeof(target: anytype) usize

Source

pub fn sizeof(target: anytype) usize { const T: type = if (@TypeOf(target) == type) target else @TypeOf(target); switch (@typeInfo(T)) { .float, .int, .@"struct", .@"union", .array, .bool, .vector => return @sizeOf(T), .@"fn" => { // sizeof(main) in C returns 1 return 1; }, .null => return @sizeOf(*anyopaque), .void => { // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC. return 1; }, .@"opaque" => { if (T == anyopaque) { // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC. return 1; } else { @compileError("Cannot use C sizeof on opaque type " ++ @typeName(T)); } }, .optional => |opt| { if (@typeInfo(opt.child) == .pointer) { return sizeof(opt.child); } else { @compileError("Cannot use C sizeof on non-pointer optional " ++ @typeName(T)); } }, .pointer => |ptr| { if (ptr.size == .slice) { @compileError("Cannot use C sizeof on slice type " ++ @typeName(T)); } // for strings, sizeof("a") returns 2. // normal pointer decay scenarios from C are handled // in the .array case above, but strings remain literals // and are therefore always pointers, so they need to be // specially handled here. if (ptr.size == .one and ptr.is_const and @typeInfo(ptr.child) == .array) { const array_info = @typeInfo(ptr.child).array; if ((array_info.child == u8 or array_info.child == u16) and array_info.sentinel() == 0) { // length of the string plus one for the null terminator. return (array_info.len + 1) * @sizeOf(array_info.child); } } // When zero sized pointers are removed, this case will no // longer be reachable and can be deleted. if (@sizeOf(T) == 0) { return @sizeOf(*anyopaque); } return @sizeOf(T); }, .comptime_float => return @sizeOf(f64), // TODO c_double #3999 .comptime_int => { // TODO to get the correct result we have to translate // `1073741824 * 4` as `int(1073741824) *% int(4)` since // sizeof(1073741824 * 4) != sizeof(4294967296). // TODO test if target fits in int, long or long long return @sizeOf(c_int); }, else => @compileError("std.meta.sizeof does not support type " ++ @typeName(T)), } }