@@ -1456,8 +1456,7 @@ test "pointer array access" {
14561456 // Taking an address of an individual element gives a
14571457 // pointer to a single item. This kind of pointer
14581458 // does not support pointer arithmetic.
1459-
1460- var array = []u8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1459+ var array = []u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
14611460 const ptr = &array[2];
14621461 assert(@typeOf(ptr) == *u8);
14631462
@@ -1469,7 +1468,7 @@ test "pointer array access" {
14691468test "pointer slicing" {
14701469 // In Zig, we prefer using slices over null-terminated pointers.
14711470 // You can turn an array into a slice using slice syntax:
1472- var array = []u8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1471+ var array = []u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
14731472 const slice = array[2..4];
14741473 assert(slice.len == 2);
14751474
@@ -1541,13 +1540,13 @@ test "pointer casting" {
15411540 // To convert one pointer type to another, use @ptrCast. This is an unsafe
15421541 // operation that Zig cannot protect you against. Use @ptrCast only when other
15431542 // conversions are not possible.
1544- const bytes align(@alignOf(u32)) = []u8{0x12, 0x12, 0x12, 0x12};
1543+ const bytes align(@alignOf(u32)) = []u8{ 0x12, 0x12, 0x12, 0x12 };
15451544 const u32_ptr = @ptrCast(*const u32, &bytes[0]);
15461545 assert(u32_ptr.* == 0x12121212);
15471546
15481547 // Even this example is contrived - there are better ways to do the above than
15491548 // pointer casting. For example, using a slice narrowing cast:
1550- const u32_value = ([]const u32)( bytes[0..])[0];
1549+ const u32_value = @bytesToSlice( u32, bytes[0..])[0];
15511550 assert(u32_value == 0x12121212);
15521551
15531552 // And even another way, the most straightforward way to do it:
@@ -1630,13 +1629,13 @@ test "function alignment" {
16301629const assert = @import("std").debug.assert;
16311630
16321631test "pointer alignment safety" {
1633- var array align(4) = []u32{0x11111111, 0x11111111};
1634- const bytes = ([]u8) (array[0..]);
1632+ var array align(4) = []u32{ 0x11111111, 0x11111111 };
1633+ const bytes = @sliceToBytes (array[0..]);
16351634 assert(foo(bytes) == 0x11111111);
16361635}
16371636fn foo(bytes: []u8) u32 {
16381637 const slice4 = bytes[1..5];
1639- const int_slice = ([] u32)( @alignCast(4, slice4));
1638+ const int_slice = @bytesToSlice( u32, @alignCast(4, slice4));
16401639 return int_slice[0];
16411640}
16421641 {#code_end#}
@@ -1728,8 +1727,8 @@ test "slice pointer" {
17281727test "slice widening" {
17291728 // Zig supports slice widening and slice narrowing. Cast a slice of u8
17301729 // to a slice of anything else, and Zig will perform the length conversion.
1731- const array align(@alignOf(u32)) = []u8{0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13};
1732- const slice = ([]const u32)( array[0..]);
1730+ const array align(@alignOf(u32)) = []u8{ 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13 };
1731+ const slice = @bytesToSlice( u32, array[0..]);
17331732 assert(slice.len == 2);
17341733 assert(slice[0] == 0x12121212);
17351734 assert(slice[1] == 0x13131313);
@@ -1901,9 +1900,9 @@ const Value = enum(u2) {
19011900// Now you can cast between u2 and Value.
19021901// The ordinal value starts from 0, counting up for each member.
19031902test "enum ordinal value" {
1904- assert(u2 (Value.Zero) == 0);
1905- assert(u2 (Value.One) == 1);
1906- assert(u2 (Value.Two) == 2);
1903+ assert(@enumToInt (Value.Zero) == 0);
1904+ assert(@enumToInt (Value.One) == 1);
1905+ assert(@enumToInt (Value.Two) == 2);
19071906}
19081907
19091908// You can override the ordinal value for an enum.
@@ -1913,9 +1912,9 @@ const Value2 = enum(u32) {
19131912 Million = 1000000,
19141913};
19151914test "set enum ordinal value" {
1916- assert(u32 (Value2.Hundred) == 100);
1917- assert(u32 (Value2.Thousand) == 1000);
1918- assert(u32 (Value2.Million) == 1000000);
1915+ assert(@enumToInt (Value2.Hundred) == 100);
1916+ assert(@enumToInt (Value2.Thousand) == 1000);
1917+ assert(@enumToInt (Value2.Million) == 1000000);
19191918}
19201919
19211920// Enums can have methods, the same as structs and unions.
@@ -4651,6 +4650,18 @@ comptime {
46514650 </p>
46524651 {#header_close#}
46534652
4653+ {#header_open|@bytesToSlice#}
4654+ <pre><code class="zig">@bytesToSlice(comptime Element: type, bytes: []u8) []Element</code></pre>
4655+ <p>
4656+ Converts a slice of bytes or array of bytes into a slice of <code>Element</code>.
4657+ The resulting slice has the same {#link|pointer|Pointers#} properties as the parameter.
4658+ </p>
4659+ <p>
4660+ Attempting to convert a number of bytes with a length that does not evenly divide into a slice of
4661+ elements results in safety-protected {#link|Undefined Behavior#}.
4662+ </p>
4663+ {#header_close#}
4664+
46544665 {#header_open|@cDefine#}
46554666 <pre><code class="zig">@cDefine(comptime name: []u8, value)</code></pre>
46564667 <p>
@@ -4919,12 +4930,23 @@ test "main" {
49194930 </p>
49204931 {#see_also|@import#}
49214932 {#header_close#}
4922- {#header_open|@export#}
4923- <pre><code class="zig">@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) []const u8</code></pre>
4933+
4934+ {#header_open|@enumToInt#}
4935+ <pre><code class="zig">@enumToInt(enum_value: var) var</code></pre>
49244936 <p>
4925- Creates a symbol in the output object file.
4937+ Converts an enumeration value into its integer tag type.
4938+ </p>
4939+ {#see_also|@intToEnum#}
4940+ {#header_close#}
4941+
4942+ {#header_open|@errSetCast#}
4943+ <pre><code class="zig">@errSetCast(comptime T: DestType, value: var) DestType</code></pre>
4944+ <p>
4945+ Converts an error value from one error set to another error set. Attempting to convert an error
4946+ which is not in the destination error set results in safety-protected {#link|Undefined Behavior#}.
49264947 </p>
49274948 {#header_close#}
4949+
49284950 {#header_open|@errorName#}
49294951 <pre><code class="zig">@errorName(err: error) []u8</code></pre>
49304952 <p>
@@ -4941,6 +4963,7 @@ test "main" {
49414963 error name table will be generated.
49424964 </p>
49434965 {#header_close#}
4966+
49444967 {#header_open|@errorReturnTrace#}
49454968 <pre><code class="zig">@errorReturnTrace() ?*builtin.StackTrace</code></pre>
49464969 <p>
@@ -4949,6 +4972,33 @@ test "main" {
49494972 stack trace object. Otherwise returns `null`.
49504973 </p>
49514974 {#header_close#}
4975+
4976+ {#header_open|@errorToInt#}
4977+ <pre><code class="zig">@errorToInt(err: var) @IntType(false, @sizeOf(error) * 8)</code></pre>
4978+ <p>
4979+ Supports the following types:
4980+ </p>
4981+ <ul>
4982+ <li>error unions</li>
4983+ <li><code>E!void</code></li>
4984+ </ul>
4985+ <p>
4986+ Converts an error to the integer representation of an error.
4987+ </p>
4988+ <p>
4989+ It is generally recommended to avoid this
4990+ cast, as the integer representation of an error is not stable across source code changes.
4991+ </p>
4992+ {#see_also|@intToError#}
4993+ {#header_close#}
4994+
4995+ {#header_open|@export#}
4996+ <pre><code class="zig">@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) []const u8</code></pre>
4997+ <p>
4998+ Creates a symbol in the output object file.
4999+ </p>
5000+ {#header_close#}
5001+
49525002 {#header_open|@fence#}
49535003 <pre><code class="zig">@fence(order: AtomicOrder)</code></pre>
49545004 <p>
@@ -5049,8 +5099,36 @@ fn add(a: i32, b: i32) i32 { return a + b; }
50495099 <p>
50505100 Converts an integer to another integer while keeping the same numerical value.
50515101 Attempting to convert a number which is out of range of the destination type results in
5052- {#link|Undefined Behavior#}.
5102+ safety-protected {#link|Undefined Behavior#}.
5103+ </p>
5104+ {#header_close#}
5105+
5106+ {#header_open|@intToEnum#}
5107+ <pre><code class="zig">@intToEnum(comptime DestType: type, int_value: @TagType(DestType)) DestType</code></pre>
5108+ <p>
5109+ Converts an integer into an {#link|enum#} value.
5110+ </p>
5111+ <p>
5112+ Attempting to convert an integer which represents no value in the chosen enum type invokes
5113+ safety-checked {#link|Undefined Behavior#}.
5114+ </p>
5115+ {#see_also|@enumToInt#}
5116+ {#header_close#}
5117+
5118+ {#header_open|@intToError#}
5119+ <pre><code class="zig">@intToError(value: @IntType(false, @sizeOf(error) * 8)) error</code></pre>
5120+ <p>
5121+ Converts from the integer representation of an error into the global error set type.
50535122 </p>
5123+ <p>
5124+ It is generally recommended to avoid this
5125+ cast, as the integer representation of an error is not stable across source code changes.
5126+ </p>
5127+ <p>
5128+ Attempting to convert an integer that does not correspond to any error results in
5129+ safety-protected {#link|Undefined Behavior#}.
5130+ </p>
5131+ {#see_also|@errorToInt#}
50545132 {#header_close#}
50555133
50565134 {#header_open|@intToFloat#}
@@ -5456,15 +5534,25 @@ pub const FloatMode = enum {
54565534 </p>
54575535 {#see_also|@shlExact|@shlWithOverflow#}
54585536 {#header_close#}
5537+
54595538 {#header_open|@sizeOf#}
5460- <pre><code class="zig">@sizeOf(comptime T: type) (number literal) </code></pre>
5539+ <pre><code class="zig">@sizeOf(comptime T: type) comptime_int </code></pre>
54615540 <p>
54625541 This function returns the number of bytes it takes to store <code>T</code> in memory.
54635542 </p>
54645543 <p>
54655544 The result is a target-specific compile time constant.
54665545 </p>
54675546 {#header_close#}
5547+
5548+ {#header_open|@sliceToBytes#}
5549+ <pre><code class="zig">@sliceToBytes(value: var) []u8</code></pre>
5550+ <p>
5551+ Converts a slice or array to a slice of <code>u8</code>. The resulting slice has the same
5552+ {#link|pointer|Pointers#} properties as the parameter.
5553+ </p>
5554+ {#header_close#}
5555+
54685556 {#header_open|@sqrt#}
54695557 <pre><code class="zig">@sqrt(comptime T: type, value: T) T</code></pre>
54705558 <p>
@@ -5817,10 +5905,10 @@ pub fn build(b: &Builder) void {
58175905 {#header_open|Undefined Behavior#}
58185906 <p>
58195907 Zig has many instances of undefined behavior. If undefined behavior is
5820- detected at compile-time, Zig emits an error. Most undefined behavior that
5821- cannot be detected at compile-time can be detected at runtime. In these cases,
5822- Zig has safety checks. Safety checks can be disabled on a per-block basis
5823- with {#link|setRuntimeSafety#}. The {#link|ReleaseFast#}
5908+ detected at compile-time, Zig emits a compile error and refuses to continue.
5909+ Most undefined behavior that cannot be detected at compile-time can be detected
5910+ at runtime. In these cases, Zig has safety checks. Safety checks can be disabled
5911+ on a per-block basis with {#link|setRuntimeSafety#}. The {#link|ReleaseFast#}
58245912 build mode disables all safety checks in order to facilitate optimizations.
58255913 </p>
58265914 <p>
@@ -6091,8 +6179,8 @@ fn getNumberOrFail() !i32 {
60916179 {#code_begin|test_err|integer value 11 represents no error#}
60926180comptime {
60936181 const err = error.AnError;
6094- const number = u32 (err) + 10;
6095- const invalid_err = error (number);
6182+ const number = @errorToInt (err) + 10;
6183+ const invalid_err = @intToError (number);
60966184}
60976185 {#code_end#}
60986186 <p>At runtime crashes with the message <code>invalid error code</code> and a stack trace.</p>
@@ -6101,6 +6189,11 @@ comptime {
61016189 <p>TODO</p>
61026190
61036191 {#header_close#}
6192+
6193+ {#header_open|Invalid Error Set Cast#}
6194+ <p>TODO</p>
6195+ {#header_close#}
6196+
61046197 {#header_open|Incorrect Pointer Alignment#}
61056198 <p>TODO</p>
61066199
@@ -6109,6 +6202,7 @@ comptime {
61096202 <p>TODO</p>
61106203
61116204 {#header_close#}
6205+
61126206 {#header_close#}
61136207 {#header_open|Memory#}
61146208 <p>TODO: explain no default allocator in zig</p>
@@ -6793,7 +6887,7 @@ hljs.registerLanguage("zig", function(t) {
67936887 a = t.IR + "\\s*\\(",
67946888 c = {
67956889 keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong resume cancel await async orelse",
6796- built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast intCast floatCast intToFloat floatToInt boolToInt bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall",
6890+ built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast intCast floatCast intToFloat floatToInt boolToInt bytesToSlice sliceToBytes errSetCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall errorToInt intToError enumToInt intToEnum ",
67976891 literal: "true false null undefined"
67986892 },
67996893 n = [e, t.CLCM, t.CBCM, s, r];
0 commit comments