hasUniqueRepresentation
True if every value of the type T has a unique bit pattern representing it.
In other words, T has no unused bits and no padding.
Result is always comptime-known.
Function parameters
Parameters
- T:type
Functions in this namespace
Functions
- stringToEnum
- Returns the variant of an enum type, `T`, which is named `str`, or `null` if no such variant exists.
- alignment
- Returns the alignment of type T.
- Child
- Given a parameterized type (array, vector, pointer, optional), returns the "child type".
- Elem
- Given a "memory span" type (array, slice, vector, or pointer to such), returns the "element type".
- sentinel
- Given a type which can have a sentinel e.g.
- Sentinel
- Given a "memory span" type, returns the same type except with the given sentinel value.
- declarations
- Instead of this function, prefer to use e.g.
- tags
- Given an enum or error set type, returns a pointer to an array containing all tags for that
- FieldEnum
- Returns an enum with a variant named after each field of `T`.
- activeTag
- Returns the active tag of a tagged union
- TagPayloadByName
- Deprecated: Use @FieldType(U, tag_name)
- TagPayload
- Deprecated: Use @FieldType(U, @tagName(tag))
- eql
- Compares two of any type for equality.
- intToEnum
- Deprecated: use `std.enums.fromInt` instead and handle null instead of an error.
- fieldIndex
- Given a type and a name, return the field index according to source order.
- declList
- Returns a slice of pointers to public declarations of a namespace.
- ArgsTuple
- For a given function type, returns a tuple type which fields will
- Tuple
- For a given anonymous list of types, returns a new tuple type
- isError
- Returns whether `error_union` contains an error.
- hasFn
- Returns true if a type has a namespace and the namespace contains `name`;
- hasMethod
- Returns true if a type has a `name` method; `false` otherwise.
- hasUniqueRepresentation
- True if every value of the type `T` has a unique bit pattern representing it.
Error sets in this namespace
Error Sets
- IntToEnumError
- Deprecated: use `std.enums.fromInt` instead and handle null.
Source
Implementation
pub inline fn hasUniqueRepresentation(comptime T: type) bool {
return switch (@typeInfo(T)) {
else => false, // TODO can we know if it's true for some of these types ?
.@"anyframe",
.@"enum",
.error_set,
.@"fn",
=> true,
.bool => false,
.int => |info| @sizeOf(T) * 8 == info.bits,
.pointer => |info| info.size != .slice,
.optional => |info| switch (@typeInfo(info.child)) {
.pointer => |ptr| !ptr.is_allowzero and switch (ptr.size) {
.slice, .c => false,
.one, .many => true,
},
else => false,
},
.array => |info| hasUniqueRepresentation(info.child),
.@"struct" => |info| {
if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T);
var sum_size = @as(usize, 0);
inline for (info.fields) |field| {
if (field.is_comptime) continue;
if (!hasUniqueRepresentation(field.type)) return false;
sum_size += @sizeOf(field.type);
}
return @sizeOf(T) == sum_size;
},
.vector => |info| hasUniqueRepresentation(info.child) and
@sizeOf(T) == @sizeOf(info.child) * info.len,
};
}