-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for TrustedLen (trusted_len
)
#37572
Comments
Oh, finally this thing is there! I was avoiding |
I think it is better named |
Or perhaps |
As the referenced code is merged - is this still an issue ? thank you ! |
Yes, tracking issues keep note on the code while it is still an unstable feature; they are closed when the feature is stabilized or removed. |
By the way, what is holding stabilization right now? |
I feel that Any thoughts? |
Do we need a RFC for stabilization? Also, is TrustedLen meant to be the successor (replacement) of ExactSizeIterator? |
It serves a different purpose, so I don't think it is a successor. Good start towards stabilization would be to incorporate the naming suggestions from this tracking issue I think. |
Unless I misunderstand (which is likely), this trait indicates that the number of iterations can be calculated in advance, and that it will be a finite number, possibly greater than max In that case, I would suggest one of
I also considered By the way, what exactly does |
usize::max_value() |
The
As currently spec'd, they're complements of each other. Speaking roughly, ExactSizeIterator can be shrunk but not grown, whereas TrustedLen can be grown but not shrunk. (For specific examples, Skip propagates ExactSizeIterator but not TrustedLen, and Chain propagates TrustedLen but not ExactSizeIterator.) |
The libs team discussed this and the consensus was that, since this trait was introduced in order to specialize on it, it should stay unstable until trait specialization itself becomes closer to stabilization. |
|
|
That's not quite true; see the example in #37572 (comment). |
Ah. So perhaps eventually. @scottmcm The type doesn't actually mean anything inherently; it's only a refinement on the return value of |
IMHO we're missing As far as |
I think there are only two things blocking stability for this trait. First, there's questions about what the name should be. Second, we need to improve documentation to make the distinction between this and ExactSizeIterator crystal clear. There are also open questions here about methods that may be included, but they can be handled as separate issues to allow the trait to continue toward stabilization. Here's what is not in question:
I suggest the lang team (or lib team? I get the teams all mixed up) should consider the options presented here and revise the name. Then we can change the name in nightly, and see if there are any comments for a few months. This allows us to make small, steady steps forward. Edit: I spotted the comment #37572 (comment) that said the team wanted to block this until trait specialization is done. I'd still like to make small steps forward, but if they want to hold off then I understand. Sorry for the disruption. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Implementing I believe that we do not want to require this guarantee on TrustedLen impls, but I am raising it here because this would need to be settled prior to stabilization; let me know if anyone would like to make a case. |
I think that fn trusted_len(&self) -> Option<usize>; instead of relying on |
If
Implemented correctly for type |
Closely related to |
In some cases it would be useful to know when an iterator has a known length that is guaranteed to be Having a stricter let a: Iterator + TrustedLen + TrustedUsizeLen = ...;
let b: Iterator + TrustedLen + TrustedUsizeLen = ...;
let chained: Iterator + TrustedLen = a.chain(b);
let length_recovered: Iterator + TrustedLen + TrustedUsizeLen = chained.take(usize::MAX) I.e. This in turn could enable specializations on |
Even |
The |
The current description confuses me. Some annotations for my future self and hopefully others:
Why is the definition not "ExactSizeIterator without mercy"? To allow chains of TrustedLen iterators to be TrustedLen too, without panicking or failing on overflow. Also, to allow repeating with a finite count (e.g. When is a properly implemented iterator TrustedLen but not ExactSizeIterator? Apart from the adapters discussed above, when it can yield more than usize::MAX elements, like When is a properly implemented iterator ExactSizeIterator but not TrustedLen? Apart from the shortening adapters discussed above ( |
Considering the nature of
Well, those comments are just me interpreting I should have updated
We could conceivably mark some things as |
Granted, it is a reason why a possible ExactSizeIterator would not be TrustedLen, though that #68352 case is quite specific to the Cycle adapter, that isn't a true iterator adapter but an Iterator+Clone adapter. But it's far from the reason why so many ExactSizeIterator in the standard library are not TrustedLen - they aren't even adapters. |
Oh sure I was giving a reason why an adapter cannot be
|
I was beginning to think |
What do you think about adding a Adding |
It could be an associated function instead |
Former commentJust as a data point for why this is useful, I just had to create a copy of this trait for [a fun project of mine](https://users.rust-lang.org/t/iteratorilp-speeding-up-iterator-reductions-with-instruction-level-parallelism/90289) that wouldn't work without some unsafe assertion that `ExactSizeIterator::len()` is implemented correctly.I believe I have used due dilligence in trying every safe alternative to Turns out I was wrong, and my project requires only an accurate lower bound, so a strict subset of TrustedLen's guarantees that is honored by any correctly implemented iterator. The best way to handle this would probably be some kind of |
Implemented in #37306
Open Questions:
The text was updated successfully, but these errors were encountered: