fixedbitset
fixedbitset copied to clipboard
Owned iterator: into_ones()
Hi,
I'm building a tiny abstraction over this lib for my use case (a set of usize
s with an API very similar to BTreeSet
) and I'm stuck on the implementation of my IntoIterator
because there is no owning equivalent of ones()
. I mean, One<'a>
iterates over a borrow of the FixedBitSet
and I cannot easily keep the FixedBitSet
and the One<'_>
around on the same struct:
struct IntoIter<'a> {
bit_set: FixedBitSet,
ones: Ones<'a>,
}
I'm currently using rental as a workaround, but that is quite complex.
Does it make sense to implement a method like pub fn into_ones(&self) -> IntoOnes
? I can work on a PR if you are interested.
Best regards,
I think it makes sense. My review time is a bit limited, but hopefully it can be solved
@sitegui do you mind clarifying how into_ones
allows writing struct IntoIter
?
Hi @jrraymond , of course :)
This is a reduced example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=aadc6f54122373d6724ead8fa8a8e828
The main problem is that Ones
borrows the FixedBitSet
, so I cannot create an owning struct. See that IntoIter
ends up borrowing as well:
struct IntoIter<'a>(Ones<'a>);
impl<'a> IntoIterator for MySet {
type Item = usize;
type IntoIter = IntoIter<'a>;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.ones())
}
}
The example is very reduced and seems very artificial, but in my use-case I have a complex type T
that I convert into usize
to store into the bitset and then I'd like to code like this to work seamlessly:
let my_set: MySet = todo!();
for el in my_set {
// an owned `el`
}
but that requires MySet: IntoIterator
. My type T
is Copy
, so I can work around with:
for &el in &my_set {
// an owned `el`
}
However, I've ended up using a Vec<bool>
instead of FixedBitSet
in my project to implement the API I wanted.
Hope it clarifies ;)