@@ -15,16 +15,67 @@ use cmp;
15
15
use io:: { self , SeekFrom , Error , ErrorKind } ;
16
16
use slice;
17
17
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]
19
19
/// implementation.
20
20
///
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
24
22
///
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
+ /// ```
28
79
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
29
80
#[ derive( Clone , Debug ) ]
30
81
pub struct Cursor < T > {
@@ -34,31 +85,111 @@ pub struct Cursor<T> {
34
85
35
86
impl < T > Cursor < T > {
36
87
/// 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
+ /// ```
37
98
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
38
99
pub fn new ( inner : T ) -> Cursor < T > {
39
100
Cursor { pos : 0 , inner : inner }
40
101
}
41
102
42
103
/// 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
+ /// ```
43
116
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
44
117
pub fn into_inner ( self ) -> T { self . inner }
45
118
46
119
/// 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
+ /// ```
47
132
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
48
133
pub fn get_ref ( & self ) -> & T { & self . inner }
49
134
50
135
/// Gets a mutable reference to the underlying value in this cursor.
51
136
///
52
137
/// Care should be taken to avoid modifying the internal I/O state of the
53
138
/// 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
+ /// ```
54
151
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
55
152
pub fn get_mut ( & mut self ) -> & mut T { & mut self . inner }
56
153
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
+ /// ```
58
173
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
59
174
pub fn position ( & self ) -> u64 { self . pos }
60
175
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
+ /// ```
62
193
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
63
194
pub fn set_position ( & mut self , pos : u64 ) { self . pos = pos; }
64
195
}
0 commit comments