Struct Buffer

Source
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

Source

pub fn new() -> Buffer

new initialization a Buffer

zh-cn 初始化生成器
Source

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会被作为返回值的底层缓冲切片。
Source

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。
Source

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返回构建器底层字节切片的容量。它是为正在生成的字符串分配的总空间,包括已写入的所有字节。
Source

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.

zh-cn 如果需要的话,Grow会增加b的容量,以保证另一个n字节的空间。在Grow(n)之后,至少可以将n个字节写入b而无需另一次分配。如果n为负,则增加恐慌。
Source

pub fn Len(&self) -> int

Len returns the number of accumulated bytes; b.Len() == len!(b.String()).

zh-cn Len返回累计字节数;b.Len() == len!(b.String()).
Source

pub fn Reset(&mut self)

Reset resets the Builder to be empty.

zh-cn 重置将生成器重置为空。
Source

pub fn Bytes(&self) -> Vec<byte>

String returns the accumulated string.

zh-cn 返回累积的字节序列
Source

pub fn String(&self) -> String

String returns the accumulated string.

zh-cn 返回累积的字符串。
Source

pub fn WriteRune<'a>(&mut self, r: rune) -> Result<int, &'a str>

WriteRune appends the UTF-8 encoding of Unicode code point r to b’s buffer. It returns the length of r.

zh-cn 将Unicode代码点r的UTF-8编码附加到b的缓冲区。它返回r的长度。

Trait Implementations§

Source§

impl ByteWriter for Buffer

Source§

fn WriteByte(&mut self, c: byte) -> Result<(), Error>

WriteByte appends the byte c to b’s buffer.

zh-cn WriteByte将字节c追加到b的缓冲区。
Source§

impl Clone for Buffer

Source§

fn clone(&self) -> Buffer

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Buffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Buffer

Source§

fn default() -> Buffer

Returns the “default value” for a type. Read more
Source§

impl Display for Buffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Buffer

Source§

fn eq(&self, other: &Buffer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Buffer

Source§

fn partial_cmp(&self, other: &Buffer) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StringWriter for Buffer

Source§

fn WriteString(&mut self, s: &str) -> Result<int, Error>

WriteString appends the contents of s to b’s buffer. It returns the length of s.

zh-cn WriteString将s的内容附加到b的缓冲区。它返回s的长度。
Source§

impl Writer for Buffer

Source§

fn Write<'a>(&mut self, p: Vec<byte>) -> Result<int, Error>

Write appends the contents of p to b’s buffer. Write always returns len!(p).

zh-cn Write将p的内容附加到b的缓冲区, 写总是返回len!(p).
Source§

impl StructuralPartialEq for Buffer

Auto Trait Implementations§

§

impl Freeze for Buffer

§

impl RefUnwindSafe for Buffer

§

impl Send for Buffer

§

impl Sync for Buffer

§

impl Unpin for Buffer

§

impl UnwindSafe for Buffer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.