diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-01-30 01:46:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-30 01:46:45 +0100 |
| commit | 3df1ff2608eac304b74af024584bd35cff0285cf (patch) | |
| tree | ba7b76d69530a06108e2e11ee7176f4a906b1735 /src | |
| parent | 4af33a9c2608383fea0bac87fc0205603833bf6f (diff) | |
| parent | ab5e29689086d5368e8e526b0295c628bd17ae92 (diff) | |
| download | rust-3df1ff2608eac304b74af024584bd35cff0285cf.tar.gz rust-3df1ff2608eac304b74af024584bd35cff0285cf.zip | |
Rollup merge of #68640 - LeSeulArtichaut:doc-from-ip, r=steveklabnik
Document remaining undocumented `From` implementations for IPs Some `From` implementations were missing documentation. I added examples, I tried to be as close to existing examples as possible. Related to #51430 (cc @skade) r? @steveklabnik
Diffstat (limited to 'src')
| -rw-r--r-- | src/libstd/net/ip.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 6410a4f2b65..200b00b1195 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -813,6 +813,20 @@ impl fmt::Display for IpAddr { #[stable(feature = "ip_from_ip", since = "1.16.0")] impl From<Ipv4Addr> for IpAddr { + /// Copies this address to a new `IpAddr::V4`. + /// + /// # Examples + /// + /// ``` + /// use std::net::{IpAddr, Ipv4Addr}; + /// + /// let addr = Ipv4Addr::new(127, 0, 0, 1); + /// + /// assert_eq!( + /// IpAddr::V4(addr), + /// IpAddr::from(addr) + /// ) + /// ``` fn from(ipv4: Ipv4Addr) -> IpAddr { IpAddr::V4(ipv4) } @@ -820,6 +834,20 @@ impl From<Ipv4Addr> for IpAddr { #[stable(feature = "ip_from_ip", since = "1.16.0")] impl From<Ipv6Addr> for IpAddr { + /// Copies this address to a new `IpAddr::V6`. + /// + /// # Examples + /// + /// ``` + /// use std::net::{IpAddr, Ipv6Addr}; + /// + /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff); + /// + /// assert_eq!( + /// IpAddr::V6(addr), + /// IpAddr::from(addr) + /// ); + /// ``` fn from(ipv6: Ipv6Addr) -> IpAddr { IpAddr::V6(ipv6) } @@ -975,6 +1003,8 @@ impl From<u32> for Ipv4Addr { #[stable(feature = "from_slice_v4", since = "1.9.0")] impl From<[u8; 4]> for Ipv4Addr { + /// Creates an `Ipv4Addr` from a four element byte array. + /// /// # Examples /// /// ``` @@ -1734,6 +1764,27 @@ impl From<u128> for Ipv6Addr { #[stable(feature = "ipv6_from_octets", since = "1.9.0")] impl From<[u8; 16]> for Ipv6Addr { + /// Creates an `Ipv6Addr` from a sixteen element byte array. + /// + /// # Examples + /// + /// ``` + /// use std::net::Ipv6Addr; + /// + /// let addr = Ipv6Addr::from([ + /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8, + /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8, + /// ]); + /// assert_eq!( + /// Ipv6Addr::new( + /// 0x1918, 0x1716, + /// 0x1514, 0x1312, + /// 0x1110, 0x0f0e, + /// 0x0d0c, 0x0b0a + /// ), + /// addr + /// ); + /// ``` fn from(octets: [u8; 16]) -> Ipv6Addr { let inner = c::in6_addr { s6_addr: octets }; Ipv6Addr::from_inner(inner) @@ -1742,6 +1793,27 @@ impl From<[u8; 16]> for Ipv6Addr { #[stable(feature = "ipv6_from_segments", since = "1.16.0")] impl From<[u16; 8]> for Ipv6Addr { + /// Creates an `Ipv6Addr` from an eight element 16-bit array. + /// + /// # Examples + /// + /// ``` + /// use std::net::Ipv6Addr; + /// + /// let addr = Ipv6Addr::from([ + /// 525u16, 524u16, 523u16, 522u16, + /// 521u16, 520u16, 519u16, 518u16, + /// ]); + /// assert_eq!( + /// Ipv6Addr::new( + /// 0x20d, 0x20c, + /// 0x20b, 0x20a, + /// 0x209, 0x208, + /// 0x207, 0x206 + /// ), + /// addr + /// ); + /// ``` fn from(segments: [u16; 8]) -> Ipv6Addr { let [a, b, c, d, e, f, g, h] = segments; Ipv6Addr::new(a, b, c, d, e, f, g, h) |
