| ... | @@ -1444,7 +1444,7 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void { | ... | @@ -1444,7 +1444,7 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void { |
| 1444 | | 1444 | |
| 1445 | // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough | 1445 | // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough |
| 1446 | const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); | 1446 | const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); |
| 1447 | var bits = @truncate(uint, value); | 1447 | var bits = @bitCast(uint, value); |
| 1448 | for (buffer) |*b| { | 1448 | for (buffer) |*b| { |
| 1449 | b.* = @truncate(u8, bits); | 1449 | b.* = @truncate(u8, bits); |
| 1450 | bits >>= 8; | 1450 | bits >>= 8; |
| ... | @@ -1464,7 +1464,7 @@ pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void { | ... | @@ -1464,7 +1464,7 @@ pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void { |
| 1464 | | 1464 | |
| 1465 | // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough | 1465 | // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough |
| 1466 | const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); | 1466 | const uint = std.meta.Int(.unsigned, @typeInfo(T).Int.bits); |
| 1467 | var bits = @truncate(uint, value); | 1467 | var bits = @bitCast(uint, value); |
| 1468 | var index: usize = buffer.len; | 1468 | var index: usize = buffer.len; |
| 1469 | while (index != 0) { | 1469 | while (index != 0) { |
| 1470 | index -= 1; | 1470 | index -= 1; |
| ... | @@ -2028,6 +2028,30 @@ fn testWriteIntImpl() !void { | ... | @@ -2028,6 +2028,30 @@ fn testWriteIntImpl() !void { |
| 2028 | 0x00, | 2028 | 0x00, |
| 2029 | 0x00, | 2029 | 0x00, |
| 2030 | })); | 2030 | })); |
| | 2031 | |
| | 2032 | writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Little); |
| | 2033 | try testing.expect(eql(u8, &bytes, &[_]u8{ |
| | 2034 | 0xCD, |
| | 2035 | 0xAB, |
| | 2036 | 0x00, |
| | 2037 | 0x00, |
| | 2038 | 0x00, |
| | 2039 | 0x00, |
| | 2040 | 0x00, |
| | 2041 | 0x00, |
| | 2042 | })); |
| | 2043 | |
| | 2044 | writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Big); |
| | 2045 | try testing.expect(eql(u8, &bytes, &[_]u8{ |
| | 2046 | 0x00, |
| | 2047 | 0x00, |
| | 2048 | 0x00, |
| | 2049 | 0x00, |
| | 2050 | 0x00, |
| | 2051 | 0x00, |
| | 2052 | 0xAB, |
| | 2053 | 0xCD, |
| | 2054 | })); |
| 2031 | } | 2055 | } |
| 2032 | | 2056 | |
| 2033 | /// Returns the smallest number in a slice. O(n). | 2057 | /// Returns the smallest number in a slice. O(n). |