Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TinyVec/ArrayVec's Clone uses the naive (default) version of Clone::clone_from. #143

Closed
thomcc opened this issue Jul 6, 2021 · 0 comments · Fixed by #144
Closed

TinyVec/ArrayVec's Clone uses the naive (default) version of Clone::clone_from. #143

thomcc opened this issue Jul 6, 2021 · 0 comments · Fixed by #144

Comments

@thomcc
Copy link
Contributor

thomcc commented Jul 6, 2021

You derive Clone on TinyVec/ArrayVec. This means you get the default impl of clone_from, which doesn't allow items stored inside these to reuse their memory. (Ideally, #[derive(Clone)] would do this, but they don't, as it would increase compile time for all rust programs for a rarely-used method. Thankfully, at least all the stdlib container provide impls of this...)

I'm using this for a TinyVec<[FloatImagePlane; 4]> (a float image plane being a Vec<f32> and some metadata). These cane be fairly large, so I'd have an option to have the memory reused.

I think this implementation that should work. I didn't write tests, and don't have time to PR it. https://gist.github.com/thomcc/56f9804d23459da4650112a573acf9dd.

Lokathor added a commit that referenced this issue Jul 6, 2021
Lokathor added a commit that referenced this issue Jul 22, 2021
* improve clone impl as per #143

* per #144 (comment)

* rustfmt.

* formatting

* whoops haha that's a little tricky to get the types to line up

we don't have deref for the A so we have to keep using as_slice_mut and such
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants