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

Wishlist: TryFrom<SmallVec<A>> for [T; N] #357

Open
ZhennanWu opened this issue Jun 10, 2024 · 0 comments
Open

Wishlist: TryFrom<SmallVec<A>> for [T; N] #357

ZhennanWu opened this issue Jun 10, 2024 · 0 comments

Comments

@ZhennanWu
Copy link

ZhennanWu commented Jun 10, 2024

This is an equivalent API of TryFrom<Vec<T>> for [T; N] which was stabilized in Rust 1.48.

Reason:

When using the smallvec crate in a message-passing context, I often find I using it as a container for payloads with pre-determined length.

fn send(&self) -> SmallVec<[i32; 2]> {
    smallvec![1, 2, 3]
}

fn recv(&mut self, msg: SmallVec<[Box<dyn Any>; 2]>) {
    let [a, b, c] = some_unwrap_magic!(msg);
    // ...
}

Now the only way to do this seems to be calling next().unwrap() on msg.into_iter(). But Vec provides a very nice way of doing this, and presumably more friendly to compiler optimizations:

let [a, b, c]: [Box<dyn Any>; 3] = msg.try_into().unwrap();

I wish this feature to make its way into ver 2.0

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

1 participant