-
Notifications
You must be signed in to change notification settings - Fork 27
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 FromIterator implementations for Collection and CollectionWithId #291
Conversation
6ff1039
to
f8b450d
Compare
src/collection.rs
Outdated
{ | ||
iter.into_iter() | ||
.fold(CollectionWithId::default(), |mut accumulator, object| { | ||
accumulator.push(object); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is one problem here. The function .push()
returns a Result<>
which means, this can fails. The current implementation here will just ignore the error totally (usually, there is compiler warning for an unused Result
but it doesn't show anymore because of #![allow(unused_must_use)]
above).
This means that the implementation will ignore duplicate (see last test below for an example). The trait FromIterator
doesn't really provide a way to fail. And I couldn't find a TryFromIterator
trait and a corresponding .try_collect()
method (which I guess would have solved my problem here).
f8b450d
to
81e84cc
Compare
782ffb6
to
5a6f261
Compare
f7b6604
to
de8105a
Compare
1c5b46a
to
49e6384
Compare
b322046
to
319fea7
Compare
e2c4135
to
5d38c95
Compare
Just some update about this Draft PR. The problem lies with 1. Ignore duplicatesCollecting a duplicate identifier will simply ignore it, without raising an error. + the code doesn't panic
- no warning about what is happening and you lose some data without noticing it 2. Update duplicatesWhen a duplicate identifier is being collected, then the new one will be inserted in place of the old one. + the code doesn't panic
+ might seems logical to insert them in order so the last one wins
- no warning about what is happening and you lose some data without noticing it 3. Panic on duplicateWe see this kind of behavior sometimes in the standard library (for example, Note: I couldn't find a + the code does show there is an error
+ no loss of data
- the code panic, it'd be better to return an error
- the corresponding `try_collect()` doesn't exists in the standard API, neither the [`TryFromIterator`] 4. Do not implement
|
5d38c95
to
e9968b6
Compare
e9968b6
to
313c92c
Compare
After some thoughts, no confortable solution is possible for As for the implementation of I'm closing this PR. Maybe we can come back to it later if we have the need. |
Proposition to add the
FromIterator
to the custom collections we have intransit_model
:Collection
andCollectionWithId
.This would allow us to do replace a code like the following
into something which avoid to declare mutable fields and make good use of the
Iterator
.