| ... | @@ -1887,7 +1887,11 @@ test "writeIntBig and writeIntLittle" { | ... | @@ -1887,7 +1887,11 @@ test "writeIntBig and writeIntLittle" { |
| 1887 | pub fn byteSwapAllFields(comptime S: type, ptr: *S) void { | 1887 | pub fn byteSwapAllFields(comptime S: type, ptr: *S) void { |
| 1888 | if (@typeInfo(S) != .Struct) @compileError("byteSwapAllFields expects a struct as the first argument"); | 1888 | if (@typeInfo(S) != .Struct) @compileError("byteSwapAllFields expects a struct as the first argument"); |
| 1889 | inline for (std.meta.fields(S)) |f| { | 1889 | inline for (std.meta.fields(S)) |f| { |
| 1890 | @field(ptr, f.name) = @byteSwap(@field(ptr, f.name)); | 1890 | if (@typeInfo(f.type) == .Struct) { |
| | 1891 | byteSwapAllFields(f.type, &@field(ptr, f.name)); |
| | 1892 | } else { |
| | 1893 | @field(ptr, f.name) = @byteSwap(@field(ptr, f.name)); |
| | 1894 | } |
| 1891 | } | 1895 | } |
| 1892 | } | 1896 | } |
| 1893 | | 1897 | |
| ... | @@ -1897,17 +1901,33 @@ test "byteSwapAllFields" { | ... | @@ -1897,17 +1901,33 @@ test "byteSwapAllFields" { |
| 1897 | f1: u16, | 1901 | f1: u16, |
| 1898 | f2: u32, | 1902 | f2: u32, |
| 1899 | }; | 1903 | }; |
| | 1904 | const K = extern struct { |
| | 1905 | f0: u8, |
| | 1906 | f1: T, |
| | 1907 | f2: u16, |
| | 1908 | }; |
| 1900 | var s = T{ | 1909 | var s = T{ |
| 1901 | .f0 = 0x12, | 1910 | .f0 = 0x12, |
| 1902 | .f1 = 0x1234, | 1911 | .f1 = 0x1234, |
| 1903 | .f2 = 0x12345678, | 1912 | .f2 = 0x12345678, |
| 1904 | }; | 1913 | }; |
| | 1914 | var k = K{ |
| | 1915 | .f0 = 0x12, |
| | 1916 | .f1 = s, |
| | 1917 | .f2 = 0x1234, |
| | 1918 | }; |
| 1905 | byteSwapAllFields(T, &s); | 1919 | byteSwapAllFields(T, &s); |
| | 1920 | byteSwapAllFields(K, &k); |
| 1906 | try std.testing.expectEqual(T{ | 1921 | try std.testing.expectEqual(T{ |
| 1907 | .f0 = 0x12, | 1922 | .f0 = 0x12, |
| 1908 | .f1 = 0x3412, | 1923 | .f1 = 0x3412, |
| 1909 | .f2 = 0x78563412, | 1924 | .f2 = 0x78563412, |
| 1910 | }, s); | 1925 | }, s); |
| | 1926 | try std.testing.expectEqual(K{ |
| | 1927 | .f0 = 0x12, |
| | 1928 | .f1 = s, |
| | 1929 | .f2 = 0x3412, |
| | 1930 | }, k); |
| 1911 | } | 1931 | } |
| 1912 | | 1932 | |
| 1913 | /// Returns an iterator that iterates over the slices of `buffer` that are not | 1933 | /// Returns an iterator that iterates over the slices of `buffer` that are not |