authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-07-12 17:39:25+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-12 21:55:49+00:00
log5b570bceb531656c320684fbe42a87feff710a3b
tree97748b857cab92c502886099b60bc01e8ea34607
parent7adbc1140373548f2985c8054b540cc5648f6857

document a few functions in std.mem


1 files changed, 12 insertions(+), 3 deletions(-)

lib/std/mem.zig+12-3
......@@ -8,6 +8,7 @@ const meta = std.meta;
88const trait = meta.trait;
99const testing = std.testing;
1010
11// https://github.com/ziglang/zig/issues/2564
1112pub const page_size = switch (builtin.arch) {
1213 .wasm32, .wasm64 => 64 * 1024,
1314 else => 4 * 1024,
......@@ -518,6 +519,7 @@ pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {
518519 }
519520}
520521
522/// Sets all elements of `dest` to `value`.
521523pub fn set(comptime T: type, dest: []T, value: T) void {
522524 for (dest) |*d|
523525 d.* = value;
......@@ -675,6 +677,8 @@ test "mem.zeroes" {
675677 }
676678}
677679
680/// Sets a slice to zeroes.
681/// Prevents the store from being optimized out.
678682pub fn secureZero(comptime T: type, s: []T) void {
679683 // NOTE: We do not use a volatile slice cast here since LLVM cannot
680684 // see that it can be replaced by a memset.
......@@ -767,6 +771,7 @@ test "zeroInit" {
767771 });
768772}
769773
774/// Compares two slices of numbers lexicographically. O(n).
770775pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
771776 const n = math.min(lhs.len, rhs.len);
772777 var i: usize = 0;
......@@ -1904,6 +1909,8 @@ fn testWriteIntImpl() void {
19041909 }));
19051910}
19061911
1912/// Returns the smallest number in a slice. O(n).
1913/// `slice` must not be empty.
19071914pub fn min(comptime T: type, slice: []const T) T {
19081915 var best = slice[0];
19091916 for (slice[1..]) |item| {
......@@ -1916,6 +1923,8 @@ test "mem.min" {
19161923 testing.expect(min(u8, "abcdefg") == 'a');
19171924}
19181925
1926/// Returns the largest number in a slice. O(n).
1927/// `slice` must not be empty.
19191928pub fn max(comptime T: type, slice: []const T) T {
19201929 var best = slice[0];
19211930 for (slice[1..]) |item| {
......@@ -2071,7 +2080,7 @@ test "asBytes" {
20712080 testing.expect(eql(u8, asBytes(&zero), ""));
20722081}
20732082
2074///Given any value, returns a copy of its bytes in an array.
2083/// Given any value, returns a copy of its bytes in an array.
20752084pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {
20762085 return asBytes(&value).*;
20772086}
......@@ -2105,7 +2114,7 @@ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
21052114 return if (comptime trait.isConstPtr(B)) *align(alignment) const T else *align(alignment) T;
21062115}
21072116
2108///Given a pointer to an array of bytes, returns a pointer to a value of the specified type
2117/// Given a pointer to an array of bytes, returns a pointer to a value of the specified type
21092118/// backed by those bytes, preserving constness.
21102119pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
21112120 return @ptrCast(BytesAsValueReturnType(T, @TypeOf(bytes)), bytes);
......@@ -2148,7 +2157,7 @@ test "bytesAsValue" {
21482157 testing.expect(meta.eql(inst, inst2.*));
21492158}
21502159
2151///Given a pointer to an array of bytes, returns a value of the specified type backed by a
2160/// Given a pointer to an array of bytes, returns a value of the specified type backed by a
21522161/// copy of those bytes.
21532162pub fn bytesToValue(comptime T: type, bytes: anytype) T {
21542163 return bytesAsValue(T, bytes).*;