orium / rpds

Rust persistent data structures
Mozilla Public License 2.0
1.28k stars 58 forks source link

Copy trait for HashTrieMap ? #93

Closed gl-yziquel closed 6 months ago

gl-yziquel commented 6 months ago

Hi.

Really really great library. Thank you for having pointed it out to me on irc (before I got banned for asking a specific question).

Is there any specific reason why the Copy trait is not implemented for HashTrieMap ?

I am in a situation where, to implement the Copy trait on one of my datastructures embedding a HashTrieMap, I have to embed it as a reference, which creates an additional and, I believe, needless lifetime in the type system of my code.

Having had a look at the code of rpds, I do not see a specific reason why the Copy trait could not be implemented, but, as I am quite new to rust, there may be such a reason.

I'd welcome a Copy trait.

orium commented 6 months ago

Hi. The Copy trait should only be implemented if copying the data structure is very very cheap: usually just copying a few bytes of memory (if you have to call functions inside clone() it is probably too expensive to be Copy). That's not the case for any of the rpds data structures as there is more complex logic when cloning.

I suggest you live with the lifetime if possible. Or otherwise explore other abstractions to share state like Rc/RefCell.

gl-yziquel commented 6 months ago

Thank you.

I'll deal with the lifetime, then.

gl-yziquel commented 6 months ago

For the wider context and rationale about my inquiry concerning the copy trait for hastriemap:

https://github.com/rust-bakery/nom/issues/1765