Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions course/code/14/enum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ const EnumSize = struct {
};

pub fn main() !void {
try expect(@typeInfo(Small).Enum.tag_type == u2);
try expect(@typeInfo(Small).Enum.fields.len == 4);
try expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two"));
try expect(@typeInfo(Small).@"enum".tag_type == u2);
try expect(@typeInfo(Small).@"enum".fields.len == 4);
try expect(mem.eql(u8, @typeInfo(Small).@"enum".fields[1].name, "two"));
try expect(mem.eql(u8, @tagName(Small.three), "three"));
}
// #endregion enum_size
Expand Down
2 changes: 1 addition & 1 deletion course/code/14/optional_type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const ComptimeAccessOptionalType = struct {
foo = 1234;

// 使用编译期反射来获取 foo 的类型信息
try comptime expect(@typeInfo(@TypeOf(foo)).Optional.child == i32);
try comptime expect(@typeInfo(@TypeOf(foo)).optional.child == i32);
// #endregion comptime_access_optional_type
}
};
4 changes: 2 additions & 2 deletions course/code/14/pointer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const Align = struct {

if (builtin.target.cpu.arch == .x86_64) {
// 获取了 x86_64 架构的指针对齐大小
try expect(@typeInfo(*i32).Pointer.alignment == 4);
try expect(@typeInfo(*i32).pointer.alignment == 4);
}
}
// #endregion align
Expand All @@ -137,7 +137,7 @@ const AlignCast = struct {

pub fn main() !void {
// 全局变量对齐
try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
try expect(@typeInfo(@TypeOf(&foo)).pointer.alignment == 4);
try expect(@TypeOf(&foo) == *align(4) u8);
const as_pointer_to_array: *align(4) [1]u8 = &foo;
const as_slice: []align(4) u8 = as_pointer_to_array;
Expand Down
10 changes: 5 additions & 5 deletions course/code/14/reflection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const typeInfo = struct {
// 通过 @typeInfo 获取类型信息
const type_info = @typeInfo(T);
// 断言它为 struct
const struct_info = type_info.Struct;
const struct_info = type_info.@"struct";

// inline for 打印该结构体内部字段的信息
inline for (struct_info.fields) |field| {
Expand All @@ -82,7 +82,7 @@ const TypeInfo2 = struct {

fn IntToArray(comptime T: type) type {
// 获得类型信息,并断言为Int
const int_info = @typeInfo(T).Int;
const int_info = @typeInfo(T).int;
// 获得Int位数
const bits = int_info.bits;
// 检查位数是否被8整除
Expand All @@ -106,7 +106,7 @@ const TypeInfo3 = struct {

fn ExternAlignOne(comptime T: type) type {
// 获得类型信息,并断言为Struct.
comptime var struct_info = @typeInfo(T).Struct;
comptime var struct_info = @typeInfo(T).@"struct";
// 将内存布局改为 extern
struct_info.layout = .@"extern";
// 复制字段信息(原为只读切片,故需复制)
Expand All @@ -116,7 +116,7 @@ const TypeInfo3 = struct {
// 替换字段定义
struct_info.fields = &new_fields;
// 重新构造类型
return @Type(.{ .Struct = struct_info });
return @Type(.{ .@"struct" = struct_info });
}

const MyStruct = struct {
Expand Down Expand Up @@ -245,7 +245,7 @@ const Type = struct {
const std = @import("std");

const T = @Type(.{
.Struct = .{
.@"struct" = .{
.layout = .auto,
.fields = &.{
.{
Expand Down