Skip to content

Commit

Permalink
refactor: select helper
Browse files Browse the repository at this point in the history
- remove unneeded clippy allows
- remove horrifying comment
- remove unnecessasry intermediate var
- do not pub internal types
- added docstring
  • Loading branch information
jqnatividad committed Feb 22, 2025
1 parent 6830fa9 commit bfbe64c
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,21 +392,31 @@ impl fmt::Debug for OneSelector {
#[derive(Clone, Debug)]
pub struct Selection(Vec<usize>);

pub type _GetField = for<'c> fn(&mut &'c csv::ByteRecord, &usize) -> Option<&'c [u8]>;
type _GetField = for<'c> fn(&mut &'c csv::ByteRecord, &usize) -> Option<&'c [u8]>;

impl Selection {
#[inline]
/// Returns an iterator that yields selected fields from a CSV record.
///
/// This method takes a CSV record and returns an iterator that yields only the fields
/// specified by this Selection. The fields are returned in the order they were selected.
///
/// # Arguments
///
/// * `row` - The CSV record to select fields from
///
/// # Returns
///
/// An iterator that yields references to the selected fields as byte slices
pub fn select<'a, 'b>(
&'a self,
row: &'b csv::ByteRecord,
) -> iter::Scan<slice::Iter<'a, usize>, &'b csv::ByteRecord, _GetField> {
// This is horrifying.
#[allow(clippy::unnecessary_wraps)]
#[allow(clippy::trivially_copy_pass_by_ref)]
#[allow(clippy::needless_pass_by_ref_mut)]
fn get_field<'c>(row: &mut &'c csv::ByteRecord, idx: &usize) -> Option<&'c [u8]> {
Some(&row[*idx])
}

let get_field: _GetField = get_field;
self.iter().scan(row, get_field)
}
Expand Down Expand Up @@ -443,25 +453,39 @@ impl ops::Deref for Selection {
#[derive(Clone, Debug)]
pub struct NormalSelection(Vec<bool>);

pub type _NormalScan<'a, T, I> = iter::Scan<iter::Enumerate<I>, &'a [bool], _NormalGetField<T>>;
type _NormalScan<'a, T, I> = iter::Scan<iter::Enumerate<I>, &'a [bool], _NormalGetField<T>>;

pub type _NormalFilterMap<'a, T, I> =
type _NormalFilterMap<'a, T, I> =
iter::FilterMap<_NormalScan<'a, T, I>, fn(Option<T>) -> Option<T>>;

pub type _NormalGetField<T> = fn(&mut &[bool], (usize, T)) -> Option<Option<T>>;
type _NormalGetField<T> = fn(&mut &[bool], (usize, T)) -> Option<Option<T>>;

impl NormalSelection {
#[allow(clippy::needless_lifetimes)]
pub fn select<'a, T, I>(&'a self, row: I) -> _NormalFilterMap<'a, T, I>
/// Selects elements from an iterator based on the normal selection pattern.
///
/// This method takes an iterator and returns a filtered version that only includes
/// elements at positions marked as true in the selection pattern.
///
/// # Arguments
///
/// * `row` - An iterator containing elements to filter
///
/// # Returns
///
/// Returns a filtered iterator that only yields elements at selected positions
///
/// # Type Parameters
///
/// * `T` - The type of elements in the iterator
/// * `I` - The type of the input iterator
pub fn select<T, I>(&self, row: I) -> _NormalFilterMap<'_, T, I>
where
I: Iterator<Item = T>,
{
const fn filmap<T>(v: Option<T>) -> Option<T> {
v
}
#[allow(clippy::option_option)]
#[allow(clippy::unnecessary_wraps)]
#[allow(clippy::needless_pass_by_ref_mut)]
fn get_field<T>(set: &mut &[bool], t: (usize, T)) -> Option<Option<T>> {
let (i, v) = t;
if i < set.len() && set[i] {
Expand Down

0 comments on commit bfbe64c

Please sign in to comment.