pub struct OptimizedFormatBuffer { /* private fields */ }Expand description
High-performance buffer for error formatting with safe optimizations
Implementations§
Source§impl OptimizedFormatBuffer
impl OptimizedFormatBuffer
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new optimized format buffer with default capacity.
Initializes a new OptimizedFormatBuffer with a default capacity of 4KB,
which is optimized for typical error formatting scenarios. The buffer
uses intelligent growth strategies to minimize memory allocations.
§Returns
A new OptimizedFormatBuffer instance with default capacity.
§Examples
let buffer = OptimizedFormatBuffer::new();
assert_eq!(buffer.as_str(), "");Source§impl OptimizedFormatBuffer
impl OptimizedFormatBuffer
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new optimized format buffer with specified capacity.
Initializes a new OptimizedFormatBuffer with a custom initial capacity.
This is useful when you have an estimate of the final formatted size
and want to avoid reallocations during formatting operations.
§Arguments
capacity- The initial capacity for the internal string buffer.
§Returns
A new OptimizedFormatBuffer instance with the specified capacity.
§Examples
let buffer = OptimizedFormatBuffer::with_capacity(8192);
assert_eq!(buffer.as_str(), "");Sourcepub fn append_optimized(&mut self, s: &str)
pub fn append_optimized(&mut self, s: &str)
High-performance string appending with optimized growth strategy
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns a string slice of the buffer’s contents.
This method provides read-only access to the formatted content within the buffer. The returned string slice is guaranteed to be valid UTF-8 as all input is validated.
§Returns
A string slice containing the current buffer contents.
§Examples
let mut buffer = OptimizedFormatBuffer::new();
buffer.append_optimized("Hello, World!");
assert_eq!(buffer.as_str(), "Hello, World!");§Performance
This operation has O(1) time complexity and does not involve any allocations.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the buffer contents while preserving the allocated capacity.
This method efficiently removes all content from the buffer without deallocating the underlying storage. This allows for optimal memory reuse when the buffer will be used again with similar content sizes.
§Examples
let mut buffer = OptimizedFormatBuffer::new();
buffer.append_optimized("Hello, World!");
assert_eq!(buffer.as_str().len(), 13);
buffer.clear();
assert_eq!(buffer.as_str().len(), 0);
assert!(buffer.as_str().is_empty());§Performance
This operation has O(1) time complexity and preserves allocated capacity for optimal memory reuse patterns.
Sourcepub fn append_multiple(&mut self, fragments: &[&str])
pub fn append_multiple(&mut self, fragments: &[&str])
Optimized formatting for multiple string fragments