pub struct Buffer { /* private fields */ }Expand description
A Buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use.
zh-cn
Buffer是一个实现了读写方法的可变大小的字节缓冲。本类型的零值是一个空的可用于读写的缓冲。§Example
use gostd_io::*;
use gostd_bytes::Buffer;
let mut buf = Buffer::new();
buf.WriteString("hello");
buf.WriteByte(b' ');
buf.WriteString("world");
buf.WriteByte(b'!');
assert_eq!("hello world!", buf.String());
buf.Reset(); // clear 清空数据
for i in 'a'..='z' {
buf.WriteByte(i as u8);
}
assert_eq!("abcdefghijklmnopqrstuvwxyz", buf.String());
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn with_bytes(buf: Vec<byte>) -> Buffer
pub fn with_bytes(buf: Vec<byte>) -> Buffer
with_bytes creates and initializes a new Buffer using buf as its initial contents. The new Buffer takes ownership of buf, and the caller should not use buf after this call. with_bytes is intended to prepare a Buffer to read existing data. It can also be used to set the initial size of the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.
In most cases, new() is sufficient to initialize a Buffer.
zh-cn
使用buf作为初始内容创建并初始化一个Buffer。本函数用于创建一个用于读取已存在数据的buffer;也用于指定用于写入的内部缓冲的大小,此时,buf应为一个具有指定容量但长度为0的切片。buf会被作为返回值的底层缓冲切片。Sourcepub fn with_str(s: &str) -> Buffer
pub fn with_str(s: &str) -> Buffer
with_str creates and initializes a new Buffer using string s as its initial contents. It is intended to prepare a buffer to read an existing string.
In most cases, new() is sufficient to initialize a Buffer.
zh-cn
使用s作为初始内容创建并初始化一个Buffer。本函数用于创建一个用于读取已存在数据的buffer。Sourcepub fn Cap(&self) -> int
pub fn Cap(&self) -> int
Cap returns the capacity of the builder’s underlying byte slice. It is the total space allocated for the string being built and includes any bytes already written.
zh-cn
Cap返回构建器底层字节切片的容量。它是为正在生成的字符串分配的总空间,包括已写入的所有字节。Sourcepub fn Grow(&mut self, n: int)
pub fn Grow(&mut self, n: int)
Grow grows b’s capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to b without another allocation. If n is negative, Grow panics.