Skip to content

Commit 5ca9fa4

Browse files
Rollup merge of #37880 - GuillaumeGomez:socket-4-doc, r=frewsxcv
Add missing examples in SocketAddr r? @frewsxcv
2 parents 53cead4 + bf78ef3 commit 5ca9fa4

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

src/libstd/net/addr.rs

+78-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub enum SocketAddr {
3131
/// An IPv4 socket address which is a (ip, port) combination.
3232
#[stable(feature = "rust1", since = "1.0.0")]
3333
V4(#[stable(feature = "rust1", since = "1.0.0")] SocketAddrV4),
34-
/// An IPv6 socket address
34+
/// An IPv6 socket address.
3535
#[stable(feature = "rust1", since = "1.0.0")]
3636
V6(#[stable(feature = "rust1", since = "1.0.0")] SocketAddrV6),
3737
}
@@ -48,6 +48,16 @@ pub struct SocketAddrV6 { inner: c::sockaddr_in6 }
4848

4949
impl SocketAddr {
5050
/// Creates a new socket address from the (ip, port) pair.
51+
///
52+
/// # Examples
53+
///
54+
/// ```
55+
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
56+
///
57+
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
58+
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
59+
/// assert_eq!(socket.port(), 8080);
60+
/// ```
5161
#[stable(feature = "ip_addr", since = "1.7.0")]
5262
pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
5363
match ip {
@@ -57,6 +67,15 @@ impl SocketAddr {
5767
}
5868

5969
/// Returns the IP address associated with this socket address.
70+
///
71+
/// # Examples
72+
///
73+
/// ```
74+
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
75+
///
76+
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
77+
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
78+
/// ```
6079
#[stable(feature = "ip_addr", since = "1.7.0")]
6180
pub fn ip(&self) -> IpAddr {
6281
match *self {
@@ -66,6 +85,16 @@ impl SocketAddr {
6685
}
6786

6887
/// Change the IP address associated with this socket address.
88+
///
89+
/// # Examples
90+
///
91+
/// ```
92+
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
93+
///
94+
/// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
95+
/// socket.set_ip(IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
96+
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
97+
/// ```
6998
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
7099
pub fn set_ip(&mut self, new_ip: IpAddr) {
71100
// `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
@@ -77,6 +106,15 @@ impl SocketAddr {
77106
}
78107

79108
/// Returns the port number associated with this socket address.
109+
///
110+
/// # Examples
111+
///
112+
/// ```
113+
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
114+
///
115+
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
116+
/// assert_eq!(socket.port(), 8080);
117+
/// ```
80118
#[stable(feature = "rust1", since = "1.0.0")]
81119
pub fn port(&self) -> u16 {
82120
match *self {
@@ -86,6 +124,16 @@ impl SocketAddr {
86124
}
87125

88126
/// Change the port number associated with this socket address.
127+
///
128+
/// # Examples
129+
///
130+
/// ```
131+
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
132+
///
133+
/// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
134+
/// socket.set_port(1025);
135+
/// assert_eq!(socket.port(), 1025);
136+
/// ```
89137
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
90138
pub fn set_port(&mut self, new_port: u16) {
91139
match *self {
@@ -96,6 +144,20 @@ impl SocketAddr {
96144

97145
/// Returns true if the IP in this `SocketAddr` is a valid IPv4 address,
98146
/// false if it's a valid IPv6 address.
147+
///
148+
/// # Examples
149+
///
150+
/// ```
151+
/// #![feature(sockaddr_checker)]
152+
///
153+
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
154+
///
155+
/// fn main() {
156+
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
157+
/// assert_eq!(socket.is_ipv4(), true);
158+
/// assert_eq!(socket.is_ipv6(), false);
159+
/// }
160+
/// ```
99161
#[unstable(feature = "sockaddr_checker", issue = "36949")]
100162
pub fn is_ipv4(&self) -> bool {
101163
match *self {
@@ -106,6 +168,21 @@ impl SocketAddr {
106168

107169
/// Returns true if the IP in this `SocketAddr` is a valid IPv6 address,
108170
/// false if it's a valid IPv4 address.
171+
///
172+
/// # Examples
173+
///
174+
/// ```
175+
/// #![feature(sockaddr_checker)]
176+
///
177+
/// use std::net::{IpAddr, Ipv6Addr, SocketAddr};
178+
///
179+
/// fn main() {
180+
/// let socket = SocketAddr::new(
181+
/// IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 0, 1)), 8080);
182+
/// assert_eq!(socket.is_ipv4(), false);
183+
/// assert_eq!(socket.is_ipv6(), true);
184+
/// }
185+
/// ```
109186
#[unstable(feature = "sockaddr_checker", issue = "36949")]
110187
pub fn is_ipv6(&self) -> bool {
111188
match *self {

0 commit comments

Comments
 (0)