#![doc = include_str!("../README.md")]
#![no_std]
use core::fmt;
pub struct Cursor<T> {
pub data: T,
pub offset: usize,
}
impl<'a, T> Cursor<&'a [T]> {
#[inline]
pub fn remaining_slice(&self) -> &'a [T] {
unsafe { self.data.get_unchecked(self.offset.min(self.data.len())..) }
}
#[inline]
pub fn read_slice(&mut self, len: usize) -> Option<&'a [T]> {
let total_len = self.offset + len;
let slice = self.data.get(self.offset..total_len)?;
self.offset = total_len;
Some(slice)
}
}
impl<T> Cursor<T> {
#[inline]
pub const fn new(data: T) -> Self {
Self { data, offset: 0 }
}
}
impl<T> From<T> for Cursor<T> {
#[inline]
fn from(data: T) -> Self {
Self::new(data)
}
}
impl<T: Clone> Clone for Cursor<T> {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
offset: self.offset,
}
}
}
impl<T: fmt::Debug> fmt::Debug for Cursor<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Cursor")
.field("data", &self.data)
.field("offset", &self.offset)
.finish()
}
}