ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

0039-Bytes-bytes源码阅读

2022-08-30 08:32:56  阅读:208  来源: 互联网

标签:slice Bytes self bytes 源码 fn other impl


环境

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

前言

说明

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

目标

实现 bytes.rs 中的一部分方法。

线程安全

实现了两个线程安全的标记接口。

unsafe impl Send for Bytes {}
unsafe impl Sync for Bytes {}

Hash

实现了 Hash 函数。

impl hash::Hash for Bytes {
    fn hash<H>(&self, state: &mut H)
    where
        H: hash::Hasher,
    {
        self.as_slice().hash(state);
    }
}

Borrow

impl Borrow<[u8]> for Bytes {
    fn borrow(&self) -> &[u8] {
        self.as_slice()
    }
}

PartialEq

Bytes 实现了很多类型的比较方法,主要是方便对不同类型直接进行比较,下面只列出自己和自己的比较。

impl PartialEq for Bytes {
    fn eq(&self, other: &Bytes) -> bool {
        self.as_slice() == other.as_slice()
    }
}

PartialOrd

impl PartialOrd for Bytes {
    fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
        self.as_slice().partial_cmp(other.as_slice())
    }
}

Ord

impl Ord for Bytes {
    fn cmp(&self, other: &Bytes) -> cmp::Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}

impl Eq for Bytes {}

Vtable

Vtable 实现 Debug

impl fmt::Debug for Vtable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Vtable")
            .field("clone", &(self.clone as *const ()))
            .field("drop", &(self.drop as *const ()))
            .finish()
    }
}

总结

Bytes 实现了 HashBorrowEq 等方法。

附录

标签:slice,Bytes,self,bytes,源码,fn,other,impl
来源: https://www.cnblogs.com/jiangbo4444/p/16638032.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有