@@ -194,6 +194,14 @@ impl SocketAddr {
194
194
195
195
impl SocketAddrV4 {
196
196
/// Creates a new socket address from the (ip, port) pair.
197
+ ///
198
+ /// # Examples
199
+ ///
200
+ /// ```
201
+ /// use std::net::{SocketAddrV4, Ipv4Addr};
202
+ ///
203
+ /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
204
+ /// ```
197
205
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
198
206
pub fn new ( ip : Ipv4Addr , port : u16 ) -> SocketAddrV4 {
199
207
SocketAddrV4 {
@@ -207,6 +215,15 @@ impl SocketAddrV4 {
207
215
}
208
216
209
217
/// Returns the IP address associated with this socket address.
218
+ ///
219
+ /// # Examples
220
+ ///
221
+ /// ```
222
+ /// use std::net::{SocketAddrV4, Ipv4Addr};
223
+ ///
224
+ /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
225
+ /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
226
+ /// ```
210
227
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
211
228
pub fn ip ( & self ) -> & Ipv4Addr {
212
229
unsafe {
@@ -215,18 +232,47 @@ impl SocketAddrV4 {
215
232
}
216
233
217
234
/// Change the IP address associated with this socket address.
235
+ ///
236
+ /// # Examples
237
+ ///
238
+ /// ```
239
+ /// use std::net::{SocketAddrV4, Ipv4Addr};
240
+ ///
241
+ /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
242
+ /// socket.set_ip(Ipv4Addr::new(192, 168, 0, 1));
243
+ /// assert_eq!(socket.ip(), &Ipv4Addr::new(192, 168, 0, 1));
244
+ /// ```
218
245
#[ stable( feature = "sockaddr_setters" , since = "1.9.0" ) ]
219
246
pub fn set_ip ( & mut self , new_ip : Ipv4Addr ) {
220
247
self . inner . sin_addr = * new_ip. as_inner ( )
221
248
}
222
249
223
250
/// Returns the port number associated with this socket address.
251
+ ///
252
+ /// # Examples
253
+ ///
254
+ /// ```
255
+ /// use std::net::{SocketAddrV4, Ipv4Addr};
256
+ ///
257
+ /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
258
+ /// assert_eq!(socket.port(), 8080);
259
+ /// ```
224
260
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
225
261
pub fn port ( & self ) -> u16 {
226
262
ntoh ( self . inner . sin_port )
227
263
}
228
264
229
265
/// Change the port number associated with this socket address.
266
+ ///
267
+ /// # Examples
268
+ ///
269
+ /// ```
270
+ /// use std::net::{SocketAddrV4, Ipv4Addr};
271
+ ///
272
+ /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
273
+ /// socket.set_port(4242);
274
+ /// assert_eq!(socket.port(), 4242);
275
+ /// ```
230
276
#[ stable( feature = "sockaddr_setters" , since = "1.9.0" ) ]
231
277
pub fn set_port ( & mut self , new_port : u16 ) {
232
278
self . inner . sin_port = hton ( new_port) ;
0 commit comments