Skip to content

Add some conversion options for alloc vecs #541

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

Merged
merged 11 commits into from
Apr 8, 2025

Conversation

rilipco
Copy link
Contributor

@rilipco rilipco commented Mar 21, 2025

This PR introudces a new feature alloc which will make heapless depend on the alloc crate and adds two trait implementations on heapless::Vec and alloc::vec::Vec to allow conversion between them.

Conversion from heapless::Vec to alloc::vec::Vec is fallible (due to OOM errors) and implemented via TryFrom.
Conversion from alloc::vec::Vec to heapless::Vec is fallible (due to capacity limit) and implemented via TryFrom.

@newAM
Copy link
Member

newAM commented Mar 22, 2025

Conversion from heapless::Vec to alloc::vec::Vec is infallible and implemented via From.

On embedded platforms people may want to handle allocation errors locally, such as using try_with_capacity (sadly not stable at the moment).

I'm hesitant to implement an infallible implementation because in #381 I found that implementing infallible conversions prevents fallible ones from being implemented later.

@newAM
Copy link
Member

newAM commented Mar 25, 2025

Would you be willing to remove the conversion from heapless::Vec to alloc::vec::Vec for now?

I'm happy to merge from heapless::Vec to alloc::vec::Vec!

@newAM
Copy link
Member

newAM commented Mar 25, 2025

I realized a fallible heapless::Vec to alloc::vec::Vec is possible with the current APIs.

alloc::vec::Vec::new will not allocate until elements are pushed onto it. That plus try_reserve should allow for a fallible conversion implementation.

@newAM newAM requested a review from reitermarkus April 7, 2025 00:57
@reitermarkus reitermarkus added this pull request to the merge queue Apr 8, 2025
Merged via the queue into rust-embedded:main with commit 6ae82d5 Apr 8, 2025
22 checks passed
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 this pull request may close these issues.

3 participants