-
Notifications
You must be signed in to change notification settings - Fork 924
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
[red-knot] flatten unions #11783
[red-knot] flatten unions #11783
Conversation
|
I managed to get the diff --git a/crates/red_knot/src/semantic/types.rs b/crates/red_knot/src/semantic/types.rs
index 360b157c5..ae866e00c 100644
--- a/crates/red_knot/src/semantic/types.rs
+++ b/crates/red_knot/src/semantic/types.rs
@@ -1,4 +1,6 @@
#![allow(dead_code)]
+use std::iter::FusedIterator;
+
use crate::ast_ids::NodeKey;
use crate::db::{QueryResult, SemanticDb, SemanticJar};
use crate::files::FileId;
@@ -531,12 +533,41 @@ pub struct UnionTypeId {
}
impl UnionTypeId {
- pub fn elements(self, type_store: &TypeStore) -> Vec<Type> {
+ pub fn elements(self, type_store: &TypeStore) -> UnionElementsIterator {
let union = type_store.get_union(self);
- union.elements.iter().copied().collect()
+ UnionElementsIterator { union, index: 0 }
}
}
+pub struct UnionElementsIterator<'a> {
+ union: UnionTypeRef<'a>,
+ index: usize,
+}
+
+impl<'a> Iterator for UnionElementsIterator<'a> {
+ type Item = Type;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let item = self.union.elements.get_index(self.index).copied();
+ self.index += 1;
+ item
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ let size = self.union.elements.len() - self.index;
+ (size, Some(size))
+ }
+
+ fn last(self) -> Option<Self::Item> {
+ self.union.elements.last().copied()
+ }
+}
+
+impl<'a> FusedIterator for UnionElementsIterator<'a> {}
+impl<'a> ExactSizeIterator for UnionElementsIterator<'a> {} A bit more verbose, but maybe worth it if we'll be iterating over union elements a lot? WDYT? |
Yeah, I think that's probably worth it! I knew this type of borrowing iterator was an option, but at the time I was being stubborn about trying to get it working with We should do the same for I think this is something that can wait until post-Salsa, though; no need to introduce more PRs right now for Micha to port. |
Flatten union types. Fixes #11781