Skip to main content

Cursor

Struct Cursor 

Source
pub struct Cursor<'a> { /* private fields */ }
Expand description

顺序读取小端序整数的游标。

每个方法都做边界检查并返回结构化错误,避免 panic —— 解析不可信输入时这点很重要。

Implementations§

Source§

impl<'a> Cursor<'a>

Source

pub const fn new(data: &'a [u8], context: &'static str) -> Self

新建游标。

Source

pub const fn position(&self) -> usize

当前偏移量。

Source

pub const fn remaining(&self) -> usize

剩余字节数。

Source

pub fn skip(&mut self, n: usize) -> Result<()>

跳过 n 字节。

§Errors

剩余不足时返回 Error::Truncated。

Source

pub fn take(&mut self, n: usize) -> Result<&'a [u8]>

取出 n 字节切片。

§Errors

剩余不足时返回 Error::Truncated。

Source

pub fn take_array<const N: usize>(&mut self) -> Result<[u8; N]>

取出固定长度数组。

§Errors

剩余不足时返回 Error::Truncated。

Source

pub fn u8(&mut self) -> Result<u8>

读 u8。

§Errors

剩余不足时返回 Error::Truncated。

Source

pub fn u16le(&mut self) -> Result<u16>

读小端序 u16。

§Errors

剩余不足时返回 Error::Truncated。

Source

pub fn u32le(&mut self) -> Result<u32>

读小端序 u32。

§Errors

剩余不足时返回 Error::Truncated。

Source

pub fn u64le(&mut self) -> Result<u64>

读小端序 u64。

§Errors

剩余不足时返回 Error::Truncated。

Auto Trait Implementations§

§

impl<'a> Forget for Cursor<'a>

§

impl<'a> Freeze for Cursor<'a>

§

impl<'a> RefUnwindSafe for Cursor<'a>

§

impl<'a> Send for Cursor<'a>

§

impl<'a> Sync for Cursor<'a>

§

impl<'a> Unpin for Cursor<'a>

§

impl<'a> UnsafeUnpin for Cursor<'a>

§

impl<'a> UnwindSafe for Cursor<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.