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

DirectiveList::into_iter unexpectedly yields references #821

Closed
BrynCooke opened this issue Feb 7, 2024 · 2 comments · Fixed by #826
Closed

DirectiveList::into_iter unexpectedly yields references #821

BrynCooke opened this issue Feb 7, 2024 · 2 comments · Fixed by #826

Comments

@BrynCooke
Copy link
Contributor

DirectiveList::into_iter unexpectedly yields references.
I'd expect a function name into_iter to iterate over owned values.

  let list = DirectiveList::new();
  list.into_iter().map(|d: &Node<Directive>| d  ) // &Node!

Standard Vec:

  let list = vec![1, 2];
  list.into_iter().map(|d: i32| d  ); // Owned
@BrynCooke BrynCooke changed the title DirectiveList::into_iter unexpectedly yields references. DirectiveList::into_iter unexpectedly yields references Feb 7, 2024
@SimonSapin
Copy link
Contributor

DirectiveList itself does not have into_iter, so in this example code the compiler implicitly adds a reference like (&list).into_iter() to call the trait impl for &'_ DirectiveList:

impl<'a> IntoIterator for &'a DirectiveList {
type Item = &'a Node<Directive>;
type IntoIter = std::slice::Iter<'a, Node<Directive>>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}

This impl exists to make code like for dir in &something.directives work. We can add an owned impl IntoIterator for DirectiveList to make this example behave as expected.

In what kind of situation do you manipulate an owned DirectiveList?

@BrynCooke
Copy link
Contributor Author

I hadn't realized that into_iter may yeild reference, owned or mut ref depending on context.

I think implementing IntoIterator would still be a nice thing, the exact usecase I don't remember as the code was a conversion of some apollo-smith logic that eventually got deleted anyway. It was just an unexpected surprise.

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

Successfully merging a pull request may close this issue.

2 participants