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

ACP: Implement FromIterator for Box<str> #196

Closed
calebsander opened this issue Mar 25, 2023 · 0 comments
Closed

ACP: Implement FromIterator for Box<str> #196

calebsander opened this issue Mar 25, 2023 · 0 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@calebsander
Copy link

Proposal

Problem statement

Boxed slices are space-saving alternatives to Vec and String when the ability to grow the allocation is not required. To make them easier replacements, it would be ideal for Box<[T]> and Box<str> to provide all the functionality of Vec<T> and String that doesn't require resizing.
One functionality gap is that String implements FromIterator (for 6 types of Iterator item), but Box<str> doesn't. There's already an analogous implementation for Box<[T]>: FromIterator is implemented for Box<[T]> by using Vec<T>'s FromIterator implementation and then calling into_boxed_slice() to convert to a Box<[T]>. The proposal is to do the same thing for Box<str>: provide the same FromIterator implementations that String has, which can be implemented by collecting into a String and then calling into_boxed_str().

Additionally, impl FromIterator<Box<str>> for String and impl Extend<Box<str>> for String can be generalized to allow boxed strings with allocators other than Global. The implementations already copy the bytes to the String's allocation, so they work for Box<str, A> with any A: Allocator.

Motivation, use-cases

The existing impl<I> FromIterator<I> for Box<[I]> makes it convenient to collect an iterator into a boxed slice, for example:

let squares: Box<_> = (1..10).map(|x| x * x).collect();

Box<str> is an even more useful alternative to String than Box<[T]> is for Vec<T>, as strings are often immutable. The existing FromIterator<char> implementation for String makes it easy, for example, to reverse the characters in a string:

fn reverse(s: &str) -> String {
    s.chars().rev().collect()
}

However, returning a Box<str> requires a much more verbose implementation:

fn reverse(s: &str) -> Box<str> {
    s.chars().rev().collect::<String>().into_boxed_str()
}

Providing a FromIterator implementation for Box<str> would allow for the same implementation as for String.

Solution sketches

An implementation is available here: rust-lang/rust#99969
The same FromIterator implementations as for String are added for Box<str>. The implementations are straightforward, using String's FromIterator implementation and then calling into_boxed_str() to remove the excess capacity, analogous to impl<I> FromIterator<I> for Box<[I]>.

One other possibility is to extend the FromIterator implementations to Box<str, A> for all A: Allocator. But this is not currently done for Box<[T], A> or Vec<T, A>, so I opted to do the same and only add implementations for Box<str>.

Links and related work

rust-lang PR: rust-lang/rust#99969

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

@calebsander calebsander added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Mar 25, 2023
@m-ou-se m-ou-se closed this as completed Mar 5, 2024
@m-ou-se m-ou-se added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Mar 5, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 15, 2024
…, r=dtolnay

alloc: implement FromIterator for Box<str>

`Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`.
Add analogous `FromIterator` implementations for `Box<str>`
matching the current implementations for `String`.
Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too.

ACP: rust-lang/libs-team#196
bors added a commit to rust-lang-ci/rust that referenced this issue May 18, 2024
…r=dtolnay

alloc: implement FromIterator for Box<str>

`Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`.
Add analogous `FromIterator` implementations for `Box<str>`
matching the current implementations for `String`.
Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too.

ACP: rust-lang/libs-team#196
bors added a commit to rust-lang-ci/rust that referenced this issue May 19, 2024
…r=dtolnay

alloc: implement FromIterator for Box<str>

`Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`.
Add analogous `FromIterator` implementations for `Box<str>`
matching the current implementations for `String`.
Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too.

ACP: rust-lang/libs-team#196
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue May 19, 2024
alloc: implement FromIterator for Box<str>

`Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`.
Add analogous `FromIterator` implementations for `Box<str>`
matching the current implementations for `String`.
Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too.

ACP: rust-lang/libs-team#196
RalfJung pushed a commit to RalfJung/miri that referenced this issue May 19, 2024
alloc: implement FromIterator for Box<str>

`Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`.
Add analogous `FromIterator` implementations for `Box<str>`
matching the current implementations for `String`.
Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too.

ACP: rust-lang/libs-team#196
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue May 24, 2024
alloc: implement FromIterator for Box<str>

`Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`.
Add analogous `FromIterator` implementations for `Box<str>`
matching the current implementations for `String`.
Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too.

ACP: rust-lang/libs-team#196
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants