@@ -154,15 +154,42 @@ pub struct StdinLock<'a> {
154
154
inner : MutexGuard < ' a , BufReader < Maybe < StdinRaw > > > ,
155
155
}
156
156
157
- /// Creates a new handle to the global standard input stream of this process.
157
+ /// Constructs a new handle to the standard input of the current process.
158
158
///
159
- /// The handle returned refers to a globally shared buffer between all threads.
160
- /// Access is synchronized and can be explicitly controlled with the `lock()`
161
- /// method.
159
+ /// Each handle returned is a reference to a shared global buffer whose access
160
+ /// is synchronized via a mutex. If you need more explicit control over
161
+ /// locking, see the [lock() method][lock].
162
+ ///
163
+ /// [lock]: struct.Stdin.html#method.lock
164
+ ///
165
+ /// # Examples
166
+ ///
167
+ /// Using implicit synchronization:
168
+ ///
169
+ /// ```
170
+ /// use std::io::{self, Read};
171
+ ///
172
+ /// # fn foo() -> io::Result<String> {
173
+ /// let mut buffer = String::new();
174
+ /// try!(io::stdin().read_to_string(&mut buffer));
175
+ /// # Ok(buffer)
176
+ /// # }
177
+ /// ```
178
+ ///
179
+ /// Using explicit synchronization:
162
180
///
163
- /// The `Read` trait is implemented for the returned value but the `BufRead`
164
- /// trait is not due to the global nature of the standard input stream. The
165
- /// locked version, `StdinLock`, implements both `Read` and `BufRead`, however.
181
+ /// ```
182
+ /// use std::io::{self, Read};
183
+ ///
184
+ /// # fn foo() -> io::Result<String> {
185
+ /// let mut buffer = String::new();
186
+ /// let stdin = io::stdin();
187
+ /// let mut handle = stdin.lock();
188
+ ///
189
+ /// try!(handle.read_to_string(&mut buffer));
190
+ /// # Ok(buffer)
191
+ /// # }
192
+ /// ```
166
193
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
167
194
pub fn stdin ( ) -> Stdin {
168
195
static INSTANCE : Lazy < Mutex < BufReader < Maybe < StdinRaw > > > > = Lazy :: new ( stdin_init) ;
@@ -298,13 +325,42 @@ pub struct StdoutLock<'a> {
298
325
inner : ReentrantMutexGuard < ' a , RefCell < LineWriter < Maybe < StdoutRaw > > > > ,
299
326
}
300
327
301
- /// Constructs a new reference to the standard output of the current process.
328
+ /// Constructs a new handle to the standard output of the current process.
302
329
///
303
330
/// Each handle returned is a reference to a shared global buffer whose access
304
- /// is synchronized via a mutex. Explicit control over synchronization is
305
- /// provided via the `lock` method.
331
+ /// is synchronized via a mutex. If you need more explicit control over
332
+ /// locking, see the [lock() method][lock].
333
+ ///
334
+ /// [lock]: struct.Stdout.html#method.lock
335
+ ///
336
+ /// # Examples
337
+ ///
338
+ /// Using implicit synchronization:
339
+ ///
340
+ /// ```
341
+ /// use std::io::{self, Write};
342
+ ///
343
+ /// # fn foo() -> io::Result<()> {
344
+ /// try!(io::stdout().write(b"hello world"));
306
345
///
307
- /// The returned handle implements the `Write` trait.
346
+ /// # Ok(())
347
+ /// # }
348
+ /// ```
349
+ ///
350
+ /// Using explicit synchronization:
351
+ ///
352
+ /// ```
353
+ /// use std::io::{self, Write};
354
+ ///
355
+ /// # fn foo() -> io::Result<()> {
356
+ /// let stdout = io::stdout();
357
+ /// let mut handle = stdout.lock();
358
+ ///
359
+ /// try!(handle.write(b"hello world"));
360
+ ///
361
+ /// # Ok(())
362
+ /// # }
363
+ /// ```
308
364
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
309
365
pub fn stdout ( ) -> Stdout {
310
366
static INSTANCE : Lazy < ReentrantMutex < RefCell < LineWriter < Maybe < StdoutRaw > > > > >
@@ -376,12 +432,38 @@ pub struct StderrLock<'a> {
376
432
inner : ReentrantMutexGuard < ' a , RefCell < Maybe < StderrRaw > > > ,
377
433
}
378
434
379
- /// Constructs a new reference to the standard error stream of a process.
435
+ /// Constructs a new handle to the standard error of the current process.
436
+ ///
437
+ /// This handle is not buffered.
438
+ ///
439
+ /// # Examples
440
+ ///
441
+ /// Using implicit synchronization:
442
+ ///
443
+ /// ```
444
+ /// use std::io::{self, Write};
445
+ ///
446
+ /// # fn foo() -> io::Result<()> {
447
+ /// try!(io::stderr().write(b"hello world"));
448
+ ///
449
+ /// # Ok(())
450
+ /// # }
451
+ /// ```
452
+ ///
453
+ /// Using explicit synchronization:
454
+ ///
455
+ /// ```
456
+ /// use std::io::{self, Write};
457
+ ///
458
+ /// # fn foo() -> io::Result<()> {
459
+ /// let stderr = io::stderr();
460
+ /// let mut handle = stderr.lock();
380
461
///
381
- /// Each returned handle is synchronized amongst all other handles created from
382
- /// this function. No handles are buffered, however.
462
+ /// try!(handle.write(b"hello world"));
383
463
///
384
- /// The returned handle implements the `Write` trait.
464
+ /// # Ok(())
465
+ /// # }
466
+ /// ```
385
467
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
386
468
pub fn stderr ( ) -> Stderr {
387
469
static INSTANCE : Lazy < ReentrantMutex < RefCell < Maybe < StderrRaw > > > > = Lazy :: new ( stderr_init) ;
0 commit comments