Expand description
A contiguous growable byte string, written as Buf, short for “buffer”.
Buffers have O(1) indexing, amortized O(1) push (to the end) and O(1) pop (from the end).
Buffers ensure they never allocate more than isize::MAX bytes.
Buffers are transparent wrappers around Vec<u8> with a minimized API
sufficient for implementing the Ruby String type.
Buffers do not assume any encoding. Encoding is a higher-level concept that
should be built on top of Buf.
§Examples
You can explicitly create a Buf with Buf::new:
use scolapasta_strbuf::Buf;
let buf = Buf::new();You can push_byte bytes into the end of a buffer (which will grow the
buffer as needed):
use scolapasta_strbuf::Buf;
let mut buf = Buf::from(b"12");
buf.push_byte(b'3');
assert_eq!(buf, b"123");Popping bytes works in much the same way:
use scolapasta_strbuf::Buf;
let mut buf = Buf::from(b"12");
let alpha_two = buf.pop_byte();
assert_eq!(alpha_two, Some(b'2'));Buffers also support indexing (through the Index and IndexMut
traits):
use scolapasta_strbuf::Buf;
let mut buf = Buf::from(b"123");
let three = buf[2];
buf[1] = b'!';§Crate features
- std: Enabled by default. Implement
std::io::WriteforBuf. If this feature is disabled, this crate only depends onalloc. - nul-terminated: Use an alternate byte buffer backend that ensures
byte content is always followed by a NUL byte in the buffer’s spare
capacity. This feature can be used to ensure
Bufs are FFI compatible with C code that expects byte content to be NUL terminated.