首页 > 其他分享 >0048-Bytes-BufMut使用

0048-Bytes-BufMut使用

时间:2022-09-30 20:35:25浏览次数:85  
标签:0048 mut .. Bytes assert let put buf BufMut

环境

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

前言

说明

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

目标

使用 BufMut 的方法。

remaining_mut

fn main() {
    let mut dst = [0; 10];
    let mut buf = &mut dst[..];
    println!("{:?}", buf.remaining_mut());
    buf.put(&b"hello"[..]);
    println!("{:?}", buf.remaining_mut());
}

new

fn main() {
    let mut buf = Vec::with_capacity(16);
    buf.chunk_mut()[0..2].copy_from_slice(b"he");
    unsafe { buf.advance_mut(2) };
    buf.chunk_mut()[0..3].copy_from_slice(b"llo");

    unsafe {
        buf.advance_mut(3);
    }

    assert_eq!(5, buf.len());
    assert_eq!(buf, b"hello");
}

chunk_mut

fn main() {
    let mut buf = Vec::with_capacity(16);
    unsafe {
        buf.chunk_mut()[0..].as_mut_ptr().write(b'h');
        buf.chunk_mut()[1..].as_mut_ptr().write(b'e');
        buf.advance_mut(2);
        buf.chunk_mut()[0..].as_mut_ptr().write(b'l');
        buf.chunk_mut()[1..].as_mut_ptr().write(b'l');
        buf.chunk_mut()[2..].as_mut_ptr().write(b'o');

        buf.advance_mut(3);
    }

    assert_eq!(5, buf.len());
    assert_eq!(buf, b"hello");
}

has_remaining_mut

fn main() {
    let mut dst = [0; 5];
    let buf = &mut dst[..];
    println!("{:?}", buf.has_remaining_mut());
}

put

fn main() {
    let mut buf = vec![];

    buf.put_u8(b'h');
    buf.put(&b"ello"[..]);
    buf.put(b" world".as_ref());
    assert_eq!(buf, b"hello world");
}

put_slice

fn main() {
    let mut dst = [0; 6];
    let mut buf = &mut dst[..];
    buf.put_slice(b"hello");
    assert_eq!(1, buf.remaining_mut());
    assert_eq!(b"hello\0", &dst);
}

put_bytes

fn main() {
    let mut dst = [0; 6];
    let mut buf = &mut dst[..];
    buf.put_bytes(b'a', 4);
    assert_eq!(2, buf.remaining_mut());
    assert_eq!(b"aaaa\0\0", &dst);
}

put_u8

fn main() {
    let mut buf = vec![];
    buf.put_u8(0x01);
    assert_eq!(buf, b"\x01");
}

总结

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

附录

标签:0048,mut,..,Bytes,assert,let,put,buf,BufMut
From: https://www.cnblogs.com/jiangbo4444/p/16746031.html

相关文章

  • 0043-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/bytes目标Bytes实现迭代器。IntoIter#[derive(Debug)]pubstructInt......
  • Specified key was too long; max key length is 767 bytes错误的原因
    将mysql数据库里某个UNIQUE唯一索引字段从utf8改为utf8mb4时提示1071-Specifiedkeywastoolong;maxkeylengthis767bytes,来看看这个错误的来原因。来几个知识点......
  • 实例-rust-string和bytes转换
    Cargo.toml[package]name="rust-example9"version="0.1.0"edition="2021"#Seemorekeysandtheirdefinitionsathttps://doc.rust-lang.org/cargo/refere......
  • 0042-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/byteshttps://zhuanlan.zhihu.com/p/109977513目标之前阅读的部分,都......
  • TypeError: Object of type 'bytes' is not JSON serializable
    转载自: https://blog.csdn.net/weixin_41951954/article/details/124838931   ......
  • [Bug0048] Redis安装成功后Warning: no config file specified, using the default co
    1、问题redis启动错误:Warning:noconfigfilespecified,usingthedefaultconfig.Inordertospecifyaconfig2、场景迁移环境,新windows环境下双击redis-serve......
  • 0039-Bytes-bytes源码阅读
    环境Time2022-05-28Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/bytes目标实现bytes.rs中的一部分方法。线程安全实现了两个线程安全......
  • 0040-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/byteshttps://zhuanlan.zhihu.com/p/109977513目标之前阅读的部分,都......
  • 0041-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/byteshttps://zhuanlan.zhihu.com/p/109977513目标之前阅读的部分,都......
  • 0035-Bytes-bytes源码阅读
    环境Time2022-05-28Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/bytes目标了解从静态生命周期的字节中创建bytes.rs,以及实现一部分方法。......