Crate padder

Source
Expand description

A highly efficient Rust crate for padding data during runtime.

padder is a lightweight Rust crate for padding and formatting data structures at runtime efficiently. It provides fine-grained control over alignment, truncating strategies, padding, and memory allocation - making it ideal for performance-critical applications.

Unlike the builtin format! macro, padder avoids unneccessary repeated heap allocations and lets you pad and format directly into preallocated buffers, or modify buffers in-place.

Fully UTF-8 compatible - padder works seamlessly with any Unicode characters like emojis (πŸ‰), Japanese kana/kanji (こんにけは), or other multibyte symbols, when operating on String types.

Β§Features

  • Pad strings, slices, and vectors with custom alignment and width.
  • Zero-cost abstractions via the Source and MutableSource traits.
  • Pad directly into buffers for fine-grained heap allocation control.
  • Highly extensible to custom types through the provided traits.

Β§Usage

use padder::*;

let mut string = String::from("kratos");
(&mut string).pad(10, Alignment::Right, '$');  // "$$$$kratos"
pad_mut(&mut string, 14, Alignment::Center, '-');  // "--$$$$kratos--"

let s = "dragon";
let padded = pad(s, 11, Alignment::Left, 'πŸ‰');
assert_eq!("dragonπŸ‰πŸ‰πŸ‰πŸ‰πŸ‰", padded);

let vec: Vec<u8> = Vec::from(&[0u8, 2, 5]);
let mut buffer: Vec<u8> = Vec::with_capacity(5);
pad_to_buffer(vec, 5, Alignment::Right, 128u8, &mut buffer);
assert_eq!(Vec::from(&[128u8, 128, 0, 2, 5]), buffer);

StructsΒ§

Pads
Represents padding as a pair of (left, right) counts.

EnumsΒ§

Alignment
Specifies the alignment strategy to use when padding or truncating a buffer.

TraitsΒ§

MutableSource
A trait representing a mutable, width-aware data buffer that can be padded (and truncated).
Source
A trait representing a width-aware, read-only data buffer that can be padded (and truncated).

FunctionsΒ§

pad
Pads the given source buffer to the specified width using the provided symbol and alignment mode.
pad_mut
Pad the given mutable source buffer in-place to the specified width using the provided symbol and alignment mode.
pad_to_buffer
Pad the given source to match the specified width according to the specified alignment mode by writing into the provided buffer.