| ... | @@ -614,7 +614,7 @@ test "spanZ" { | ... | @@ -614,7 +614,7 @@ test "spanZ" { |
| 614 | } | 614 | } |
| 615 | | 615 | |
| 616 | /// Takes a pointer to an array, an array, a vector, a sentinel-terminated pointer, | 616 | /// Takes a pointer to an array, an array, a vector, a sentinel-terminated pointer, |
| 617 | /// or a slice, and returns the length. | 617 | /// a slice or a tuple, and returns the length. |
| 618 | /// In the case of a sentinel-terminated array, it uses the array length. | 618 | /// In the case of a sentinel-terminated array, it uses the array length. |
| 619 | /// For C pointers it assumes it is a pointer-to-many with a 0 sentinel. | 619 | /// For C pointers it assumes it is a pointer-to-many with a 0 sentinel. |
| 620 | pub fn len(value: anytype) usize { | 620 | pub fn len(value: anytype) usize { |
| ... | @@ -633,6 +633,9 @@ pub fn len(value: anytype) usize { | ... | @@ -633,6 +633,9 @@ pub fn len(value: anytype) usize { |
| 633 | .C => indexOfSentinel(info.child, 0, value), | 633 | .C => indexOfSentinel(info.child, 0, value), |
| 634 | .Slice => value.len, | 634 | .Slice => value.len, |
| 635 | }, | 635 | }, |
| | 636 | .Struct => |info| if (info.is_tuple) { |
| | 637 | return info.fields.len; |
| | 638 | } else @compileError("invalid type given to std.mem.len"), |
| 636 | else => @compileError("invalid type given to std.mem.len"), | 639 | else => @compileError("invalid type given to std.mem.len"), |
| 637 | }; | 640 | }; |
| 638 | } | 641 | } |
| ... | @@ -658,6 +661,11 @@ test "len" { | ... | @@ -658,6 +661,11 @@ test "len" { |
| 658 | const vector: meta.Vector(2, u32) = [2]u32{ 1, 2 }; | 661 | const vector: meta.Vector(2, u32) = [2]u32{ 1, 2 }; |
| 659 | testing.expect(len(vector) == 2); | 662 | testing.expect(len(vector) == 2); |
| 660 | } | 663 | } |
| | 664 | { |
| | 665 | const tuple = .{ 1, 2 }; |
| | 666 | testing.expect(len(tuple) == 2); |
| | 667 | testing.expect(tuple[0] == 1); |
| | 668 | } |
| 661 | } | 669 | } |
| 662 | | 670 | |
| 663 | /// Takes a pointer to an array, an array, a sentinel-terminated pointer, | 671 | /// Takes a pointer to an array, an array, a sentinel-terminated pointer, |