首页 > 编程语言 >0043-Bytes-bytes源码阅读

0043-Bytes-bytes源码阅读

时间:2022-09-30 20:22:46浏览次数:86  
标签:bytes self 0043 IntoIter Bytes 源码 inner into fn

环境

  • Time 2022-05-29
  • Rust 1.61.0
  • Bytes 1.1.0

前言

说明

参考:https://github.com/tokio-rs/bytes

目标

Bytes 实现迭代器。

IntoIter

#[derive(Debug)]
pub struct IntoIter<T> {
    inner: T,
}

impl

impl<T> IntoIter<T> {

    pub(crate) fn new(inner: T) -> IntoIter<T> {
        IntoIter { inner }
    }

    pub fn into_inner(self) -> T {
        self.inner
    }

    pub fn get_ref(&self) -> &T {
        &self.inner
    }

    pub fn get_mut(&mut self) -> &mut T {
        &mut self.inner
    }
}

实现迭代器

impl<T: Buf> Iterator for IntoIter<T> {
    type Item = u8;

    fn next(&mut self) -> Option<u8> {
        if !self.inner.has_remaining() {
            return None;
        }

        let b = self.inner.chunk()[0];
        self.inner.advance(1);

        Some(b)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let rem = self.inner.remaining();
        (rem, Some(rem))
    }
}
impl<T: Buf> ExactSizeIterator for IntoIter<T> {}

值迭代

impl IntoIterator for Bytes {
    type Item = u8;
    type IntoIter = IntoIter<Bytes>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self)
    }
}

引用迭代

impl<'a> IntoIterator for &'a Bytes {
    type Item = &'a u8;
    type IntoIter = core::slice::Iter<'a, u8>;

    fn into_iter(self) -> Self::IntoIter {
        self.as_slice().into_iter()
    }
}

FromIterator

impl FromIterator<u8> for Bytes {
    fn from_iter<T: IntoIterator<Item = u8>>(into_iter: T) -> Self {
        Vec::from_iter(into_iter).into()
    }
}

总结

Bytes 实现了迭代器的相关方法。

附录

标签:bytes,self,0043,IntoIter,Bytes,源码,inner,into,fn
From: https://www.cnblogs.com/jiangbo4444/p/16746009.html

相关文章