diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-10-14 06:04:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-14 06:04:27 +0200 |
| commit | e01eae72daff6e7931d4aa05250480e404f15efc (patch) | |
| tree | 4c78ef771fe8e8e098480139783a94c539a7cee5 /compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | |
| parent | 55f8b9e7d8847bf744756513570fd60a24a14760 (diff) | |
| parent | 0b7e39908ee6314dd8b8561dd4c8a47ce3ba2f38 (diff) | |
| download | rust-e01eae72daff6e7931d4aa05250480e404f15efc.tar.gz rust-e01eae72daff6e7931d4aa05250480e404f15efc.zip | |
Rollup merge of #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 #27709 #76205~~
Tracking issue: https://github.com/rust-lang/rust/issues/131360
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp')
0 files changed, 0 insertions, 0 deletions
