pub struct Buffer<'buf, R: Read> {
pub buf: &'buf mut [u8],
pub n_bytes: usize,
pub n_shifted_out: usize,
pub pos_shifted: LinePosition,
/* private fields */
}Expand description
A buffer for reading JSON data. Is a private struct, the “pub” is only for testing.
Fields§
§buf: &'buf mut [u8]The working buffer for reading JSON data.
n_bytes: usizeNumber of valid bytes in the buffer. Contract: n_bytes <= buf.len()
n_shifted_out: usizeNumber of bytes that have been shifted out of the buffer.
pos_shifted: LinePositionLine position correction due to shifting operations.
Implementations§
Source§impl<'buf, R: Read> Buffer<'buf, R>
impl<'buf, R: Read> Buffer<'buf, R>
Sourcepub fn new(reader: &'buf mut R, buf: &'buf mut [u8]) -> Self
pub fn new(reader: &'buf mut R, buf: &'buf mut [u8]) -> Self
Creates a new buffer with the given reader and buffer.
Sourcepub fn read_more(&mut self) -> RJiterResult<usize>
pub fn read_more(&mut self) -> RJiterResult<usize>
Read from the underlying reader into the buffer.
Returns the number of bytes read.
§Errors
From the underlying reader.
Sourcepub fn shift_buffer(&mut self, to_pos: usize, from_pos: usize)
pub fn shift_buffer(&mut self, to_pos: usize, from_pos: usize)
Shift the buffer to the left, and update the index and line-column position.
§Arguments
to_pos: The position to shift to. Usually is 0 or is 1 for strings.from_pos: The position to shift from. The case of outside the buffer is handled.
Sourcepub fn skip_spaces(&mut self, pos: usize) -> RJiterResult<()>
pub fn skip_spaces(&mut self, pos: usize) -> RJiterResult<()>
Sourcepub fn collect_while<F>(
&mut self,
predicate: F,
start_pos: usize,
allow_shift: bool,
) -> RJiterResult<(usize, usize)>
pub fn collect_while<F>( &mut self, predicate: F, start_pos: usize, allow_shift: bool, ) -> RJiterResult<(usize, usize)>
Collect bytes while a predicate is true, starting at the given position.
Returns a tuple of (start_position, end_position) where end_position is the offset
of the first rejected byte, or EOF.
If buffer is full with all accepted bytes, it’s an error.
The function can optionally shift the buffer once to discard bytes before start_pos.
§Arguments
predicate- A function that returns true if the byte should be acceptedstart_pos- The position in the buffer to start collecting fromallow_shift- If true, allows shifting the buffer once when it fills up (discards bytes beforestart_pos)
§Errors
Returns ErrorType::BufferFull if the buffer fills up with all accepted bytes.
Also returns errors from the underlying reader.
Sourcepub fn collect_count(
&mut self,
count: usize,
start_pos: usize,
allow_shift: bool,
) -> RJiterResult<(usize, usize)>
pub fn collect_count( &mut self, count: usize, start_pos: usize, allow_shift: bool, ) -> RJiterResult<(usize, usize)>
Collect exactly count bytes starting at the given position, or until EOF.
Returns a tuple of (start_position, end_position) where end_position is the offset
after the collected bytes (start_pos + actual_collected).
If buffer is too small to hold the requested bytes, it’s an error.
The function can optionally shift the buffer once to discard bytes before start_pos.
§Arguments
count- The number of bytes to collectstart_pos- The position in the buffer to start collecting fromallow_shift- If true, allows shifting the buffer once when it fills up (discards bytes beforestart_pos)
§Errors
Returns ErrorType::BufferFull if the buffer is too small to hold the requested bytes.
Also returns errors from the underlying reader.
Sourcepub fn skip_n(
&mut self,
count: usize,
start_pos: usize,
) -> RJiterResult<(usize, usize)>
pub fn skip_n( &mut self, count: usize, start_pos: usize, ) -> RJiterResult<(usize, usize)>
Skip exactly count bytes starting at the given position, or until EOF.
Returns the new position in the buffer after skipping.
This function works incrementally and can skip any number of bytes regardless of buffer size. It repeatedly shifts and reads as needed when the buffer is too small. When bytes fit in the buffer, it just returns the new position without shifting.
§Arguments
count- The number of bytes to skipstart_pos- The position in the buffer to start skipping from
§Errors
Returns errors from the underlying reader.
Trait Implementations§
Auto Trait Implementations§
impl<'buf, R> Freeze for Buffer<'buf, R>
impl<'buf, R> RefUnwindSafe for Buffer<'buf, R>where
R: RefUnwindSafe,
impl<'buf, R> Send for Buffer<'buf, R>where
R: Send,
impl<'buf, R> Sync for Buffer<'buf, R>where
R: Sync,
impl<'buf, R> Unpin for Buffer<'buf, R>
impl<'buf, R> !UnwindSafe for Buffer<'buf, R>
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
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.