| Component | Kind | Backing storage | Role |
|---|---|---|---|
| type alias | stack array |
Fixed-size record manipulated through free functions | |
| struct | Same fixed size, boxed on the heap, with methods | ||
| struct | Variable-size message with a type tag and content-size header |
| Constant | Value | Meaning |
|---|---|---|
| Fixed length of |
||
| Suggested maximum total |
# Build the library cargo build # Run the bundled example cargo run --example test1 # Run the unit tests cargo test
[dependencies]
rust_byte_record = { path = "../RustByteRecord" }
use rust_byte_record::*;
fn main() {
let mut br: ByteRecord = [0; BYTE_RECORD_SIZE];
set_field(&mut br, 2, &usize_to_bytes(255));
show_record(&br, 8);
}
| Function | Signature | Purpose |
|---|---|---|
| 8-byte slice back to |
||
| 4-byte slice back to |
||
| Borrow the UTF-8 bytes of a |
||
| Decode a slice as UTF-8 | ||
| Zero every byte of a |
||
| Copy |
||
| Print a folded, decimal dump of the record |
pub const BYTE_RECORD_SIZE: usize = 32; pub type ByteRecord = [u8; BYTE_RECORD_SIZE];
let mut br: ByteRecord = [0; BYTE_RECORD_SIZE]; let buff: [u8; 4] = [1, 2, 3, 4]; set_field(&mut br, 3, &buff); show_record(&br, 8);
| Method | Purpose |
|---|---|
| Allocate a zeroed boxed array | |
| Length helpers | |
| Borrow the underlying | |
| Zero every byte | |
| Copy | |
| Borrow a sub-slice | |
| Write the UTF-8 bytes of | |
| Decode a sub-slice as UTF-8 | |
| Folded decimal dump |
let mut ba = ByteArray::new(); ba.set_str(4, "a literal string"); let s = ba.get_str(4, "a literal string".len()).unwrap();
+--------+----------------------+--------------------------+ | type | content size | content | | 1 byte | 8 bytes (BE usize) | content_size bytes | +--------+----------------------+--------------------------+The first byte is an
pub enum MsgType {
DEFAULT = 0,
TEXT = 1,
REPLY = 2,
END = 4,
QUIT = 8,
}
The discriminants are powers of two so callers may treat them as bit flags
when that is useful.
| Method | Purpose |
|---|---|
| Allocate a message buffer of | |
| Re-zero an existing message in place, preserving its length | |
| Length helpers | |
| Read or write the type tag | |
| Write the payload bytes and update the content-size header | |
| Borrow the current payload as | |
| Write a | |
| Decode the payload as UTF-8 | |
| Folded decimal dump of the entire buffer |
| Method | Purpose |
|---|---|
| Raw byte read/write | |
| Read or write just the size header | |
| Raw UTF-8 read/write | |
| Borrow the entire backing buffer as |
let mut msg = Message::new(32);
msg.set_type(MsgType::TEXT);
msg.set_content_str("a literal string");
let s = msg.get_content_str().unwrap();
cargo run --example test1
# Build cargo build # Run the bundled example cargo run --example test1 # Unit tests cargo test