authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-02-23 18:03:50+01:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-02-23 18:03:50+01:00
log7664c3bc11988fd1ee2b3f26d1f08ac80d873c64
tree5470775882ec5d4379fde64ea5eb6cd10d3d0342
parent783e8ad0319b950a1d42d5a93e4fbb9e9df1be10

remove @bytesToSlice, @sliceToBytes from tests, docs


14 files changed, 141 insertions(+), 181 deletions(-)

doc/langref.html.in+11-50
......@@ -2025,7 +2025,8 @@ test "volatile" {
20252025 conversions are not possible.
20262026 </p>
20272027 {#code_begin|test#}
2028const assert = @import("std").debug.assert;
2028const std = @import("std");
2029const assert = std.debug.assert;
20292030
20302031test "pointer casting" {
20312032 const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };
......@@ -2034,7 +2035,7 @@ test "pointer casting" {
20342035
20352036 // Even this example is contrived - there are better ways to do the above than
20362037 // pointer casting. For example, using a slice narrowing cast:
2037 const u32_value = @bytesToSlice(u32, bytes[0..])[0];
2038 const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];
20382039 assert(u32_value == 0x12121212);
20392040
20402041 // And even another way, the most straightforward way to do it:
......@@ -2114,16 +2115,16 @@ test "function alignment" {
21142115 {#link|safety check|Incorrect Pointer Alignment#}:
21152116 </p>
21162117 {#code_begin|test_safety|incorrect alignment#}
2117const assert = @import("std").debug.assert;
2118const std = @import("std");
21182119
21192120test "pointer alignment safety" {
21202121 var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
2121 const bytes = @sliceToBytes(array[0..]);
2122 assert(foo(bytes) == 0x11111111);
2122 const bytes = std.mem.sliceAsBytes(array[0..]);
2123 std.debug.assert(foo(bytes) == 0x11111111);
21232124}
21242125fn foo(bytes: []u8) u32 {
21252126 const slice4 = bytes[1..5];
2126 const int_slice = @bytesToSlice(u32, @alignCast(4, slice4));
2127 const int_slice = std.mem.bytesAsSlice(u32, @alignCast(4, slice4));
21272128 return int_slice[0];
21282129}
21292130 {#code_end#}
......@@ -2249,7 +2250,7 @@ test "slice widening" {
22492250 // Zig supports slice widening and slice narrowing. Cast a slice of u8
22502251 // to a slice of anything else, and Zig will perform the length conversion.
22512252 const array align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13 };
2252 const slice = @bytesToSlice(u32, array[0..]);
2253 const slice = mem.bytesAsSlice(u32, array[0..]);
22532254 assert(slice.len == 2);
22542255 assert(slice[0] == 0x12121212);
22552256 assert(slice[1] == 0x13131313);
......@@ -5186,7 +5187,6 @@ test "coercion of zero bit types" {
51865187 <li>{#link|@bitCast#} - change type but maintain bit representation</li>
51875188 <li>{#link|@alignCast#} - make a pointer have more alignment</li>
51885189 <li>{#link|@boolToInt#} - convert true to 1 and false to 0</li>
5189 <li>{#link|@bytesToSlice#} - convert a slice of bytes to a slice of another type</li>
51905190 <li>{#link|@enumToInt#} - obtain the integer tag value of an enum or tagged union</li>
51915191 <li>{#link|@errSetCast#} - convert to a smaller error set</li>
51925192 <li>{#link|@errorToInt#} - obtain the integer value of an error code</li>
......@@ -5199,7 +5199,6 @@ test "coercion of zero bit types" {
51995199 <li>{#link|@intToPtr#} - convert an address to a pointer</li>
52005200 <li>{#link|@ptrCast#} - convert between pointer types</li>
52015201 <li>{#link|@ptrToInt#} - obtain the address of a pointer</li>
5202 <li>{#link|@sliceToBytes#} - convert a slice of anything to a slice of bytes</li>
52035202 <li>{#link|@truncate#} - convert between integer types, chopping off bits</li>
52045203 </ul>
52055204 {#header_close#}
......@@ -6929,18 +6928,6 @@ async fn func(y: *i32) void {
69296928 {#see_also|@bitOffsetOf#}
69306929 {#header_close#}
69316930
6932 {#header_open|@bytesToSlice#}
6933 <pre>{#syntax#}@bytesToSlice(comptime Element: type, bytes: []u8) []Element{#endsyntax#}</pre>
6934 <p>
6935 Converts a slice of bytes or array of bytes into a slice of {#syntax#}Element{#endsyntax#}.
6936 The resulting slice has the same {#link|pointer|Pointers#} properties as the parameter.
6937 </p>
6938 <p>
6939 Attempting to convert a number of bytes with a length that does not evenly divide into a slice of
6940 elements results in safety-protected {#link|Undefined Behavior#}.
6941 </p>
6942 {#header_close#}
6943
69446931 {#header_open|@call#}
69456932 <pre>{#syntax#}@call(options: std.builtin.CallOptions, function: var, args: var) var{#endsyntax#}</pre>
69466933 <p>
......@@ -8101,14 +8088,6 @@ test "@setRuntimeSafety" {
81018088 {#see_also|@bitSizeOf|@typeInfo#}
81028089 {#header_close#}
81038090
8104 {#header_open|@sliceToBytes#}
8105 <pre>{#syntax#}@sliceToBytes(value: var) []u8{#endsyntax#}</pre>
8106 <p>
8107 Converts a slice or array to a slice of {#syntax#}u8{#endsyntax#}. The resulting slice has the same
8108 {#link|pointer|Pointers#} properties as the parameter.
8109 </p>
8110 {#header_close#}
8111
81128091 {#header_open|@splat#}
81138092 <pre>{#syntax#}@splat(comptime len: u32, scalar: var) @Vector(len, @TypeOf(scalar)){#endsyntax#}</pre>
81148093 <p>
......@@ -8919,25 +8898,6 @@ pub fn main() void {
89198898 var b: u32 = 3;
89208899 var c = @divExact(a, b);
89218900 std.debug.warn("value: {}\n", .{c});
8922}
8923 {#code_end#}
8924 {#header_close#}
8925 {#header_open|Slice Widen Remainder#}
8926 <p>At compile-time:</p>
8927 {#code_begin|test_err|unable to convert#}
8928comptime {
8929 var bytes = [5]u8{ 1, 2, 3, 4, 5 };
8930 var slice = @bytesToSlice(u32, bytes[0..]);
8931}
8932 {#code_end#}
8933 <p>At runtime:</p>
8934 {#code_begin|exe_err#}
8935const std = @import("std");
8936
8937pub fn main() void {
8938 var bytes = [5]u8{ 1, 2, 3, 4, 5 };
8939 var slice = @bytesToSlice(u32, bytes[0..]);
8940 std.debug.warn("value: {}\n", .{slice[0]});
89418901}
89428902 {#code_end#}
89438903 {#header_close#}
......@@ -9119,14 +9079,15 @@ comptime {
91199079 {#code_end#}
91209080 <p>At runtime:</p>
91219081 {#code_begin|exe_err#}
9082const mem = @import("std").mem;
91229083pub fn main() !void {
91239084 var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
9124 const bytes = @sliceToBytes(array[0..]);
9085 const bytes = mem.sliceAsBytes(array[0..]);
91259086 if (foo(bytes) != 0x11111111) return error.Wrong;
91269087}
91279088fn foo(bytes: []u8) u32 {
91289089 const slice4 = bytes[1..5];
9129 const int_slice = @bytesToSlice(u32, @alignCast(4, slice4));
9090 const int_slice = mem.bytesAsSlice(u32, @alignCast(4, slice4));
91309091 return int_slice[0];
91319092}
91329093 {#code_end#}
lib/std/mem.zig+100-14
......@@ -1488,28 +1488,34 @@ test "bytesToValue" {
14881488
14891489//TODO copy also is_volatile, etc. I tried to use @typeInfo, modify child type, use @Type, but ran into issues.
14901490fn BytesAsSliceReturnType(comptime T: type, comptime bytesType: type) type {
1491 if (comptime !trait.isSlice(bytesType) or comptime meta.Child(bytesType) != u8) {
1492 @compileError("expected []u8, passed " ++ @typeName(bytesType));
1491 if (!(trait.isSlice(bytesType) and meta.Child(bytesType) == u8) and !(trait.isPtrTo(.Array)(bytesType) and meta.Child(meta.Child(bytesType)) == u8)) {
1492 @compileError("expected []u8 or *[_]u8, passed " ++ @typeName(bytesType));
14931493 }
14941494
1495 const alignment = comptime meta.alignment(bytesType);
1495 if (trait.isPtrTo(.Array)(bytesType) and @typeInfo(meta.Child(bytesType)).Array.len % @sizeOf(T) != 0) {
1496 @compileError("number of bytes in " ++ @typeName(bytesType) ++ " is not divisible by size of " ++ @typeName(T));
1497 }
14961498
1497 return if (comptime trait.isConstPtr(bytesType)) []align(alignment) const T else []align(alignment) T;
1499 const alignment = meta.alignment(bytesType);
1500
1501 return if (trait.isConstPtr(bytesType)) []align(alignment) const T else []align(alignment) T;
14981502}
14991503
15001504pub fn bytesAsSlice(comptime T: type, bytes: var) BytesAsSliceReturnType(T, @TypeOf(bytes)) {
1505 const bytesSlice = if (comptime trait.isPtrTo(.Array)(@TypeOf(bytes))) bytes[0..] else bytes;
1506
15011507 // let's not give an undefined pointer to @ptrCast
15021508 // it may be equal to zero and fail a null check
1503 if (bytes.len == 0) {
1509 if (bytesSlice.len == 0) {
15041510 return &[0]T{};
15051511 }
15061512
1507 const bytesType = @TypeOf(bytes);
1513 const bytesType = @TypeOf(bytesSlice);
15081514 const alignment = comptime meta.alignment(bytesType);
15091515
15101516 const castTarget = if (comptime trait.isConstPtr(bytesType)) [*]align(alignment) const T else [*]align(alignment) T;
15111517
1512 return @ptrCast(castTarget, bytes.ptr)[0..@divExact(bytes.len, @sizeOf(T))];
1518 return @ptrCast(castTarget, bytesSlice.ptr)[0..@divExact(bytes.len, @sizeOf(T))];
15131519}
15141520
15151521test "bytesAsSlice" {
......@@ -1520,30 +1526,59 @@ test "bytesAsSlice" {
15201526 testing.expect(bigToNative(u16, slice[1]) == 0xBEEF);
15211527}
15221528
1529test "bytesAsSlice keeps pointer alignment" {
1530 var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 };
1531 const numbers = bytesAsSlice(u32, bytes[0..]);
1532 comptime testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32);
1533}
1534
1535test "bytesAsSlice on a packed struct" {
1536 const F = packed struct {
1537 a: u8,
1538 };
1539
1540 var b = [1]u8{9};
1541 var f = bytesAsSlice(F, &b);
1542 testing.expect(f[0].a == 9);
1543}
1544
1545test "bytesAsSlice with specified alignment" {
1546 var bytes align(4) = [_]u8{
1547 0x33,
1548 0x33,
1549 0x33,
1550 0x33,
1551 };
1552 const slice: []u32 = std.mem.bytesAsSlice(u32, bytes[0..]);
1553 testing.expect(slice[0] == 0x33333333);
1554}
1555
15231556//TODO copy also is_volatile, etc. I tried to use @typeInfo, modify child type, use @Type, but ran into issues.
15241557fn SliceAsBytesReturnType(comptime sliceType: type) type {
1525 if (comptime !trait.isSlice(sliceType)) {
1526 @compileError("expected []T, passed " ++ @typeName(sliceType));
1558 if (!trait.isSlice(sliceType) and !trait.isPtrTo(.Array)(sliceType)) {
1559 @compileError("expected []T or *[_]T, passed " ++ @typeName(sliceType));
15271560 }
15281561
1529 const alignment = comptime meta.alignment(sliceType);
1562 const alignment = meta.alignment(sliceType);
15301563
1531 return if (comptime trait.isConstPtr(sliceType)) []align(alignment) const u8 else []align(alignment) u8;
1564 return if (trait.isConstPtr(sliceType)) []align(alignment) const u8 else []align(alignment) u8;
15321565}
15331566
15341567pub fn sliceAsBytes(slice: var) SliceAsBytesReturnType(@TypeOf(slice)) {
1568 const actualSlice = if (comptime trait.isPtrTo(.Array)(@TypeOf(slice))) slice[0..] else slice;
1569
15351570 // let's not give an undefined pointer to @ptrCast
15361571 // it may be equal to zero and fail a null check
1537 if (slice.len == 0) {
1572 if (actualSlice.len == 0) {
15381573 return &[0]u8{};
15391574 }
15401575
1541 const sliceType = @TypeOf(slice);
1576 const sliceType = @TypeOf(actualSlice);
15421577 const alignment = comptime meta.alignment(sliceType);
15431578
15441579 const castTarget = if (comptime trait.isConstPtr(sliceType)) [*]align(alignment) const u8 else [*]align(alignment) u8;
15451580
1546 return @ptrCast(castTarget, slice.ptr)[0 .. slice.len * @sizeOf(comptime meta.Child(sliceType))];
1581 return @ptrCast(castTarget, actualSlice.ptr)[0 .. actualSlice.len * @sizeOf(comptime meta.Child(sliceType))];
15471582}
15481583
15491584test "sliceAsBytes" {
......@@ -1556,6 +1591,57 @@ test "sliceAsBytes" {
15561591 }));
15571592}
15581593
1594test "sliceAsBytes packed struct at runtime and comptime" {
1595 const Foo = packed struct {
1596 a: u4,
1597 b: u4,
1598 };
1599 const S = struct {
1600 fn doTheTest() void {
1601 var foo: Foo = undefined;
1602 var slice = sliceAsBytes(@as(*[1]Foo, &foo)[0..1]);
1603 slice[0] = 0x13;
1604 switch (builtin.endian) {
1605 .Big => {
1606 testing.expect(foo.a == 0x1);
1607 testing.expect(foo.b == 0x3);
1608 },
1609 .Little => {
1610 testing.expect(foo.a == 0x3);
1611 testing.expect(foo.b == 0x1);
1612 },
1613 }
1614 }
1615 };
1616 S.doTheTest();
1617 comptime S.doTheTest();
1618}
1619
1620test "sliceAsBytes and bytesAsSlice back" {
1621 testing.expect(@sizeOf(i32) == 4);
1622
1623 var big_thing_array = [_]i32{ 1, 2, 3, 4 };
1624 const big_thing_slice: []i32 = big_thing_array[0..];
1625
1626 const bytes = sliceAsBytes(big_thing_slice);
1627 testing.expect(bytes.len == 4 * 4);
1628
1629 bytes[4] = 0;
1630 bytes[5] = 0;
1631 bytes[6] = 0;
1632 bytes[7] = 0;
1633 testing.expect(big_thing_slice[1] == 0);
1634
1635 const big_thing_again = bytesAsSlice(i32, bytes);
1636 testing.expect(big_thing_again[2] == 3);
1637
1638 big_thing_again[2] = -1;
1639 testing.expect(bytes[8] == math.maxInt(u8));
1640 testing.expect(bytes[9] == math.maxInt(u8));
1641 testing.expect(bytes[10] == math.maxInt(u8));
1642 testing.expect(bytes[11] == math.maxInt(u8));
1643}
1644
15591645fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type {
15601646 if (trait.isConstPtr(T))
15611647 return *const [length]meta.Child(meta.Child(T));
lib/std/meta/trait.zig+16
......@@ -135,6 +135,22 @@ test "std.meta.trait.isPtrTo" {
135135 testing.expect(!isPtrTo(.Struct)(**struct {}));
136136}
137137
138pub fn isSliceOf(comptime id: builtin.TypeId) TraitFn {
139 const Closure = struct {
140 pub fn trait(comptime T: type) bool {
141 if (!comptime isSlice(T)) return false;
142 return id == @typeId(meta.Child(T));
143 }
144 };
145 return Closure.trait;
146}
147
148test "std.meta.trait.isSliceOf" {
149 testing.expect(!isSliceOf(.Struct)(struct {}));
150 testing.expect(isSliceOf(.Struct)([]struct {}));
151 testing.expect(!isSliceOf(.Struct)([][]struct {}));
152}
153
138154///////////Strait trait Fns
139155
140156//@TODO:
test/compile_errors.zig-10
......@@ -4747,16 +4747,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
47474747 "tmp.zig:1:15: error: comptime parameter not allowed in function with calling convention 'C'",
47484748 });
47494749
4750 cases.add("convert fixed size array to slice with invalid size",
4751 \\export fn f() void {
4752 \\ var array: [5]u8 = undefined;
4753 \\ var foo = @bytesToSlice(u32, &array)[0];
4754 \\}
4755 , &[_][]const u8{
4756 "tmp.zig:3:15: error: unable to convert [5]u8 to []align(1) u32: size mismatch",
4757 "tmp.zig:3:29: note: u32 has size 4; remaining bytes: 1",
4758 });
4759
47604750 cases.add("non-pure function returns type",
47614751 \\var a: u32 = 0;
47624752 \\pub fn List(comptime T: type) type {
test/runtime_safety.zig+9-7
......@@ -553,15 +553,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
553553 );
554554
555555 cases.addRuntimeSafety("cast []u8 to bigger slice of wrong size",
556 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
557 \\ @import("std").os.exit(126);
556 \\const std = @import("std");
557 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
558 \\ std.os.exit(126);
558559 \\}
559560 \\pub fn main() !void {
560561 \\ const x = widenSlice(&[_]u8{1, 2, 3, 4, 5});
561562 \\ if (x.len == 0) return error.Whatever;
562563 \\}
563564 \\fn widenSlice(slice: []align(1) const u8) []align(1) const i32 {
564 \\ return @bytesToSlice(i32, slice);
565 \\ return std.mem.bytesAsSlice(i32, slice);
565566 \\}
566567 );
567568
......@@ -656,17 +657,18 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
656657 );
657658
658659 cases.addRuntimeSafety("@alignCast misaligned",
659 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
660 \\ @import("std").os.exit(126);
660 \\const std = @import("std");
661 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
662 \\ std.os.exit(126);
661663 \\}
662664 \\pub fn main() !void {
663665 \\ var array align(4) = [_]u32{0x11111111, 0x11111111};
664 \\ const bytes = @sliceToBytes(array[0..]);
666 \\ const bytes = std.mem.sliceAsBytes(array[0..]);
665667 \\ if (foo(bytes) != 0x11111111) return error.Wrong;
666668 \\}
667669 \\fn foo(bytes: []u8) u32 {
668670 \\ const slice4 = bytes[1..5];
669 \\ const int_slice = @bytesToSlice(u32, @alignCast(4, slice4));
671 \\ const int_slice = std.mem.bytesAsSlice(u32, @alignCast(4, slice4));
670672 \\ return int_slice[0];
671673 \\}
672674 );
test/stage1/behavior.zig-1
......@@ -92,7 +92,6 @@ comptime {
9292 _ = @import("behavior/shuffle.zig");
9393 _ = @import("behavior/sizeof_and_typeof.zig");
9494 _ = @import("behavior/slice.zig");
95 _ = @import("behavior/slicetobytes.zig");
9695 _ = @import("behavior/struct.zig");
9796 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
9897 _ = @import("behavior/struct_contains_slice_of_itself.zig");
test/stage1/behavior/align.zig-14
......@@ -81,20 +81,6 @@ fn testBytesAlign(b: u8) void {
8181 expect(ptr.* == 0x33333333);
8282}
8383
84test "specifying alignment allows slice cast" {
85 testBytesAlignSlice(0x33);
86}
87fn testBytesAlignSlice(b: u8) void {
88 var bytes align(4) = [_]u8{
89 b,
90 b,
91 b,
92 b,
93 };
94 const slice: []u32 = @bytesToSlice(u32, bytes[0..]);
95 expect(slice[0] == 0x33333333);
96}
97
9884test "@alignCast pointers" {
9985 var x: u32 align(4) = 1;
10086 expectsOnly1(&x);
test/stage1/behavior/async_fn.zig+2-2
......@@ -334,7 +334,7 @@ test "async fn with inferred error set" {
334334 var frame: [1]@Frame(middle) = undefined;
335335 var fn_ptr = middle;
336336 var result: @TypeOf(fn_ptr).ReturnType.ErrorSet!void = undefined;
337 _ = @asyncCall(@sliceToBytes(frame[0..]), &result, fn_ptr);
337 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, fn_ptr);
338338 resume global_frame;
339339 std.testing.expectError(error.Fail, result);
340340 }
......@@ -954,7 +954,7 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
954954 fn doTheTest() void {
955955 var frame: [1]@Frame(middle) = undefined;
956956 var result: @TypeOf(middle).ReturnType.ErrorSet!void = undefined;
957 _ = @asyncCall(@sliceToBytes(frame[0..]), &result, middle);
957 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, middle);
958958 resume global_frame;
959959 std.testing.expectError(error.Fail, result);
960960 }
test/stage1/behavior/bugs/1851.zig+1-1
......@@ -13,7 +13,7 @@ test "allocation and looping over 3-byte integer" {
1313 x[0] = 0xFFFFFF;
1414 x[1] = 0xFFFFFF;
1515
16 const bytes = @sliceToBytes(x);
16 const bytes = std.mem.sliceAsBytes(x);
1717 expect(@TypeOf(bytes) == []align(4) u8);
1818 expect(bytes.len == 8);
1919
test/stage1/behavior/cast.zig-20
......@@ -304,20 +304,6 @@ fn cast128Float(x: u128) f128 {
304304 return @bitCast(f128, x);
305305}
306306
307test "const slice widen cast" {
308 const bytes align(4) = [_]u8{
309 0x12,
310 0x12,
311 0x12,
312 0x12,
313 };
314
315 const u32_value = @bytesToSlice(u32, bytes[0..])[0];
316 expect(u32_value == 0x12121212);
317
318 expect(@bitCast(u32, bytes) == 0x12121212);
319}
320
321307test "single-item pointer of array to slice and to unknown length pointer" {
322308 testCastPtrOfArrayToSliceAndPtr();
323309 comptime testCastPtrOfArrayToSliceAndPtr();
......@@ -392,12 +378,6 @@ test "comptime_int @intToFloat" {
392378 }
393379}
394380
395test "@bytesToSlice keeps pointer alignment" {
396 var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 };
397 const numbers = @bytesToSlice(u32, bytes[0..]);
398 comptime expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32);
399}
400
401381test "@intCast i32 to u7" {
402382 var x: u128 = maxInt(u128);
403383 var y: i32 = 120;
test/stage1/behavior/eval.zig-10
......@@ -711,16 +711,6 @@ test "bit shift a u1" {
711711 expect(y == 1);
712712}
713713
714test "@bytesToslice on a packed struct" {
715 const F = packed struct {
716 a: u8,
717 };
718
719 var b = [1]u8{9};
720 var f = @bytesToSlice(F, &b);
721 expect(f[0].a == 9);
722}
723
724714test "comptime pointer cast array and then slice" {
725715 const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
726716
test/stage1/behavior/misc.zig-21
......@@ -3,7 +3,6 @@ const expect = std.testing.expect;
33const expectEqualSlices = std.testing.expectEqualSlices;
44const mem = std.mem;
55const builtin = @import("builtin");
6const maxInt = std.math.maxInt;
76
87// normal comment
98
......@@ -377,26 +376,6 @@ test "string concatenation" {
377376 expect(b[len] == 0);
378377}
379378
380test "cast slice to u8 slice" {
381 expect(@sizeOf(i32) == 4);
382 var big_thing_array = [_]i32{ 1, 2, 3, 4 };
383 const big_thing_slice: []i32 = big_thing_array[0..];
384 const bytes = @sliceToBytes(big_thing_slice);
385 expect(bytes.len == 4 * 4);
386 bytes[4] = 0;
387 bytes[5] = 0;
388 bytes[6] = 0;
389 bytes[7] = 0;
390 expect(big_thing_slice[1] == 0);
391 const big_thing_again = @bytesToSlice(i32, bytes);
392 expect(big_thing_again[2] == 3);
393 big_thing_again[2] = -1;
394 expect(bytes[8] == maxInt(u8));
395 expect(bytes[9] == maxInt(u8));
396 expect(bytes[10] == maxInt(u8));
397 expect(bytes[11] == maxInt(u8));
398}
399
400379test "pointer to void return type" {
401380 testPointerToVoidReturnType() catch unreachable;
402381}
test/stage1/behavior/slicetobytes.zig deleted-29
......@@ -1,29 +0,0 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const expect = std.testing.expect;
4
5test "@sliceToBytes packed struct at runtime and comptime" {
6 const Foo = packed struct {
7 a: u4,
8 b: u4,
9 };
10 const S = struct {
11 fn doTheTest() void {
12 var foo: Foo = undefined;
13 var slice = @sliceToBytes(@as(*[1]Foo, &foo)[0..1]);
14 slice[0] = 0x13;
15 switch (builtin.endian) {
16 builtin.Endian.Big => {
17 expect(foo.a == 0x1);
18 expect(foo.b == 0x3);
19 },
20 builtin.Endian.Little => {
21 expect(foo.a == 0x3);
22 expect(foo.b == 0x1);
23 },
24 }
25 }
26 };
27 S.doTheTest();
28 comptime S.doTheTest();
29}
test/stage1/behavior/struct.zig+2-2
......@@ -315,7 +315,7 @@ test "packed array 24bits" {
315315
316316 var bytes = [_]u8{0} ** (@sizeOf(FooArray24Bits) + 1);
317317 bytes[bytes.len - 1] = 0xaa;
318 const ptr = &@bytesToSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
318 const ptr = &std.mem.bytesAsSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0];
319319 expect(ptr.a == 0);
320320 expect(ptr.b[0].field == 0);
321321 expect(ptr.b[1].field == 0);
......@@ -364,7 +364,7 @@ test "aligned array of packed struct" {
364364 }
365365
366366 var bytes = [_]u8{0xbb} ** @sizeOf(FooArrayOfAligned);
367 const ptr = &@bytesToSlice(FooArrayOfAligned, bytes[0..bytes.len])[0];
367 const ptr = &std.mem.bytesAsSlice(FooArrayOfAligned, bytes[0..])[0];
368368
369369 expect(ptr.a[0].a == 0xbb);
370370 expect(ptr.a[0].b == 0xbb);