Skip to content

Commit 559141c

Browse files
Add missing examples to SocketAddrV6
1 parent 9fba8df commit 559141c

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/libstd/net/addr.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ impl SocketAddrV4 {
236236
impl SocketAddrV6 {
237237
/// Creates a new socket address from the ip/port/flowinfo/scope_id
238238
/// components.
239+
///
240+
/// # Examples
241+
///
242+
/// ```
243+
/// use std::net::{SocketAddrV6, Ipv6Addr};
244+
///
245+
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
246+
/// ```
239247
#[stable(feature = "rust1", since = "1.0.0")]
240248
pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32)
241249
-> SocketAddrV6 {
@@ -252,6 +260,15 @@ impl SocketAddrV6 {
252260
}
253261

254262
/// Returns the IP address associated with this socket address.
263+
///
264+
/// # Examples
265+
///
266+
/// ```
267+
/// use std::net::{SocketAddrV6, Ipv6Addr};
268+
///
269+
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
270+
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
271+
/// ```
255272
#[stable(feature = "rust1", since = "1.0.0")]
256273
pub fn ip(&self) -> &Ipv6Addr {
257274
unsafe {
@@ -260,44 +277,111 @@ impl SocketAddrV6 {
260277
}
261278

262279
/// Change the IP address associated with this socket address.
280+
///
281+
/// # Examples
282+
///
283+
/// ```
284+
/// use std::net::{SocketAddrV6, Ipv6Addr};
285+
///
286+
/// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
287+
/// socket.set_ip(Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
288+
/// assert_eq!(socket.ip(), &Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
289+
/// ```
263290
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
264291
pub fn set_ip(&mut self, new_ip: Ipv6Addr) {
265292
self.inner.sin6_addr = *new_ip.as_inner()
266293
}
267294

268295
/// Returns the port number associated with this socket address.
296+
///
297+
/// # Examples
298+
///
299+
/// ```
300+
/// use std::net::{SocketAddrV6, Ipv6Addr};
301+
///
302+
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
303+
/// assert_eq!(socket.port(), 8080);
304+
/// ```
269305
#[stable(feature = "rust1", since = "1.0.0")]
270306
pub fn port(&self) -> u16 {
271307
ntoh(self.inner.sin6_port)
272308
}
273309

274310
/// Change the port number associated with this socket address.
311+
///
312+
/// # Examples
313+
///
314+
/// ```
315+
/// use std::net::{SocketAddrV6, Ipv6Addr};
316+
///
317+
/// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
318+
/// socket.set_port(4242);
319+
/// assert_eq!(socket.port(), 4242);
320+
/// ```
275321
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
276322
pub fn set_port(&mut self, new_port: u16) {
277323
self.inner.sin6_port = hton(new_port);
278324
}
279325

280326
/// Returns the flow information associated with this address,
281327
/// corresponding to the `sin6_flowinfo` field in C.
328+
///
329+
/// # Examples
330+
///
331+
/// ```
332+
/// use std::net::{SocketAddrV6, Ipv6Addr};
333+
///
334+
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0);
335+
/// assert_eq!(socket.flowinfo(), 10);
336+
/// ```
282337
#[stable(feature = "rust1", since = "1.0.0")]
283338
pub fn flowinfo(&self) -> u32 {
284339
self.inner.sin6_flowinfo
285340
}
286341

287342
/// Change the flow information associated with this socket address.
343+
///
344+
/// # Examples
345+
///
346+
/// ```
347+
/// use std::net::{SocketAddrV6, Ipv6Addr};
348+
///
349+
/// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0);
350+
/// socket.set_flowinfo(56);
351+
/// assert_eq!(socket.flowinfo(), 56);
352+
/// ```
288353
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
289354
pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
290355
self.inner.sin6_flowinfo = new_flowinfo;
291356
}
292357

293358
/// Returns the scope ID associated with this address,
294359
/// corresponding to the `sin6_scope_id` field in C.
360+
///
361+
/// # Examples
362+
///
363+
/// ```
364+
/// use std::net::{SocketAddrV6, Ipv6Addr};
365+
///
366+
/// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78);
367+
/// assert_eq!(socket.scope_id(), 78);
368+
/// ```
295369
#[stable(feature = "rust1", since = "1.0.0")]
296370
pub fn scope_id(&self) -> u32 {
297371
self.inner.sin6_scope_id
298372
}
299373

300374
/// Change the scope ID associated with this socket address.
375+
///
376+
/// # Examples
377+
///
378+
/// ```
379+
/// use std::net::{SocketAddrV6, Ipv6Addr};
380+
///
381+
/// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78);
382+
/// socket.set_scope_id(42);
383+
/// assert_eq!(socket.scope_id(), 42);
384+
/// ```
301385
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
302386
pub fn set_scope_id(&mut self, new_scope_id: u32) {
303387
self.inner.sin6_scope_id = new_scope_id;

0 commit comments

Comments
 (0)