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

arrayvec vs tinyvec #1

Closed
chinoto opened this issue Jul 4, 2021 · 4 comments
Closed

arrayvec vs tinyvec #1

chinoto opened this issue Jul 4, 2021 · 4 comments

Comments

@chinoto
Copy link

chinoto commented Jul 4, 2021

I found tinyvec because of your guide, but I'd like to note that the difference isn't just that its elements must implement Default, they must also implement Copy (mutually exclusive with Drop). I was helping someone in Discord who needed a type that could vary in length and be Copied.

@paulkernfeld
Copy link
Owner

I found tinyvec because of your guide, but I'd like to note that the difference isn't just that its elements must implement Default, they must also implement Copy (mutually exclusive with Drop).

The program below makes me think that elements in tinyvec::ArrayVec don't need to implement Copy because String does not implement Copy. Does this understanding sound correct?

I was helping someone in Discord who needed a type that could vary in length and be Copied.

If by varying in length you mean that the type is not Sized, you probably wouldn't be able to store that type in an array-like data structure because they will all require that elements have a known size. To store objects with a dynamic size, you'd need some kind of way to keep track of the start and end of each object, i.e. an allocator.

#!/usr/bin/env run-cargo-script
//! https://crates.io/crates/cargo-script
//!
//! ```cargo
//! [dependencies]
//! tinyvec = "1.2.0"
//! ```
fn main() {
    tinyvec::ArrayVec::<[String; 4]>::new();
}

@chinoto
Copy link
Author

chinoto commented Jul 9, 2021

I goofed, apparently #[derive(Copy)] when used with generics expands to impl<T> Copy for ArrayVec<T> where T: Copy {}. What I should have said is that "tinyvec::ArrayVec can be a Copy type if its elements are, unlike arrayvec::ArrayVec", which I think can be a very useful feature.
As for varying length, I was referring to number of values being meaningfully stored*, not its capacity.
*I spent far too long trying to phrase that decently...

@paulkernfeld
Copy link
Owner

Ah, thanks for the clarification. Looks like there is a feature request to make a copyable arrayvec::ArrayVec so it's actually plausible that arrayvec will get this. Overall there are a lot of small differences between tinyvec and arrayvec, but I think it would be too detailed to describe all of them.

@chinoto
Copy link
Author

chinoto commented Jul 11, 2021

Unfortunately that feature request has been hanging around since 2016. I figured being Copy-able was as noteworthy as requiring Default, but I'll quit harping on it.

@chinoto chinoto closed this as completed Jul 11, 2021
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

No branches or pull requests

2 participants