Skip to content

Commit

Permalink
move code in front of test module
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Nov 12, 2024
1 parent 9065491 commit 19c5933
Showing 1 changed file with 68 additions and 68 deletions.
136 changes: 68 additions & 68 deletions src/backend/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,74 @@ fn decode_var_usize_cold(buffer: &[u8]) -> Option<(usize, usize)> {
Some((result, i + 1))
}

impl<'a, S> IntoIterator for &'a BufferBackend<S>
where
S: Symbol,
{
type Item = (S, &'a str);
type IntoIter = Iter<'a, S>;

#[cfg_attr(feature = "inline-more", inline)]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

pub struct Iter<'a, S> {
backend: &'a BufferBackend<S>,
remaining: usize,
next: usize,
}

impl<'a, S> Iter<'a, S> {
#[cfg_attr(feature = "inline-more", inline)]
pub fn new(backend: &'a BufferBackend<S>) -> Self {
Self {
backend,
remaining: backend.len_strings,
next: 0,
}
}
}

impl<'a, S> Iterator for Iter<'a, S>
where
S: Symbol,
{
type Item = (S, &'a str);

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len();
(remaining, Some(remaining))
}

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.backend
.resolve_index_to_str(self.next)
.and_then(|(bytes, next)| {
// SAFETY: Within the iterator all indices given to `resolv_index_to_str`
// are properly pointing to the start of each interned string.
let string = unsafe { str::from_utf8_unchecked(bytes) };
let symbol = S::try_from_usize(self.next)?;
self.next = next;
self.remaining -= 1;
Some((symbol, string))
})
}
}

impl<S> ExactSizeIterator for Iter<'_, S>
where
S: Symbol,
{
#[inline]
fn len(&self) -> usize {
self.remaining
}
}

#[cfg(test)]
mod tests {
use super::{decode_var_usize, encode_var_usize};
Expand Down Expand Up @@ -434,71 +502,3 @@ mod tests {
// );
}
}

impl<'a, S> IntoIterator for &'a BufferBackend<S>
where
S: Symbol,
{
type Item = (S, &'a str);
type IntoIter = Iter<'a, S>;

#[cfg_attr(feature = "inline-more", inline)]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

pub struct Iter<'a, S> {
backend: &'a BufferBackend<S>,
remaining: usize,
next: usize,
}

impl<'a, S> Iter<'a, S> {
#[cfg_attr(feature = "inline-more", inline)]
pub fn new(backend: &'a BufferBackend<S>) -> Self {
Self {
backend,
remaining: backend.len_strings,
next: 0,
}
}
}

impl<'a, S> Iterator for Iter<'a, S>
where
S: Symbol,
{
type Item = (S, &'a str);

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len();
(remaining, Some(remaining))
}

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.backend
.resolve_index_to_str(self.next)
.and_then(|(bytes, next)| {
// SAFETY: Within the iterator all indices given to `resolv_index_to_str`
// are properly pointing to the start of each interned string.
let string = unsafe { str::from_utf8_unchecked(bytes) };
let symbol = S::try_from_usize(self.next)?;
self.next = next;
self.remaining -= 1;
Some((symbol, string))
})
}
}

impl<S> ExactSizeIterator for Iter<'_, S>
where
S: Symbol,
{
#[inline]
fn len(&self) -> usize {
self.remaining
}
}

0 comments on commit 19c5933

Please sign in to comment.