Skip to content

Commit 1239e34

Browse files
committed
Add more std::io documentation.
This round: io::Result and the free functions.
1 parent cdcce3b commit 1239e34

File tree

3 files changed

+158
-19
lines changed

3 files changed

+158
-19
lines changed

src/libstd/io/error.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,37 @@ use option::Option::{self, Some, None};
1717
use result;
1818
use sys;
1919

20-
/// A type for results generated by I/O related functions where the `Err` type
21-
/// is hard-wired to `io::Error`.
20+
/// A specialized [`Result`][result] type for I/O operations.
21+
///
22+
/// [result]: ../result/enum.Result.html
23+
///
24+
/// This type is broadly used across `std::io` for any operation which may
25+
/// produce an error.
2226
///
2327
/// This typedef is generally used to avoid writing out `io::Error` directly and
24-
/// is otherwise a direct mapping to `std::result::Result`.
28+
/// is otherwise a direct mapping to `Result`.
29+
///
30+
/// While usual Rust style is to import types directly, aliases of `Result`
31+
/// often are not, to make it easier to distinguish between them. `Result` is
32+
/// generally assumed to be `std::result::Result`, and so users of this alias
33+
/// will generally use `io::Result` instead of shadowing the prelude's import
34+
/// of `std::result::Result`.
35+
///
36+
/// # Examples
37+
///
38+
/// A convenience function that bubbles an `io::Result` to its caller:
39+
///
40+
/// ```
41+
/// use std::io;
42+
///
43+
/// fn get_string() -> io::Result<String> {
44+
/// let mut buffer = String::new();
45+
///
46+
/// try!(io::stdin().read_line(&mut buffer));
47+
///
48+
/// Ok(buffer)
49+
/// }
50+
/// ```
2551
#[stable(feature = "rust1", since = "1.0.0")]
2652
pub type Result<T> = result::Result<T, Error>;
2753

src/libstd/io/stdio.rs

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,42 @@ pub struct StdinLock<'a> {
154154
inner: MutexGuard<'a, BufReader<Maybe<StdinRaw>>>,
155155
}
156156

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.
158158
///
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:
162180
///
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+
/// ```
166193
#[stable(feature = "rust1", since = "1.0.0")]
167194
pub fn stdin() -> Stdin {
168195
static INSTANCE: Lazy<Mutex<BufReader<Maybe<StdinRaw>>>> = Lazy::new(stdin_init);
@@ -298,13 +325,42 @@ pub struct StdoutLock<'a> {
298325
inner: ReentrantMutexGuard<'a, RefCell<LineWriter<Maybe<StdoutRaw>>>>,
299326
}
300327

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.
302329
///
303330
/// 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"));
306345
///
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+
/// ```
308364
#[stable(feature = "rust1", since = "1.0.0")]
309365
pub fn stdout() -> Stdout {
310366
static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>>
@@ -376,12 +432,38 @@ pub struct StderrLock<'a> {
376432
inner: ReentrantMutexGuard<'a, RefCell<Maybe<StderrRaw>>>,
377433
}
378434

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();
380461
///
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"));
383463
///
384-
/// The returned handle implements the `Write` trait.
464+
/// # Ok(())
465+
/// # }
466+
/// ```
385467
#[stable(feature = "rust1", since = "1.0.0")]
386468
pub fn stderr() -> Stderr {
387469
static INSTANCE: Lazy<ReentrantMutex<RefCell<Maybe<StderrRaw>>>> = Lazy::new(stderr_init);

src/libstd/io/util.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ use io::{self, Read, Write, ErrorKind, BufRead};
2828
/// This function will return an error immediately if any call to `read` or
2929
/// `write` returns an error. All instances of `ErrorKind::Interrupted` are
3030
/// handled by this function and the underlying operation is retried.
31+
///
32+
/// # Examples
33+
///
34+
/// ```
35+
/// use std::io;
36+
///
37+
/// # fn foo() -> io::Result<()> {
38+
/// let mut reader: &[u8] = b"hello";
39+
/// let mut writer: Vec<u8> = vec![];
40+
///
41+
/// try!(io::copy(&mut reader, &mut writer));
42+
///
43+
/// assert_eq!(reader, &writer[..]);
44+
/// # Ok(())
45+
/// # }
46+
/// ```
3147
#[stable(feature = "rust1", since = "1.0.0")]
3248
pub fn copy<R: Read, W: Write>(reader: &mut R, writer: &mut W) -> io::Result<u64> {
3349
let mut buf = [0; super::DEFAULT_BUF_SIZE];
@@ -48,9 +64,24 @@ pub fn copy<R: Read, W: Write>(reader: &mut R, writer: &mut W) -> io::Result<u64
4864
#[stable(feature = "rust1", since = "1.0.0")]
4965
pub struct Empty { _priv: () }
5066

51-
/// Creates an instance of an empty reader.
67+
/// Constructs a new handle to an empty reader.
5268
///
5369
/// All reads from the returned reader will return `Ok(0)`.
70+
///
71+
/// # Examples
72+
///
73+
/// A slightly sad example of not reading anything into a buffer:
74+
///
75+
/// ```
76+
/// use std::io;
77+
/// use std::io::Read;
78+
///
79+
/// # fn foo() -> io::Result<String> {
80+
/// let mut buffer = String::new();
81+
/// try!(io::empty().read_to_string(&mut buffer));
82+
/// # Ok(buffer)
83+
/// # }
84+
/// ```
5485
#[stable(feature = "rust1", since = "1.0.0")]
5586
pub fn empty() -> Empty { Empty { _priv: () } }
5687

0 commit comments

Comments
 (0)