Skip to content

Commit ff433da

Browse files
Add missing examples for SocketAddrV4
1 parent b8f6c20 commit ff433da

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/libstd/net/addr.rs

+46
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ impl SocketAddr {
194194

195195
impl SocketAddrV4 {
196196
/// 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+
/// ```
197205
#[stable(feature = "rust1", since = "1.0.0")]
198206
pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
199207
SocketAddrV4 {
@@ -207,6 +215,15 @@ impl SocketAddrV4 {
207215
}
208216

209217
/// 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+
/// ```
210227
#[stable(feature = "rust1", since = "1.0.0")]
211228
pub fn ip(&self) -> &Ipv4Addr {
212229
unsafe {
@@ -215,18 +232,47 @@ impl SocketAddrV4 {
215232
}
216233

217234
/// 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+
/// ```
218245
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
219246
pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
220247
self.inner.sin_addr = *new_ip.as_inner()
221248
}
222249

223250
/// 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+
/// ```
224260
#[stable(feature = "rust1", since = "1.0.0")]
225261
pub fn port(&self) -> u16 {
226262
ntoh(self.inner.sin_port)
227263
}
228264

229265
/// 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+
/// ```
230276
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
231277
pub fn set_port(&mut self, new_port: u16) {
232278
self.inner.sin_port = hton(new_port);

0 commit comments

Comments
 (0)