core/io/cursor.rs
1/// A `Cursor` wraps an in-memory buffer and provides it with a
2/// [`Seek`] implementation.
3///
4/// `Cursor`s are used with in-memory buffers, anything implementing
5/// <code>[AsRef]<\[u8]></code>, to allow them to implement [`Read`] and/or [`Write`],
6/// allowing these buffers to be used anywhere you might use a reader or writer
7/// that does actual I/O.
8///
9/// The standard library implements some I/O traits on various types which
10/// are commonly used as a buffer, like <code>Cursor<[Vec]\<u8>></code> and
11/// <code>Cursor<[&\[u8\]][bytes]></code>.
12///
13/// # Examples
14///
15/// We may want to write bytes to a [`File`] in our production
16/// code, but use an in-memory buffer in our tests. We can do this with
17/// `Cursor`:
18///
19/// [bytes]: crate::slice "slice"
20/// [`File`]: ../../std/fs/struct.File.html
21/// [`Read`]: ../../std/io/trait.Read.html
22/// [`Write`]: ../../std/io/trait.Write.html
23/// [`Seek`]: ../../std/io/trait.Seek.html
24/// [Vec]: ../../alloc/vec/struct.Vec.html
25///
26/// ```no_run
27/// use std::io::prelude::*;
28/// use std::io::{self, SeekFrom};
29/// use std::fs::File;
30///
31/// // a library function we've written
32/// fn write_ten_bytes_at_end<W: Write + Seek>(mut writer: W) -> io::Result<()> {
33/// writer.seek(SeekFrom::End(-10))?;
34///
35/// for i in 0..10 {
36/// writer.write(&[i])?;
37/// }
38///
39/// // all went well
40/// Ok(())
41/// }
42///
43/// # fn foo() -> io::Result<()> {
44/// // Here's some code that uses this library function.
45/// //
46/// // We might want to use a BufReader here for efficiency, but let's
47/// // keep this example focused.
48/// let mut file = File::create("foo.txt")?;
49/// // First, we need to allocate 10 bytes to be able to write into.
50/// file.set_len(10)?;
51///
52/// write_ten_bytes_at_end(&mut file)?;
53/// # Ok(())
54/// # }
55///
56/// // now let's write a test
57/// #[test]
58/// fn test_writes_bytes() {
59/// // setting up a real File is much slower than an in-memory buffer,
60/// // let's use a cursor instead
61/// use std::io::Cursor;
62/// let mut buff = Cursor::new(vec![0; 15]);
63///
64/// write_ten_bytes_at_end(&mut buff).unwrap();
65///
66/// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
67/// }
68/// ```
69#[stable(feature = "rust1", since = "1.0.0")]
70#[derive(Debug, Default, Eq, PartialEq)]
71pub struct Cursor<T> {
72 inner: T,
73 pos: u64,
74}
75
76impl<T> Cursor<T> {
77 /// Creates a new cursor wrapping the provided underlying in-memory buffer.
78 ///
79 /// Cursor initial position is `0` even if underlying buffer (e.g., [`Vec`])
80 /// is not empty. So writing to cursor starts with overwriting [`Vec`]
81 /// content, not with appending to it.
82 ///
83 /// [`Vec`]: ../../alloc/vec/struct.Vec.html
84 ///
85 /// # Examples
86 ///
87 /// ```
88 /// use std::io::Cursor;
89 ///
90 /// let buff = Cursor::new(Vec::new());
91 /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
92 /// # force_inference(&buff);
93 /// ```
94 #[stable(feature = "rust1", since = "1.0.0")]
95 #[rustc_const_stable(feature = "const_io_structs", since = "1.79.0")]
96 pub const fn new(inner: T) -> Cursor<T> {
97 Cursor { pos: 0, inner }
98 }
99
100 /// Consumes this cursor, returning the underlying value.
101 ///
102 /// # Examples
103 ///
104 /// ```
105 /// use std::io::Cursor;
106 ///
107 /// let buff = Cursor::new(Vec::new());
108 /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
109 /// # force_inference(&buff);
110 ///
111 /// let vec = buff.into_inner();
112 /// ```
113 #[stable(feature = "rust1", since = "1.0.0")]
114 pub fn into_inner(self) -> T {
115 self.inner
116 }
117
118 /// Gets a reference to the underlying value in this cursor.
119 ///
120 /// # Examples
121 ///
122 /// ```
123 /// use std::io::Cursor;
124 ///
125 /// let buff = Cursor::new(Vec::new());
126 /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
127 /// # force_inference(&buff);
128 ///
129 /// let reference = buff.get_ref();
130 /// ```
131 #[stable(feature = "rust1", since = "1.0.0")]
132 #[rustc_const_stable(feature = "const_io_structs", since = "1.79.0")]
133 pub const fn get_ref(&self) -> &T {
134 &self.inner
135 }
136
137 /// Gets a mutable reference to the underlying value in this cursor.
138 ///
139 /// Care should be taken to avoid modifying the internal I/O state of the
140 /// underlying value as it may corrupt this cursor's position.
141 ///
142 /// # Examples
143 ///
144 /// ```
145 /// use std::io::Cursor;
146 ///
147 /// let mut buff = Cursor::new(Vec::new());
148 /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
149 /// # force_inference(&buff);
150 ///
151 /// let reference = buff.get_mut();
152 /// ```
153 #[stable(feature = "rust1", since = "1.0.0")]
154 #[rustc_const_stable(feature = "const_mut_cursor", since = "1.86.0")]
155 pub const fn get_mut(&mut self) -> &mut T {
156 &mut self.inner
157 }
158
159 /// Returns the current position of this cursor.
160 ///
161 /// # Examples
162 ///
163 /// ```
164 /// use std::io::Cursor;
165 /// use std::io::prelude::*;
166 /// use std::io::SeekFrom;
167 ///
168 /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
169 ///
170 /// assert_eq!(buff.position(), 0);
171 ///
172 /// buff.seek(SeekFrom::Current(2)).unwrap();
173 /// assert_eq!(buff.position(), 2);
174 ///
175 /// buff.seek(SeekFrom::Current(-1)).unwrap();
176 /// assert_eq!(buff.position(), 1);
177 /// ```
178 #[stable(feature = "rust1", since = "1.0.0")]
179 #[rustc_const_stable(feature = "const_io_structs", since = "1.79.0")]
180 pub const fn position(&self) -> u64 {
181 self.pos
182 }
183
184 /// Sets the position of this cursor.
185 ///
186 /// # Examples
187 ///
188 /// ```
189 /// use std::io::Cursor;
190 ///
191 /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
192 ///
193 /// assert_eq!(buff.position(), 0);
194 ///
195 /// buff.set_position(2);
196 /// assert_eq!(buff.position(), 2);
197 ///
198 /// buff.set_position(4);
199 /// assert_eq!(buff.position(), 4);
200 /// ```
201 #[stable(feature = "rust1", since = "1.0.0")]
202 #[rustc_const_stable(feature = "const_mut_cursor", since = "1.86.0")]
203 pub const fn set_position(&mut self, pos: u64) {
204 self.pos = pos;
205 }
206
207 #[doc(hidden)]
208 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
209 #[inline]
210 pub const fn into_parts_mut(&mut self) -> (&mut u64, &mut T) {
211 (&mut self.pos, &mut self.inner)
212 }
213}
214
215impl<T> Cursor<T>
216where
217 T: AsRef<[u8]>,
218{
219 /// Splits the underlying slice at the cursor position and returns them.
220 ///
221 /// # Examples
222 ///
223 /// ```
224 /// #![feature(cursor_split)]
225 /// use std::io::Cursor;
226 ///
227 /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
228 ///
229 /// assert_eq!(buff.split(), ([].as_slice(), [1, 2, 3, 4, 5].as_slice()));
230 ///
231 /// buff.set_position(2);
232 /// assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));
233 ///
234 /// buff.set_position(6);
235 /// assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));
236 /// ```
237 #[unstable(feature = "cursor_split", issue = "86369")]
238 pub fn split(&self) -> (&[u8], &[u8]) {
239 let slice = self.inner.as_ref();
240 let pos = self.pos.min(slice.len() as u64);
241 slice.split_at(pos as usize)
242 }
243}
244
245impl<T> Cursor<T>
246where
247 T: AsMut<[u8]>,
248{
249 /// Splits the underlying slice at the cursor position and returns them
250 /// mutably.
251 ///
252 /// # Examples
253 ///
254 /// ```
255 /// #![feature(cursor_split)]
256 /// use std::io::Cursor;
257 ///
258 /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
259 ///
260 /// assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
261 ///
262 /// buff.set_position(2);
263 /// assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));
264 ///
265 /// buff.set_position(6);
266 /// assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));
267 /// ```
268 #[unstable(feature = "cursor_split", issue = "86369")]
269 pub fn split_mut(&mut self) -> (&mut [u8], &mut [u8]) {
270 let slice = self.inner.as_mut();
271 let pos = self.pos.min(slice.len() as u64);
272 slice.split_at_mut(pos as usize)
273 }
274}
275
276#[stable(feature = "rust1", since = "1.0.0")]
277impl<T> Clone for Cursor<T>
278where
279 T: Clone,
280{
281 #[inline]
282 fn clone(&self) -> Self {
283 Cursor { inner: self.inner.clone(), pos: self.pos }
284 }
285
286 #[inline]
287 fn clone_from(&mut self, other: &Self) {
288 self.inner.clone_from(&other.inner);
289 self.pos = other.pos;
290 }
291}