Skip to content

Commit

Permalink
Makes IonDataSource::is_streaming() an associated const
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Jan 9, 2025
1 parent 1947931 commit 76b9256
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/lazy/streaming_raw_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,14 @@ impl<Encoding: Decoder, Input: IonInput> StreamingRawReader<Encoding, Input> {
self.read_next(context, /*is_peek=*/ true)
}

fn input_is_streaming(&mut self) -> bool {
self.input.get_mut().is_streaming()
}

fn read_next<'top>(
&'top mut self,
context: EncodingContextRef<'top>,
is_peek: bool,
) -> IonResult<LazyRawStreamItem<'top, Encoding>> {
// If the input is a stream, we assume there may be more data available.
// If it's a fixed slice, we know it's already complete.
let mut input_source_exhausted = !self.input_is_streaming();
let mut input_source_exhausted = !Input::DataSource::IS_STREAMING;
loop {
// If the input buffer is empty, try to pull more data from the source before proceeding.
// It's important that we do this _before_ reading from the buffer; any item returned
Expand Down Expand Up @@ -257,6 +253,9 @@ impl<Encoding: Decoder, Input: IonInput> StreamingRawReader<Encoding, Input> {
/// An input source--typically an implementation of either `AsRef<[u8]>` or `io::Read`--from which
/// Ion can be read, paying the cost of buffering and I/O copies only when necessary.
pub trait IonDataSource {
/// If `true`, the current contents of the buffer may not be the complete stream.
const IS_STREAMING: bool;

/// Returns a slice of all unread bytes that are currently available in the buffer.
fn buffer(&self) -> &[u8];

Expand All @@ -268,9 +267,6 @@ pub trait IonDataSource {
/// Marks `number_of_bytes` in the buffer as having been read. The caller is responsible for
/// confirming that the buffer contains at least `number_of_bytes` bytes.
fn consume(&mut self, number_of_bytes: usize);

/// If `true`, the current contents of the buffer may not be the complete stream.
fn is_streaming(&self) -> bool;
}

/// A fixed slice of Ion data that does not grow; it wraps an implementation of `AsRef<[u8]>` such
Expand Down Expand Up @@ -303,6 +299,8 @@ impl<SliceType: AsRef<[u8]>> IonSlice<SliceType> {
}

impl<SliceType: AsRef<[u8]>> IonDataSource for IonSlice<SliceType> {
const IS_STREAMING: bool = false;

#[inline]
fn buffer(&self) -> &[u8] {
// Return the input slice containing all of the as-of-yet unread bytes.
Expand All @@ -327,11 +325,6 @@ impl<SliceType: AsRef<[u8]>> IonDataSource for IonSlice<SliceType> {
self.buffer()
);
}

#[inline(always)]
fn is_streaming(&self) -> bool {
false
}
}

/// A buffered reader for types that don't implement AsRef<[u8]>
Expand Down Expand Up @@ -377,6 +370,8 @@ impl<R: Read> IonStream<R> {
}

impl<R: Read> IonDataSource for IonStream<R> {
const IS_STREAMING: bool = true;

fn buffer(&self) -> &[u8] {
&self.buffer[self.position..self.limit]
}
Expand Down Expand Up @@ -406,11 +401,6 @@ impl<R: Read> IonDataSource for IonStream<R> {
self.position += number_of_bytes;
debug_assert!(self.position <= self.limit);
}

#[inline(always)]
fn is_streaming(&self) -> bool {
true
}
}

/// Types that can be used as a source of Ion data.
Expand Down

0 comments on commit 76b9256

Please sign in to comment.