Skip to content

Commit 9a86579

Browse files
authored
Auto merge of #38004 - GuillaumeGomez:tcp_stream_doc, r=frewsxcv
Add missing urls and examples to TcpStream r? @frewsxcv
2 parents 9003e1a + 56529cd commit 9a86579

File tree

1 file changed

+185
-8
lines changed

1 file changed

+185
-8
lines changed

src/libstd/net/tcp.rs

+185-8
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,52 @@ impl TcpStream {
8686
/// documentation for concrete examples.
8787
/// In case `ToSocketAddrs::to_socket_addrs()` returns more than one entry,
8888
/// then the first valid and reachable address is used.
89+
///
90+
/// # Examples
91+
///
92+
/// ```no_run
93+
/// use std::net::TcpStream;
94+
///
95+
/// if let Ok(stream) = TcpStream::connect("127.0.0.1:8080") {
96+
/// println!("Connected to the server!");
97+
/// } else {
98+
/// println!("Couldn't connect to server...");
99+
/// }
100+
/// ```
89101
#[stable(feature = "rust1", since = "1.0.0")]
90102
pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> {
91103
super::each_addr(addr, net_imp::TcpStream::connect).map(TcpStream)
92104
}
93105

94106
/// Returns the socket address of the remote peer of this TCP connection.
107+
///
108+
/// # Examples
109+
///
110+
/// ```no_run
111+
/// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream};
112+
///
113+
/// let stream = TcpStream::connect("127.0.0.1:8080")
114+
/// .expect("Couldn't connect to the server...");
115+
/// assert_eq!(stream.peer_addr().unwrap(),
116+
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
117+
/// ```
95118
#[stable(feature = "rust1", since = "1.0.0")]
96119
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
97120
self.0.peer_addr()
98121
}
99122

100123
/// Returns the socket address of the local half of this TCP connection.
124+
///
125+
/// # Examples
126+
///
127+
/// ```no_run
128+
/// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream};
129+
///
130+
/// let stream = TcpStream::connect("127.0.0.1:8080")
131+
/// .expect("Couldn't connect to the server...");
132+
/// assert_eq!(stream.local_addr().unwrap(),
133+
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
134+
/// ```
101135
#[stable(feature = "rust1", since = "1.0.0")]
102136
pub fn local_addr(&self) -> io::Result<SocketAddr> {
103137
self.0.socket_addr()
@@ -107,7 +141,19 @@ impl TcpStream {
107141
///
108142
/// This function will cause all pending and future I/O on the specified
109143
/// portions to return immediately with an appropriate value (see the
110-
/// documentation of `Shutdown`).
144+
/// documentation of [`Shutdown`]).
145+
///
146+
/// [`Shutdown`]: ../../std/net/enum.Shutdown.html
147+
///
148+
/// # Examples
149+
///
150+
/// ```no_run
151+
/// use std::net::{Shutdown, TcpStream};
152+
///
153+
/// let stream = TcpStream::connect("127.0.0.1:8080")
154+
/// .expect("Couldn't connect to the server...");
155+
/// stream.shutdown(Shutdown::Both).expect("shutdown call failed");
156+
/// ```
111157
#[stable(feature = "rust1", since = "1.0.0")]
112158
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
113159
self.0.shutdown(how)
@@ -119,62 +165,131 @@ impl TcpStream {
119165
/// object references. Both handles will read and write the same stream of
120166
/// data, and options set on one stream will be propagated to the other
121167
/// stream.
168+
///
169+
/// # Examples
170+
///
171+
/// ```no_run
172+
/// use std::net::TcpStream;
173+
///
174+
/// let stream = TcpStream::connect("127.0.0.1:8080")
175+
/// .expect("Couldn't connect to the server...");
176+
/// let stream_clone = stream.try_clone().expect("clone failed...");
177+
/// ```
122178
#[stable(feature = "rust1", since = "1.0.0")]
123179
pub fn try_clone(&self) -> io::Result<TcpStream> {
124180
self.0.duplicate().map(TcpStream)
125181
}
126182

127183
/// Sets the read timeout to the timeout specified.
128184
///
129-
/// If the value specified is `None`, then `read` calls will block
185+
/// If the value specified is [`None`], then [`read()`] calls will block
130186
/// indefinitely. It is an error to pass the zero `Duration` to this
131187
/// method.
132188
///
133189
/// # Note
134190
///
135191
/// Platforms may return a different error code whenever a read times out as
136192
/// a result of setting this option. For example Unix typically returns an
137-
/// error of the kind `WouldBlock`, but Windows may return `TimedOut`.
193+
/// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
194+
///
195+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
196+
/// [`read()`]: ../../std/io/trait.Read.html#tymethod.read
197+
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
198+
/// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
199+
///
200+
/// # Examples
201+
///
202+
/// ```no_run
203+
/// use std::net::TcpStream;
204+
///
205+
/// let stream = TcpStream::connect("127.0.0.1:8080")
206+
/// .expect("Couldn't connect to the server...");
207+
/// stream.set_read_timeout(None).expect("set_read_timeout call failed");
208+
/// ```
138209
#[stable(feature = "socket_timeout", since = "1.4.0")]
139210
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
140211
self.0.set_read_timeout(dur)
141212
}
142213

143214
/// Sets the write timeout to the timeout specified.
144215
///
145-
/// If the value specified is `None`, then `write` calls will block
146-
/// indefinitely. It is an error to pass the zero `Duration` to this
216+
/// If the value specified is [`None`], then [`write()`] calls will block
217+
/// indefinitely. It is an error to pass the zero [`Duration`] to this
147218
/// method.
148219
///
149220
/// # Note
150221
///
151222
/// Platforms may return a different error code whenever a write times out
152223
/// as a result of setting this option. For example Unix typically returns
153-
/// an error of the kind `WouldBlock`, but Windows may return `TimedOut`.
224+
/// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
225+
///
226+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
227+
/// [`write()`]: ../../std/io/trait.Write.html#tymethod.write
228+
/// [`Duration`]: ../../std/time/struct.Duration.html
229+
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
230+
/// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
231+
///
232+
/// # Examples
233+
///
234+
/// ```no_run
235+
/// use std::net::TcpStream;
236+
///
237+
/// let stream = TcpStream::connect("127.0.0.1:8080")
238+
/// .expect("Couldn't connect to the server...");
239+
/// stream.set_write_timeout(None).expect("set_write_timeout call failed");
240+
/// ```
154241
#[stable(feature = "socket_timeout", since = "1.4.0")]
155242
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
156243
self.0.set_write_timeout(dur)
157244
}
158245

159246
/// Returns the read timeout of this socket.
160247
///
161-
/// If the timeout is `None`, then `read` calls will block indefinitely.
248+
/// If the timeout is [`None`], then [`read()`] calls will block indefinitely.
162249
///
163250
/// # Note
164251
///
165252
/// Some platforms do not provide access to the current timeout.
253+
///
254+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
255+
/// [`read()`]: ../../std/io/trait.Read.html#tymethod.read
256+
///
257+
/// # Examples
258+
///
259+
/// ```no_run
260+
/// use std::net::TcpStream;
261+
///
262+
/// let stream = TcpStream::connect("127.0.0.1:8080")
263+
/// .expect("Couldn't connect to the server...");
264+
/// stream.set_read_timeout(None).expect("set_read_timeout call failed");
265+
/// assert_eq!(stream.read_timeout().unwrap(), None);
266+
/// ```
166267
#[stable(feature = "socket_timeout", since = "1.4.0")]
167268
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
168269
self.0.read_timeout()
169270
}
170271

171272
/// Returns the write timeout of this socket.
172273
///
173-
/// If the timeout is `None`, then `write` calls will block indefinitely.
274+
/// If the timeout is [`None`], then [`write()`] calls will block indefinitely.
174275
///
175276
/// # Note
176277
///
177278
/// Some platforms do not provide access to the current timeout.
279+
///
280+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
281+
/// [`write()`]: ../../std/io/trait.Write.html#tymethod.write
282+
///
283+
/// # Examples
284+
///
285+
/// ```no_run
286+
/// use std::net::TcpStream;
287+
///
288+
/// let stream = TcpStream::connect("127.0.0.1:8080")
289+
/// .expect("Couldn't connect to the server...");
290+
/// stream.set_write_timeout(None).expect("set_write_timeout call failed");
291+
/// assert_eq!(stream.write_timeout().unwrap(), None);
292+
/// ```
178293
#[stable(feature = "socket_timeout", since = "1.4.0")]
179294
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
180295
self.0.write_timeout()
@@ -187,6 +302,16 @@ impl TcpStream {
187302
/// small amount of data. When not set, data is buffered until there is a
188303
/// sufficient amount to send out, thereby avoiding the frequent sending of
189304
/// small packets.
305+
///
306+
/// # Examples
307+
///
308+
/// ```no_run
309+
/// use std::net::TcpStream;
310+
///
311+
/// let stream = TcpStream::connect("127.0.0.1:8080")
312+
/// .expect("Couldn't connect to the server...");
313+
/// stream.set_nodelay(true).expect("set_nodelay call failed");
314+
/// ```
190315
#[stable(feature = "net2_mutators", since = "1.9.0")]
191316
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
192317
self.0.set_nodelay(nodelay)
@@ -197,6 +322,17 @@ impl TcpStream {
197322
/// For more information about this option, see [`set_nodelay`][link].
198323
///
199324
/// [link]: #method.set_nodelay
325+
///
326+
/// # Examples
327+
///
328+
/// ```no_run
329+
/// use std::net::TcpStream;
330+
///
331+
/// let stream = TcpStream::connect("127.0.0.1:8080")
332+
/// .expect("Couldn't connect to the server...");
333+
/// stream.set_nodelay(true).expect("set_nodelay call failed");
334+
/// assert_eq!(stream.nodelay().unwrap_or(false), true);
335+
/// ```
200336
#[stable(feature = "net2_mutators", since = "1.9.0")]
201337
pub fn nodelay(&self) -> io::Result<bool> {
202338
self.0.nodelay()
@@ -206,6 +342,16 @@ impl TcpStream {
206342
///
207343
/// This value sets the time-to-live field that is used in every packet sent
208344
/// from this socket.
345+
///
346+
/// # Examples
347+
///
348+
/// ```no_run
349+
/// use std::net::TcpStream;
350+
///
351+
/// let stream = TcpStream::connect("127.0.0.1:8080")
352+
/// .expect("Couldn't connect to the server...");
353+
/// stream.set_ttl(100).expect("set_ttl call failed");
354+
/// ```
209355
#[stable(feature = "net2_mutators", since = "1.9.0")]
210356
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
211357
self.0.set_ttl(ttl)
@@ -216,6 +362,17 @@ impl TcpStream {
216362
/// For more information about this option, see [`set_ttl`][link].
217363
///
218364
/// [link]: #method.set_ttl
365+
///
366+
/// # Examples
367+
///
368+
/// ```no_run
369+
/// use std::net::TcpStream;
370+
///
371+
/// let stream = TcpStream::connect("127.0.0.1:8080")
372+
/// .expect("Couldn't connect to the server...");
373+
/// stream.set_ttl(100).expect("set_ttl call failed");
374+
/// assert_eq!(stream.ttl().unwrap_or(0), 100);
375+
/// ```
219376
#[stable(feature = "net2_mutators", since = "1.9.0")]
220377
pub fn ttl(&self) -> io::Result<u32> {
221378
self.0.ttl()
@@ -226,6 +383,16 @@ impl TcpStream {
226383
/// This will retrieve the stored error in the underlying socket, clearing
227384
/// the field in the process. This can be useful for checking errors between
228385
/// calls.
386+
///
387+
/// # Examples
388+
///
389+
/// ```no_run
390+
/// use std::net::TcpStream;
391+
///
392+
/// let stream = TcpStream::connect("127.0.0.1:8080")
393+
/// .expect("Couldn't connect to the server...");
394+
/// stream.take_error().expect("No error was expected...");
395+
/// ```
229396
#[stable(feature = "net2_mutators", since = "1.9.0")]
230397
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
231398
self.0.take_error()
@@ -235,6 +402,16 @@ impl TcpStream {
235402
///
236403
/// On Unix this corresponds to calling fcntl, and on Windows this
237404
/// corresponds to calling ioctlsocket.
405+
///
406+
/// # Examples
407+
///
408+
/// ```no_run
409+
/// use std::net::TcpStream;
410+
///
411+
/// let stream = TcpStream::connect("127.0.0.1:8080")
412+
/// .expect("Couldn't connect to the server...");
413+
/// stream.set_nonblocking(true).expect("set_nonblocking call failed");
414+
/// ```
238415
#[stable(feature = "net2_mutators", since = "1.9.0")]
239416
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
240417
self.0.set_nonblocking(nonblocking)

0 commit comments

Comments
 (0)