From 27dd523370480860338b628f9c8752da727c6274 Mon Sep 17 00:00:00 2001 From: Chris Mikkelson Date: Tue, 8 Apr 2025 15:48:07 -0500 Subject: [PATCH] Implement dyn-compatible source variant. With appropriate boxing, allows creation of dynamically-dispatched sources for heterogeneous collections of sources. --- src/merger.rs | 2 +- src/source.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/merger.rs b/src/merger.rs index 5d9e371..5abf0a9 100644 --- a/src/merger.rs +++ b/src/merger.rs @@ -167,7 +167,7 @@ mod test { let iters: Vec<_> = range .clone() .map(test_source) - //.map(|s| s.into_boxed()) + .map(|s| s.into_boxed()) .collect(); let s = Merger::from(iters); let mut v = Vec::::new(); diff --git a/src/source.rs b/src/source.rs index adefa73..04d9dd3 100644 --- a/src/source.rs +++ b/src/source.rs @@ -50,15 +50,39 @@ pub trait Source { { FilterSource::new(self, filter_func) } + + fn into_boxed(self) -> Box> + where + Self: Sized + 'static, + { + // Inner Box satisfied DynSource, outer Box required for an owned DynSource trait object. + Box::new(Box::new(self)) + } +} + +// A dyn-compatible variant of Source generating boxed SeekableIters. Necessary to create +// heterogeneous collections of sources. +pub trait DynSource { + type Item; + + fn iter(&self) -> Box + '_>; } -impl Source for Box { +impl DynSource for Box { type Item = S::Item; - fn iter(&self) -> impl SeekableIter { + + fn iter(&self) -> Box + '_> { Box::new(self.as_ref().iter()) } } +impl Source for D { + type Item = D::Item; + fn iter(&self) -> impl SeekableIter { + DynSource::iter(self) + } +} + pub struct VecIter<'a> { index: usize, vec: &'a Vec, -- 2.50.1