#[derive err_if_clone_is_not_O(1)]?

Is there a way to get warnings or errors if I try to #[derive(Clone)] on a struct/enum where it would take longer than O(1) ?

The concrete case is that I am building some complicated structs, and I am afraid that I have a #[derive(Clone)] on something where it has a field that is Vec<> instead of Rc<Vec<_>> -- and then, not realizing that the clone is O(1), over use it.

Thus, I am looking for something that can warn me on #[derive(Clone)] that are not obviously O(1).

1 Like

You could create a CheapClone trait, implement it for various standard types (like primitives, references, Rc, and Arc), and then create a #[derive(CheapClone)] macro for custom types that relies on all the fields implementing CheapClone.

6 Likes

Indeed, Facebook have implemented this approach: gazebo::dupe - Rust

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.