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

Add dedicated Iterator builder for arrays #7

Open
daymxn opened this issue Oct 19, 2024 · 1 comment
Open

Add dedicated Iterator builder for arrays #7

daymxn opened this issue Oct 19, 2024 · 1 comment

Comments

@daymxn
Copy link

daymxn commented Oct 19, 2024

Problem

fromItems fails on large arrays, and fromRawParts is too verbose for simple array usage.

Overview

Given an array, there are two ways (afaik) to make an Iterator:

Using fromItems:

Iterator.fromItems(...myArray);

Using fromRawParts:

let i = 0;
Iterator.fromRawParts(
  () => Option.wrap(myArray[i++]),
  fixedSizeHint(myArray.size())
);

fromRawParts is a bit too verbose for simple use cases, and fromItems is the ideal happy path.

Although, fromItems fails on large arrays. When trying to unpack large arrays, you'll get the following error:

too many results to unpack

Proposal

To retain the ideal happy path usage, it would be great if there was a dedicated method for arrays.

public static fromArray<T extends defined>(items: Array<T>): Iterator<T>;
@Dionysusnu
Copy link
Owner

Does Vec.fromPtr(myArray).iter() provide the behaviour you're after?

On one hand, I generally consider Array<T> to be a non-rust item, so I don't think a built-in conversion for it is necessary. On the other hand, Vec is probably less used than the other rust-classes items, so maybe catering for arrays is worth it.

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