0038-Bytes-bytes源码阅读
环境
- Time 2022-05-28
- Rust 1.61.0
- Bytes 1.1.0
前言
说明
参考:https://github.com/tokio-rs/bytes
目标
实现 bytes.rs
中的一部分方法。
split_off
在中间进行切割,分成两半。
pub fn split_off(&mut self, at: usize) -> Bytes {
// 分割的长度必须小于等于Bytes的长度
assert!(
at <= self.len(),
"split_off out of bounds: {:?} <= {:?}",
at,
self.len(),
);
// 如果从最后一位切割,相当于被切的不变,返回一个空的Bytes
if at == self.len() {
return Bytes::new();
}
// 如果从头开始切换,相当于被切的变成了空,返回被切的Bytes
if at == 0 {
return mem::replace(self, Bytes::new());
}
// 复制不会底层字节,只会复制四个usize的长度
let mut ret = self.clone();
// 被切割了,所以长度变短了
self.len = at;
// 把指向开头的指针移动到被切割的位置,并且变更长度
unsafe { ret.inc_start(at) };
ret
}
split_to
split_off
是把后面的切割下来,前面的保留,而 split_to
相反。
pub fn split_to(&mut self, at: usize) -> Bytes {
assert!(
at <= self.len(),
"split_to out of bounds: {:?} <= {:?}",
at,
self.len(),
);
if at == self.len() {
return mem::replace(self, Bytes::new());
}
if at == 0 {
return Bytes::new();
}
let mut ret = self.clone();
unsafe { self.inc_start(at) };
ret.len = at;
ret
}
Default
impl Default for Bytes {
fn default() -> Bytes {
Bytes::new()
}
}
Default 使用
fn main() {
let _ = Bytes::default();
}
from
impl From<&'static [u8]> for Bytes {
fn from(slice: &'static [u8]) -> Bytes {
Bytes::from_static(slice)
}
}
impl From<&'static str> for Bytes {
fn from(slice: &'static str) -> Bytes {
Bytes::from_static(slice.as_bytes())
}
}
from 使用
fn main() {
let _: Bytes = "Hello, world!".into();
let _: Bytes = b"Hello, world!".as_slice().into();
}
总结
给 Bytes
实现了 split_off
,split_to
,from
等方法。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-08-30 【JavaScript】String 实例方法(三)