authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-13 15:35:16-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-13 15:35:16-08:00
logbfcf18c5a7d13d990c6bbda04d6a2cd37038a9ea
treef75a1becd8f5df0e222a95d479ca7cbc260a1325
parent4fc295dc02dd0b311a22b3b8b962015138dde4c9

langref: delete misleading example code about packed structs


1 files changed, 10 insertions(+), 26 deletions(-)

doc/langref/test_structs.zig+10-26
...@@ -6,28 +6,13 @@ const Point = struct {...@@ -6,28 +6,13 @@ const Point = struct {
6 y: f32,6 y: f32,
7};7};
88
9// Maybe we want to pass it to OpenGL so we want to be particular about
10// how the bytes are arranged.
11const Point2 = packed struct {
12 x: f32,
13 y: f32,
14};
15
16// Declare an instance of a struct.9// Declare an instance of a struct.
17const p = Point{10const p: Point = .{
18 .x = 0.12,11 .x = 0.12,
19 .y = 0.34,12 .y = 0.34,
20};13};
2114
22// Maybe we're not ready to fill out some of the fields.15// Functions in the struct's namespace can be called with dot syntax.
23var p2 = Point{
24 .x = 0.12,
25 .y = undefined,
26};
27
28// Structs can have methods
29// Struct methods are not special, they are only namespaced
30// functions that you can call with dot syntax.
31const Vec3 = struct {16const Vec3 = struct {
32 x: f32,17 x: f32,
33 y: f32,18 y: f32,
...@@ -46,7 +31,6 @@ const Vec3 = struct {...@@ -46,7 +31,6 @@ const Vec3 = struct {
46 }31 }
47};32};
4833
49const expect = @import("std").testing.expect;
50test "dot product" {34test "dot product" {
51 const v1 = Vec3.init(1.0, 0.0, 0.0);35 const v1 = Vec3.init(1.0, 0.0, 0.0);
52 const v2 = Vec3.init(0.0, 1.0, 0.0);36 const v2 = Vec3.init(0.0, 1.0, 0.0);
...@@ -67,14 +51,14 @@ test "struct namespaced variable" {...@@ -67,14 +51,14 @@ test "struct namespaced variable" {
67 try expect(Empty.PI == 3.14);51 try expect(Empty.PI == 3.14);
68 try expect(@sizeOf(Empty) == 0);52 try expect(@sizeOf(Empty) == 0);
6953
70 // you can still instantiate an empty struct54 // Empty structs can be instantiated the same as usual.
71 const does_nothing = Empty{};55 const does_nothing: Empty = .{};
7256
73 _ = does_nothing;57 _ = does_nothing;
74}58}
7559
76// struct field order is determined by the compiler for optimal performance.60// Struct field order is determined by the compiler, however, a base pointer
77// however, you can still calculate a struct base pointer given a field pointer:61// can be computed from a field pointer:
78fn setYBasedOnX(x: *f32, y: f32) void {62fn setYBasedOnX(x: *f32, y: f32) void {
79 const point: *Point = @fieldParentPtr("x", x);63 const point: *Point = @fieldParentPtr("x", x);
80 point.y = y;64 point.y = y;
...@@ -88,8 +72,7 @@ test "field parent pointer" {...@@ -88,8 +72,7 @@ test "field parent pointer" {
88 try expect(point.y == 0.9);72 try expect(point.y == 0.9);
89}73}
9074
91// You can return a struct from a function. This is how we do generics75// Structs can be returned from functions.
92// in Zig:
93fn LinkedList(comptime T: type) type {76fn LinkedList(comptime T: type) type {
94 return struct {77 return struct {
95 pub const Node = struct {78 pub const Node = struct {
...@@ -105,8 +88,7 @@ fn LinkedList(comptime T: type) type {...@@ -105,8 +88,7 @@ fn LinkedList(comptime T: type) type {
105}88}
10689
107test "linked list" {90test "linked list" {
108 // Functions called at compile-time are memoized. This means you can91 // Functions called at compile-time are memoized.
109 // do this:
110 try expect(LinkedList(i32) == LinkedList(i32));92 try expect(LinkedList(i32) == LinkedList(i32));
11193
112 const list = LinkedList(i32){94 const list = LinkedList(i32){
...@@ -139,4 +121,6 @@ test "linked list" {...@@ -139,4 +121,6 @@ test "linked list" {
139 // instead of try expect(list2.first.?.*.data == 1234);121 // instead of try expect(list2.first.?.*.data == 1234);
140}122}
141123
124const expect = @import("std").testing.expect;
125
142// test126// test