codespan icon indicating copy to clipboard operation
codespan copied to clipboard

Added generic iterator methods

Open yankana opened this issue 4 years ago • 2 comments

It's unnecessary to require allocated vectors instead of just any iterator. With this change, you can use with_labels_iter and with_notes_iter in the example code (and in other code) with arrays instead of vectors to avoid extra heap allocations. The other methods were left untouched so this isn't a breaking change.

yankana avatar Nov 16 '21 22:11 yankana

Thanks for your contribution!

Since Vec<String> is compatible with impl IntoIterator<Item = String>, and likewise for the other function, I think we could even just replace the existing methods instead of creating new ones.

Johann150 avatar Nov 17 '21 08:11 Johann150

Since Vec<String> is compatible with impl IntoIterator<Item = String>, and likewise for the other function, I think we could even just replace the existing methods instead of creating new ones.

That would be a breaking change because changing a method from using a concrete type to a generic/opaque type would break code that relies on the compiler inferring the type to be a vector. Example:

// `collect::<Vec<String>>` is inferred here, but with `impl IntoIterator<Item = String>`, the compiler cannot infer
let diagnostic = Diagnostic::error().with_notes(string_iter.collect());

yankana avatar Nov 17 '21 11:11 yankana