@@ -86,18 +86,52 @@ impl TcpStream {
86
86
/// documentation for concrete examples.
87
87
/// In case `ToSocketAddrs::to_socket_addrs()` returns more than one entry,
88
88
/// 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
+ /// ```
89
101
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
90
102
pub fn connect < A : ToSocketAddrs > ( addr : A ) -> io:: Result < TcpStream > {
91
103
super :: each_addr ( addr, net_imp:: TcpStream :: connect) . map ( TcpStream )
92
104
}
93
105
94
106
/// 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
+ /// ```
95
118
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
96
119
pub fn peer_addr ( & self ) -> io:: Result < SocketAddr > {
97
120
self . 0 . peer_addr ( )
98
121
}
99
122
100
123
/// 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
+ /// ```
101
135
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
102
136
pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
103
137
self . 0 . socket_addr ( )
@@ -107,7 +141,19 @@ impl TcpStream {
107
141
///
108
142
/// This function will cause all pending and future I/O on the specified
109
143
/// 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
+ /// ```
111
157
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
112
158
pub fn shutdown ( & self , how : Shutdown ) -> io:: Result < ( ) > {
113
159
self . 0 . shutdown ( how)
@@ -119,62 +165,131 @@ impl TcpStream {
119
165
/// object references. Both handles will read and write the same stream of
120
166
/// data, and options set on one stream will be propagated to the other
121
167
/// 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
+ /// ```
122
178
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
123
179
pub fn try_clone ( & self ) -> io:: Result < TcpStream > {
124
180
self . 0 . duplicate ( ) . map ( TcpStream )
125
181
}
126
182
127
183
/// Sets the read timeout to the timeout specified.
128
184
///
129
- /// If the value specified is `None`, then `read` calls will block
185
+ /// If the value specified is [ `None`] , then [ `read()`] calls will block
130
186
/// indefinitely. It is an error to pass the zero `Duration` to this
131
187
/// method.
132
188
///
133
189
/// # Note
134
190
///
135
191
/// Platforms may return a different error code whenever a read times out as
136
192
/// 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
+ /// ```
138
209
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
139
210
pub fn set_read_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
140
211
self . 0 . set_read_timeout ( dur)
141
212
}
142
213
143
214
/// Sets the write timeout to the timeout specified.
144
215
///
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
147
218
/// method.
148
219
///
149
220
/// # Note
150
221
///
151
222
/// Platforms may return a different error code whenever a write times out
152
223
/// 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
+ /// ```
154
241
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
155
242
pub fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
156
243
self . 0 . set_write_timeout ( dur)
157
244
}
158
245
159
246
/// Returns the read timeout of this socket.
160
247
///
161
- /// If the timeout is `None`, then `read` calls will block indefinitely.
248
+ /// If the timeout is [ `None`] , then [ `read()`] calls will block indefinitely.
162
249
///
163
250
/// # Note
164
251
///
165
252
/// 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
+ /// ```
166
267
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
167
268
pub fn read_timeout ( & self ) -> io:: Result < Option < Duration > > {
168
269
self . 0 . read_timeout ( )
169
270
}
170
271
171
272
/// Returns the write timeout of this socket.
172
273
///
173
- /// If the timeout is `None`, then `write` calls will block indefinitely.
274
+ /// If the timeout is [ `None`] , then [ `write()`] calls will block indefinitely.
174
275
///
175
276
/// # Note
176
277
///
177
278
/// 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
+ /// ```
178
293
#[ stable( feature = "socket_timeout" , since = "1.4.0" ) ]
179
294
pub fn write_timeout ( & self ) -> io:: Result < Option < Duration > > {
180
295
self . 0 . write_timeout ( )
@@ -187,6 +302,16 @@ impl TcpStream {
187
302
/// small amount of data. When not set, data is buffered until there is a
188
303
/// sufficient amount to send out, thereby avoiding the frequent sending of
189
304
/// 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
+ /// ```
190
315
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
191
316
pub fn set_nodelay ( & self , nodelay : bool ) -> io:: Result < ( ) > {
192
317
self . 0 . set_nodelay ( nodelay)
@@ -197,6 +322,17 @@ impl TcpStream {
197
322
/// For more information about this option, see [`set_nodelay`][link].
198
323
///
199
324
/// [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
+ /// ```
200
336
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
201
337
pub fn nodelay ( & self ) -> io:: Result < bool > {
202
338
self . 0 . nodelay ( )
@@ -206,6 +342,16 @@ impl TcpStream {
206
342
///
207
343
/// This value sets the time-to-live field that is used in every packet sent
208
344
/// 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
+ /// ```
209
355
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
210
356
pub fn set_ttl ( & self , ttl : u32 ) -> io:: Result < ( ) > {
211
357
self . 0 . set_ttl ( ttl)
@@ -216,6 +362,17 @@ impl TcpStream {
216
362
/// For more information about this option, see [`set_ttl`][link].
217
363
///
218
364
/// [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
+ /// ```
219
376
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
220
377
pub fn ttl ( & self ) -> io:: Result < u32 > {
221
378
self . 0 . ttl ( )
@@ -226,6 +383,16 @@ impl TcpStream {
226
383
/// This will retrieve the stored error in the underlying socket, clearing
227
384
/// the field in the process. This can be useful for checking errors between
228
385
/// 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
+ /// ```
229
396
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
230
397
pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
231
398
self . 0 . take_error ( )
@@ -235,6 +402,16 @@ impl TcpStream {
235
402
///
236
403
/// On Unix this corresponds to calling fcntl, and on Windows this
237
404
/// 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
+ /// ```
238
415
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
239
416
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
240
417
self . 0 . set_nonblocking ( nonblocking)
0 commit comments