-
Notifications
You must be signed in to change notification settings - Fork 13.5k
add default FromIterator for types with Default and Extend trait #143996
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
base: master
Are you sure you want to change the base?
add default FromIterator for types with Default and Extend trait #143996
Conversation
Failed to set assignee to
|
e85cd44
to
d8d2e82
Compare
Sorry, bad source branch. The labels may be incorrect, shall I start a new pr? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
854316a
to
c0faef8
Compare
This comment has been minimized.
This comment has been minimized.
…d trait Signed-off-by: HernandoR <lzhen.dev@outlook.com>
c0faef8
to
1653767
Compare
This comment has been minimized.
This comment has been minimized.
@@ -152,6 +152,17 @@ pub trait FromIterator<A>: Sized { | |||
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self; | |||
} | |||
|
|||
// 为同时实现 Default 和 Extend 的类型实现 FromIterator | |||
#[unstable(feature = "from_iter_default", issue = "58659")] |
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.
Unfortunately unstable impl's are not supported (yet?), so you should remove this line and its accompanying feature.
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.
Should be #[stable(feature = "from_iter_default", since = "CURRENT_RUSTC_VERSION")]
, and I was probably wrong about removing the feature.
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Marijn Schouten <hkBst@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
Signed-off-by: HernandoR <lzhen.dev@outlook.com>
This comment has been minimized.
This comment has been minimized.
Signed-off-by: HernandoR <lzhen.dev@outlook.com>
This comment has been minimized.
This comment has been minimized.
Failed to set assignee to
|
Suppose there is a tuple type The constraints of the generic implementation: In this case, when from_iter is called, the compiler would need to choose between: The generic implementation: with In theory, the tuple implementation is more specific. However, the compiler cannot confirm the safety of this specialization—because both implementations rely on the core logic of |
Signed-off-by: HernandoR <lzhen.dev@outlook.com>
i could not tell if |
The job Click to see the possible cause of the failure (guessed by this bot)
|
@HernandoR I'm not sure what's going wrong here. Combining these impl seems to work fine on the playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=1f1f887ddb3205ed2a3d488d7f05955f. I'm reading up on specialization, but it's probably a good idea to ask for help on Zulip. |
As a blanket implementation, this would be a major breaking change. I can have this today, and this change conflicts with it. #[derive(Default)]
struct S(usize);
impl<I> Extend<I> for S {
fn extend<T: IntoIterator<Item = I>>(&mut self, iter: T) {
self.0 += iter.into_iter().count();
}
}
impl<I> FromIterator<I> for S {
fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
Self(
iter.into_iter().count() + 100
)
}
} |
@QuineDot I think that's exactly what feature |
relating to #58659
r? @hkBst