rust-embedded / rust-embedded/heapless

from_iter on heapless::String should be generic over any iterator that yields a type that implements AsRef<str>

Open
#630 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2k
Forks
253
Avg merge
1d 2h
Merged PRs (30d)
1

Description

You currently cannot use from_iter directly with a &[CompactString] for example. You have to do .into_iter().map(...) which is annoying. I ended up building my own helper function for this but it would be nicer if the heapless crate itself provided this generic impl.

This is my helper function:

pub fn build_hstring<const N: usize, I, S>(iter: I) -> heapless::String<N>
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let mut new = heapless::String::<N>::new();
    for c in iter {
        new.push_str(c.as_ref()).expect("build_hstring: capacity exceeded");
    }
    new
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from heapless::String::from_iter and compare it with the helper shown in the issue, including its IntoIterator and AsRef bounds. Confirm the desired behavior with an iterator over string-like values such as &[CompactString]; it is done when callers no longer need an intermediate map to build a heapless::String.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.