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

0046-Bytes-bytes源码阅读

时间:2022-09-30 20:36:09浏览次数:75  
标签:ref bytes JiangBo Bytes 源码 let println buf

环境

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

前言

说明

参考:https://docs.rs/bytes/latest/bytes/trait.Buf.html

目标

Bytes 实现了 Buf,使用一下其中的方法。

remaining

fn main() {
    let mut buf = b"JiangBo".as_ref();
    println!("{:?}", buf.remaining());
    println!("{:?}", buf.get_u8() as char);
    println!("{:?}", buf.remaining());
}

chunk

fn main() {
    let buf = b"JiangBo".as_ref();
    println!("{:?}", buf.chunk());
}

advance

fn main() {
    let mut buf = b"JiangBo".as_ref();
    buf.advance(5);
    println!("{:?}", buf);
}

copy_to_slice

fn main() {
    let mut buf = b"JiangBo".as_ref();
    let mut dst = [0; 5];
    buf.copy_to_slice(&mut dst);
    println!("{:?}", Bytes::from(buf));
}

get_u8

fn main() {
    let mut buf = b"JiangBo".as_ref();
    println!("{:?}", buf.get_u8() as char);
    println!("{:?}", Bytes::from(buf));
}

copy_to_bytes

fn main() {
    let mut buf = b"JiangBo".as_ref();
    let bytes = buf.copy_to_bytes(5);
    println!("{:?}", bytes);
}

take

fn main() {
    let buf = b"JiangBo".as_ref();
    println!("{:?}", buf.take(5));
}

chain

fn main() {
    let buf = b"Hello ".as_ref();
    let mut chain = buf.chain(b"JiangBo".as_ref());
    let bytes = chain.copy_to_bytes(13);
    println!("{:?}", bytes);
}

总结

使用了 Buf 中定义的一些方法。

附录

标签:ref,bytes,JiangBo,Bytes,源码,let,println,buf
From: https://www.cnblogs.com/jiangbo4444/p/16746028.html

相关文章