indexmap icon indicating copy to clipboard operation
indexmap copied to clipboard

`impl Clone for IntoKeys` and `IntoValues`

Open clarfonthey opened this issue 1 year ago • 4 comments

The hashbrown into-iterators currently don't support Clone (see: rust-lang/hashbrown#545) but this shouldn't stop this crate from implementing it for its into-iterators, which ditch the hash table and just use vec::IntoIter internally.

clarfonthey avatar Sep 11 '24 23:09 clarfonthey

Sure, we could do that!

cuviper avatar Sep 12 '24 00:09 cuviper

One thing that's kind of weird is that IntoKeys will still need V: Clone, and IntoValues will need K: Clone, so we can have a complete clone of the underlying vec::IntoIter. We could possibly use Default for the wasted part instead, but that seems even weirder as a constraint. What do you think?

cuviper avatar Sep 13 '24 23:09 cuviper

Hmm, that's actually an interesting point. It'd require unsafe code, but perhaps we could transmute to make the unused parts MaybeUninit, since we're just going to ignore their values anyway, and then set them to uninit?

clarfonthey avatar Sep 14 '24 01:09 clarfonthey

#348 is adding the easy ones for IntoIter, but we can leave this open for IntoKeys and IntoValues.

cuviper avatar Sep 16 '24 23:09 cuviper

#426 uses MaybeUninit without unsafe code by eagerly dropping the unused part, also taking advantage of the specialization that lets vec.into_iter().map(..).collect() work in-place.

cuviper avatar Nov 29 '25 21:11 cuviper