Commit 6d8235e
authored
Rollup merge of rust-lang#130629 - Dirbaio:net-from-octets, r=tgross35
core/net: add Ipv[46]Addr::from_octets, Ipv6Addr::from_segments.
Adds:
- `Ipv4Address::from_octets([u8;4])`
- `Ipv6Address::from_octets([u8;16])`
- `Ipv6Address::from_segments([u16;8])`
equivalent to the existing `From` impls.
Advantages:
- Consistent with `to_bits, from_bits`.
- More discoverable than the `From` impls.
- Helps with type inference: it's common to want to convert byte slices to IP addrs. If you try this
```rust
fn foo(x: &[u8]) -> Ipv4Addr {
Ipv4Addr::from(foo.try_into().unwrap())
}
```
it [doesn't work](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0e2873312de275a58fa6e33d1b213bec). You have to write `Ipv4Addr::from(<[u8;4]>::try_from(x).unwrap())` instead, which is not great. With `from_octets` it is able to infer the right types.
Found this while porting [smoltcp](https://github.com/smoltcp-rs/smoltcp/) from its own IP address types to the `core::net` types.
~~Tracking issues rust-lang#27709 rust-lang#76205~~
Tracking issue: rust-lang#1313603 files changed
+114
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
600 | 600 | | |
601 | 601 | | |
602 | 602 | | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
603 | 621 | | |
604 | 622 | | |
605 | 623 | | |
| |||
1400 | 1418 | | |
1401 | 1419 | | |
1402 | 1420 | | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
1403 | 1449 | | |
1404 | 1450 | | |
1405 | 1451 | | |
| |||
1932 | 1978 | | |
1933 | 1979 | | |
1934 | 1980 | | |
1935 | | - | |
| 1981 | + | |
1936 | 1982 | | |
1937 | 1983 | | |
1938 | 1984 | | |
| |||
1941 | 1987 | | |
1942 | 1988 | | |
1943 | 1989 | | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
1944 | 2017 | | |
1945 | 2018 | | |
1946 | 2019 | | |
| |||
2113 | 2186 | | |
2114 | 2187 | | |
2115 | 2188 | | |
2116 | | - | |
2117 | | - | |
| 2189 | + | |
| 2190 | + | |
2118 | 2191 | | |
2119 | 2192 | | |
2120 | 2193 | | |
2121 | | - | |
2122 | | - | |
2123 | | - | |
2124 | | - | |
| 2194 | + | |
| 2195 | + | |
2125 | 2196 | | |
2126 | 2197 | | |
2127 | 2198 | | |
| |||
2142 | 2213 | | |
2143 | 2214 | | |
2144 | 2215 | | |
2145 | | - | |
2146 | | - | |
| 2216 | + | |
| 2217 | + | |
2147 | 2218 | | |
2148 | 2219 | | |
2149 | 2220 | | |
2150 | | - | |
2151 | | - | |
2152 | | - | |
2153 | | - | |
| 2221 | + | |
| 2222 | + | |
2154 | 2223 | | |
2155 | 2224 | | |
2156 | 2225 | | |
| |||
2172 | 2241 | | |
2173 | 2242 | | |
2174 | 2243 | | |
2175 | | - | |
2176 | | - | |
| 2244 | + | |
| 2245 | + | |
2177 | 2246 | | |
2178 | 2247 | | |
2179 | 2248 | | |
2180 | | - | |
2181 | | - | |
2182 | | - | |
2183 | | - | |
| 2249 | + | |
| 2250 | + | |
2184 | 2251 | | |
2185 | 2252 | | |
2186 | 2253 | | |
| |||
2201 | 2268 | | |
2202 | 2269 | | |
2203 | 2270 | | |
2204 | | - | |
2205 | | - | |
| 2271 | + | |
| 2272 | + | |
2206 | 2273 | | |
2207 | 2274 | | |
2208 | 2275 | | |
2209 | | - | |
2210 | | - | |
2211 | | - | |
2212 | | - | |
| 2276 | + | |
| 2277 | + | |
2213 | 2278 | | |
2214 | 2279 | | |
2215 | 2280 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
494 | 494 | | |
495 | 495 | | |
496 | 496 | | |
| 497 | + | |
497 | 498 | | |
498 | 499 | | |
499 | 500 | | |
| |||
846 | 847 | | |
847 | 848 | | |
848 | 849 | | |
849 | | - | |
| 850 | + | |
| 851 | + | |
850 | 852 | | |
851 | 853 | | |
852 | 854 | | |
853 | 855 | | |
854 | 856 | | |
855 | 857 | | |
| 858 | + | |
| 859 | + | |
856 | 860 | | |
857 | 861 | | |
| 862 | + | |
858 | 863 | | |
859 | 864 | | |
860 | 865 | | |
| |||
865 | 870 | | |
866 | 871 | | |
867 | 872 | | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
868 | 879 | | |
| 880 | + | |
| 881 | + | |
869 | 882 | | |
870 | 883 | | |
871 | 884 | | |
| |||
915 | 928 | | |
916 | 929 | | |
917 | 930 | | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
918 | 934 | | |
919 | 935 | | |
920 | 936 | | |
| |||
971 | 987 | | |
972 | 988 | | |
973 | 989 | | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
974 | 993 | | |
975 | 994 | | |
976 | 995 | | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
977 | 999 | | |
978 | 1000 | | |
979 | 1001 | | |
| |||
0 commit comments