I would like to make an adapter that removes generic parameters (to produce a trait object) as in the example below.
use std::ops::Deref;
fn make_dyn_box<I, S>(iter_in: I)
where
I: Iterator<Item = S>,
S: Deref<Target = u8>,
{
let mut iter_out = iter_in.map(
|s| -> Box<dyn Deref<Target = u8>> {Box::new(s)}
);
take_dyn_box(&mut iter_out)
}
fn take_dyn_box<'a: 'b, 'b>(
iter: &'a mut (dyn 'a + Iterator<Item = Box<dyn 'b + Deref<Target = u8>>>),
) { }
Is there a way to accomplish this without a heap allocation, using only safe code, and no external dependencies?
The below is an idea of what I want, but the borrow checker doesn't allow this.
use std::ops::Deref;
fn make_dyn<I, S>(iter_in: I)
where
I: Iterator<Item = S>,
S: Deref<Target = u8>,
{
let mut item = None;
let item = &mut item;
let mut iter_out = iter_in.map(|s| -> &dyn Deref<Target = u8> {
item.replace(s);
Option::as_ref(item).unwrap()
});
take_dyn(&mut iter_out)
}
fn take_dyn<'a: 'b, 'b>(
iter: &'a mut (dyn 'a + Iterator<Item = &'b (dyn 'b + Deref<Target = u8>)>),
) { }