Skip to content

Commit 103749d

Browse files
committed
Improve documentation for std::io::Cursor
Beef up the docs on the type, as well as adding examples for all methods.
1 parent 4e51763 commit 103749d

File tree

1 file changed

+140
-9
lines changed

1 file changed

+140
-9
lines changed

src/libstd/io/cursor.rs

+140-9
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,67 @@ use cmp;
1515
use io::{self, SeekFrom, Error, ErrorKind};
1616
use slice;
1717

18-
/// A `Cursor` is a type which wraps a non-I/O object to provide a `Seek`
18+
/// A `Cursor` wraps another type and provides it with a [`Seek`][seek]
1919
/// implementation.
2020
///
21-
/// Cursors are typically used with memory buffer objects in order to allow
22-
/// `Seek`, `Read`, and `Write` implementations. For example, common cursor types
23-
/// include `Cursor<Vec<u8>>` and `Cursor<&[u8]>`.
21+
/// [seek]: trait.Seek.html
2422
///
25-
/// Implementations of the I/O traits for `Cursor<T>` are currently not generic
26-
/// over `T` itself. Instead, specific implementations are provided for various
27-
/// in-memory buffer types like `Vec<u8>` and `&[u8]`.
23+
/// Cursors are typically used with in-memory buffers to allow them to
24+
/// implement `Read` and/or `Write`, allowing these buffers to be used
25+
/// anywhere you might use a reader or writer that does actual I/O.
26+
///
27+
/// The standard library implements some I/O traits on various types which
28+
/// are commonly used as a buffer, like `Cursor<Vec<u8>>` and `Cursor<&[u8]>`.
29+
///
30+
/// # Examples
31+
///
32+
/// We may want to write bytes to a [`File`][file] in our production
33+
/// code, but use an in-memory buffer in our tests. We can do this with
34+
/// `Cursor`:
35+
///
36+
/// [file]: ../fs/struct.File.html
37+
///
38+
/// ```no_run
39+
/// use std::io::prelude::*;
40+
/// use std::io::{self, SeekFrom};
41+
/// use std::fs::File;
42+
///
43+
/// // a library function we've written
44+
/// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
45+
/// try!(writer.seek(SeekFrom::End(-10)));
46+
///
47+
/// for i in 0..10 {
48+
/// try!(writer.write(&[i]));
49+
/// }
50+
///
51+
/// // all went well
52+
/// Ok(())
53+
/// }
54+
///
55+
/// # fn foo() -> io::Result<()> {
56+
/// // Here's some code that uses this library function.
57+
/// //
58+
/// // We might want to use a BufReader here for efficiency, but let's
59+
/// // keep this example focused.
60+
/// let mut file = try!(File::create("foo.txt"));
61+
///
62+
/// try!(write_ten_bytes_at_end(&mut file));
63+
/// # Ok(())
64+
/// # }
65+
///
66+
/// // now let's write a test
67+
/// #[test]
68+
/// fn test_writes_bytes() {
69+
/// // setting up a real File is much more slow than an in-memory buffer,
70+
/// // let's use a cursor instead
71+
/// use std::io::Cursor;
72+
/// let mut buff = Cursor::new(vec![0; 15]);
73+
///
74+
/// write_ten_bytes(&mut buff).unwrap();
75+
///
76+
/// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
77+
/// }
78+
/// ```
2879
#[stable(feature = "rust1", since = "1.0.0")]
2980
#[derive(Clone, Debug)]
3081
pub struct Cursor<T> {
@@ -34,31 +85,111 @@ pub struct Cursor<T> {
3485

3586
impl<T> Cursor<T> {
3687
/// Creates a new cursor wrapping the provided underlying I/O object.
88+
///
89+
/// # Examples
90+
///
91+
/// ```
92+
/// use std::io::Cursor;
93+
///
94+
/// let buff = Cursor::new(Vec::new());
95+
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
96+
/// # force_inference(&buff);
97+
/// ```
3798
#[stable(feature = "rust1", since = "1.0.0")]
3899
pub fn new(inner: T) -> Cursor<T> {
39100
Cursor { pos: 0, inner: inner }
40101
}
41102

42103
/// Consumes this cursor, returning the underlying value.
104+
///
105+
/// # Examples
106+
///
107+
/// ```
108+
/// use std::io::Cursor;
109+
///
110+
/// let buff = Cursor::new(Vec::new());
111+
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
112+
/// # force_inference(&buff);
113+
///
114+
/// let vec = buff.into_inner();
115+
/// ```
43116
#[stable(feature = "rust1", since = "1.0.0")]
44117
pub fn into_inner(self) -> T { self.inner }
45118

46119
/// Gets a reference to the underlying value in this cursor.
120+
///
121+
/// # Examples
122+
///
123+
/// ```
124+
/// use std::io::Cursor;
125+
///
126+
/// let buff = Cursor::new(Vec::new());
127+
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
128+
/// # force_inference(&buff);
129+
///
130+
/// let reference = buff.get_ref();
131+
/// ```
47132
#[stable(feature = "rust1", since = "1.0.0")]
48133
pub fn get_ref(&self) -> &T { &self.inner }
49134

50135
/// Gets a mutable reference to the underlying value in this cursor.
51136
///
52137
/// Care should be taken to avoid modifying the internal I/O state of the
53138
/// underlying value as it may corrupt this cursor's position.
139+
///
140+
/// # Examples
141+
///
142+
/// ```
143+
/// use std::io::Cursor;
144+
///
145+
/// let mut buff = Cursor::new(Vec::new());
146+
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
147+
/// # force_inference(&buff);
148+
///
149+
/// let reference = buff.get_mut();
150+
/// ```
54151
#[stable(feature = "rust1", since = "1.0.0")]
55152
pub fn get_mut(&mut self) -> &mut T { &mut self.inner }
56153

57-
/// Returns the current value of this cursor
154+
/// Returns the current position of this cursor.
155+
///
156+
/// # Examples
157+
///
158+
/// ```
159+
/// use std::io::Cursor;
160+
/// use std::io::prelude::*;
161+
/// use std::io::SeekFrom;
162+
///
163+
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
164+
///
165+
/// assert_eq!(buff.position(), 0);
166+
///
167+
/// buff.seek(SeekFrom::Current(2)).unwrap();
168+
/// assert_eq!(buff.position(), 2);
169+
///
170+
/// buff.seek(SeekFrom::Current(-1)).unwrap();
171+
/// assert_eq!(buff.position(), 1);
172+
/// ```
58173
#[stable(feature = "rust1", since = "1.0.0")]
59174
pub fn position(&self) -> u64 { self.pos }
60175

61-
/// Sets the value of this cursor
176+
/// Sets the position of this cursor.
177+
///
178+
/// # Examples
179+
///
180+
/// ```
181+
/// use std::io::Cursor;
182+
///
183+
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
184+
///
185+
/// assert_eq!(buff.position(), 0);
186+
///
187+
/// buff.set_position(2);
188+
/// assert_eq!(buff.position(), 2);
189+
///
190+
/// buff.set_position(4);
191+
/// assert_eq!(buff.position(), 4);
192+
/// ```
62193
#[stable(feature = "rust1", since = "1.0.0")]
63194
pub fn set_position(&mut self, pos: u64) { self.pos = pos; }
64195
}

0 commit comments

Comments
 (0)