pub struct BufferSource { /* private fields */ }Expand description
Source implementation for reading bencode data from a memory buffer In-memory byte vector input source supporting both binary byte streaming and Unicode UTF-8 character streaming.
Implementations§
Source§impl BufferSource
impl BufferSource
Sourcepub fn new(data: &[u8]) -> BufferSource
pub fn new(data: &[u8]) -> BufferSource
Creates a new BufferSource from a byte slice.
Examples found in repository?
examples/bencode_iterative.rs (line 21)
14fn main() {
15 println!("=== Iterative Parser Demo ===\n");
16
17 // Example 1: Simple parsing comparison
18 println!("1. Simple structure:");
19 let simple = b"d3:agei25e4:name4:Johne";
20
21 let mut source = BufferSource::new(simple);
22 match parse_iterative(&mut source) {
23 Ok(node) => {
24 println!(" Parsed successfully with iterative parser");
25 if let Ok(output) = stringify_to_bytes(&node) {
26 println!(" Output: {}", String::from_utf8_lossy(&output));
27 }
28 }
29 Err(e) => println!(" Error: {}", e),
30 }
31
32 // Example 2: Deeply nested lists
33 println!("\n2. Deeply nested structure (100 levels):");
34 let mut deeply_nested = String::from("i42e");
35 for _ in 0..100 {
36 deeply_nested = format!("l{}e", deeply_nested);
37 }
38 println!(" Created nested list with {} bytes", deeply_nested.len());
39
40 // Try with iterative parser (should work)
41 match babbel_bencode::parse_bytes_iterative(deeply_nested.as_bytes()) {
42 Ok(_) => println!(" ✓ Iterative parser succeeded"),
43 Err(e) => println!(" ✗ Iterative parser failed: {}", e),
44 }
45
46 // Try with recursive parser (works but uses more stack)
47 match babbel_bencode::parse_bytes(deeply_nested.as_bytes()) {
48 Ok(_) => println!(" ✓ Recursive parser succeeded (but used more stack)"),
49 Err(e) => println!(" ✗ Recursive parser failed: {}", e),
50 }
51
52 // Example 3: Very deeply nested (1000 levels) - only iterative should handle well
53 println!("\n3. Very deeply nested structure (1000 levels):");
54 let mut very_deep = String::from("i1e");
55 for _ in 0..1000 {
56 very_deep = format!("l{}e", very_deep);
57 }
58 println!(" Created nested list with {} bytes", very_deep.len());
59
60 match babbel_bencode::parse_bytes_iterative(very_deep.as_bytes()) {
61 Ok(_) => println!(" ✓ Iterative parser succeeded (no stack issues)"),
62 Err(e) => println!(" ✗ Iterative parser failed: {}", e),
63 }
64
65 // Example 4: Complex nested dictionary
66 println!("\n4. Complex nested dictionary:");
67 let complex = b"d4:infod6:lengthi123e4:name9:test.file12:piece lengthi16384eee";
68
69 let mut source = BufferSource::new(complex);
70 match parse_iterative(&mut source) {
71 Ok(node) => {
72 println!(" Parsed complex structure successfully");
73 if let Ok(output) = stringify_to_bytes(&node) {
74 println!(" Size: {} bytes", output.len());
75 }
76 }
77 Err(e) => println!(" Error: {}", e),
78 }
79
80 // Example 5: Deeply nested mixed structure
81 println!("\n5. Mixed nested structure:");
82 let mixed = b"d4:dictd1:ai1e1:bi2ee4:listlli1ei2eeli3ei4eeee";
83
84 let mut source = BufferSource::new(mixed);
85 match parse_iterative(&mut source) {
86 Ok(node) => {
87 println!(" Parsed mixed nested structure");
88 if let Ok(output) = stringify_to_bytes(&node) {
89 println!(" Output: {}", String::from_utf8_lossy(&output));
90 }
91 }
92 Err(e) => println!(" Error: {}", e),
93 }
94
95 println!("\n=== Advantages of Iterative Parser ===");
96 println!("✓ No recursion - uses explicit stack on heap");
97 println!("✓ Safe for deeply nested structures (>1000 levels)");
98 println!("✓ Predictable memory usage");
99 println!("✓ Ideal for embedded systems with limited stack space");
100 println!("✓ Same functionality as recursive parser");
101}Sourcepub fn from_vec(buffer: Vec<u8>) -> BufferSource
pub fn from_vec(buffer: Vec<u8>) -> BufferSource
Creates a new BufferSource by taking ownership of a byte vector (zero-copy).
Sourcepub fn read_line(&mut self) -> Option<String>
pub fn read_line(&mut self) -> Option<String>
Reads the next line into a String without trailing newline.
Sourcepub fn read_line_into(&mut self, buf: &mut String) -> bool
pub fn read_line_into(&mut self, buf: &mut String) -> bool
Reads the next line into an existing buffer. Returns false at EOF.
Trait Implementations§
Source§impl Clone for BufferSource
impl Clone for BufferSource
Source§fn clone(&self) -> BufferSource
fn clone(&self) -> BufferSource
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for BufferSource
impl Debug for BufferSource
Source§impl Default for BufferSource
impl Default for BufferSource
Source§fn default() -> BufferSource
fn default() -> BufferSource
Returns the “default value” for a type. Read more
Source§impl IByteReader for BufferSource
impl IByteReader for BufferSource
Source§impl ICharStream for BufferSource
impl ICharStream for BufferSource
Source§impl IIndentationAware for BufferSource
impl IIndentationAware for BufferSource
Source§fn get_current_indent_level(&self) -> usize
fn get_current_indent_level(&self) -> usize
Returns current indentation level (number of spaces or tab-stops).
Source§fn is_indent_whitespace(&self, c: char) -> bool
fn is_indent_whitespace(&self, c: char) -> bool
Checks if character is considered indentation whitespace.
Source§fn is_whitespace(&self, c: char) -> bool
fn is_whitespace(&self, c: char) -> bool
Checks if character is whitespace (space or tab).
Source§impl ILineReader for BufferSource
impl ILineReader for BufferSource
Source§fn read_line_into(&mut self, buf: &mut String) -> bool
fn read_line_into(&mut self, buf: &mut String) -> bool
Reads the next line into an existing buffer to avoid heap allocations.
Appends the line content without newline characters to
buf.
Returns true if a line (including an empty line) was read, false at EOF.Source§impl ILocationAware for BufferSource
impl ILocationAware for BufferSource
Source§impl IPeekable for BufferSource
impl IPeekable for BufferSource
Source§impl IPositionAware for BufferSource
impl IPositionAware for BufferSource
Source§impl IRewindable for BufferSource
impl IRewindable for BufferSource
Source§impl ISource for BufferSource
impl ISource for BufferSource
Source§impl IStatefulStream for BufferSource
impl IStatefulStream for BufferSource
Source§fn save_state(&mut self) -> SaveState
fn save_state(&mut self) -> SaveState
Snapshots the source read position and metadata.
Source§fn restore_state(&mut self, state: SaveState)
fn restore_state(&mut self, state: SaveState)
Restores a previously saved state.
Auto Trait Implementations§
impl Freeze for BufferSource
impl RefUnwindSafe for BufferSource
impl Send for BufferSource
impl Sync for BufferSource
impl Unpin for BufferSource
impl UnsafeUnpin for BufferSource
impl UnwindSafe for BufferSource
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IByteStream for T
impl<T> IByteStream for T
impl<T> ICharReader for Twhere
T: ICharStream + ?Sized,
Source§impl<T> ITracked for T
impl<T> ITracked for T
Source§fn offset(&self) -> usize
fn offset(&self) -> usize
Returns current absolute byte offset (delegates to
IPositionAware::position).