@@ -31,7 +31,7 @@ pub enum SocketAddr {
31
31
/// An IPv4 socket address which is a (ip, port) combination.
32
32
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
33
33
V4 ( #[ stable( feature = "rust1" , since = "1.0.0" ) ] SocketAddrV4 ) ,
34
- /// An IPv6 socket address
34
+ /// An IPv6 socket address.
35
35
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
36
36
V6 ( #[ stable( feature = "rust1" , since = "1.0.0" ) ] SocketAddrV6 ) ,
37
37
}
@@ -48,6 +48,16 @@ pub struct SocketAddrV6 { inner: c::sockaddr_in6 }
48
48
49
49
impl SocketAddr {
50
50
/// 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
+ /// ```
51
61
#[ stable( feature = "ip_addr" , since = "1.7.0" ) ]
52
62
pub fn new ( ip : IpAddr , port : u16 ) -> SocketAddr {
53
63
match ip {
@@ -57,6 +67,15 @@ impl SocketAddr {
57
67
}
58
68
59
69
/// 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
+ /// ```
60
79
#[ stable( feature = "ip_addr" , since = "1.7.0" ) ]
61
80
pub fn ip ( & self ) -> IpAddr {
62
81
match * self {
@@ -66,6 +85,16 @@ impl SocketAddr {
66
85
}
67
86
68
87
/// 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
+ /// ```
69
98
#[ stable( feature = "sockaddr_setters" , since = "1.9.0" ) ]
70
99
pub fn set_ip ( & mut self , new_ip : IpAddr ) {
71
100
// `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
@@ -77,6 +106,15 @@ impl SocketAddr {
77
106
}
78
107
79
108
/// 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
+ /// ```
80
118
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
81
119
pub fn port ( & self ) -> u16 {
82
120
match * self {
@@ -86,6 +124,16 @@ impl SocketAddr {
86
124
}
87
125
88
126
/// 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
+ /// ```
89
137
#[ stable( feature = "sockaddr_setters" , since = "1.9.0" ) ]
90
138
pub fn set_port ( & mut self , new_port : u16 ) {
91
139
match * self {
@@ -96,6 +144,20 @@ impl SocketAddr {
96
144
97
145
/// Returns true if the IP in this `SocketAddr` is a valid IPv4 address,
98
146
/// 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
+ /// ```
99
161
#[ unstable( feature = "sockaddr_checker" , issue = "36949" ) ]
100
162
pub fn is_ipv4 ( & self ) -> bool {
101
163
match * self {
@@ -106,6 +168,21 @@ impl SocketAddr {
106
168
107
169
/// Returns true if the IP in this `SocketAddr` is a valid IPv6 address,
108
170
/// 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
+ /// ```
109
186
#[ unstable( feature = "sockaddr_checker" , issue = "36949" ) ]
110
187
pub fn is_ipv6 ( & self ) -> bool {
111
188
match * self {
0 commit comments