use futures_core::task::{Context, Poll}; use futures_io::{AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, IoSlice, IoSliceMut, SeekFrom}; use std::io; use std::pin::Pin;
/// A `Cursor` wraps an in-memory buffer and provides it with a /// [`AsyncSeek`] implementation. /// /// `Cursor`s are used with in-memory buffers, anything implementing /// `AsRef<[u8]>`, to allow them to implement [`AsyncRead`] and/or [`AsyncWrite`], /// allowing these buffers to be used anywhere you might use a reader or writer /// that does actual I/O. /// /// This library implements some I/O traits on various types which /// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and /// `Cursor<`[`&[u8]`][bytes]`>`. /// /// [`AsyncSeek`]: trait.AsyncSeek.html /// [`AsyncRead`]: trait.AsyncRead.html /// [`AsyncWrite`]: trait.AsyncWrite.html /// [bytes]: https://doc.rust-lang.org/std/primitive.slice.html #[derive(Clone, Debug, Default)] pubstruct Cursor<T> {
inner: io::Cursor<T>,
}
impl<T> Cursor<T> { /// Creates a new cursor wrapping the provided underlying in-memory buffer. /// /// Cursor initial position is `0` even if underlying buffer (e.g., `Vec`) /// is not empty. So writing to cursor starts with overwriting `Vec` /// content, not with appending to it. /// /// # Examples /// /// ``` /// use futures::io::Cursor; /// /// let buff = Cursor::new(Vec::new()); /// # fn force_inference(_: &Cursor<Vec<u8>>) {} /// # force_inference(&buff); /// ``` pubfn new(inner: T) -> Self { Self { inner: io::Cursor::new(inner) }
}
/// Consumes this cursor, returning the underlying value. /// /// # Examples /// /// ``` /// use futures::io::Cursor; /// /// let buff = Cursor::new(Vec::new()); /// # fn force_inference(_: &Cursor<Vec<u8>>) {} /// # force_inference(&buff); /// /// let vec = buff.into_inner(); /// ``` pubfn into_inner(self) -> T { self.inner.into_inner()
}
/// Gets a reference to the underlying value in this cursor. /// /// # Examples /// /// ``` /// use futures::io::Cursor; /// /// let buff = Cursor::new(Vec::new()); /// # fn force_inference(_: &Cursor<Vec<u8>>) {} /// # force_inference(&buff); /// /// let reference = buff.get_ref(); /// ``` pubfn get_ref(&self) -> &T { self.inner.get_ref()
}
/// Gets a mutable reference to the underlying value in this cursor. /// /// Care should be taken to avoid modifying the internal I/O state of the /// underlying value as it may corrupt this cursor's position. /// /// # Examples /// /// ``` /// use futures::io::Cursor; /// /// let mut buff = Cursor::new(Vec::new()); /// # fn force_inference(_: &Cursor<Vec<u8>>) {} /// # force_inference(&buff); /// /// let reference = buff.get_mut(); /// ``` pubfn get_mut(&mutself) -> &mut T { self.inner.get_mut()
}
/// Returns the current position of this cursor. /// /// # Examples /// /// ``` /// # futures::executor::block_on(async { /// use futures::io::{AsyncSeekExt, Cursor, SeekFrom}; /// /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]); /// /// assert_eq!(buff.position(), 0); /// /// buff.seek(SeekFrom::Current(2)).await?; /// assert_eq!(buff.position(), 2); /// /// buff.seek(SeekFrom::Current(-1)).await?; /// assert_eq!(buff.position(), 1); /// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap(); /// ``` pubfn position(&self) -> u64 { self.inner.position()
}
/// Sets the position of this cursor. /// /// # Examples /// /// ``` /// use futures::io::Cursor; /// /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]); /// /// assert_eq!(buff.position(), 0); /// /// buff.set_position(2); /// assert_eq!(buff.position(), 2); /// /// buff.set_position(4); /// assert_eq!(buff.position(), 4); /// ``` pubfn set_position(&mutself, pos: u64) { self.inner.set_position(pos)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.