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

accept any iterator for PySet::new and PyFrozenSet::new #2795

Merged
merged 2 commits into from
Dec 9, 2022

Conversation

davidhewitt
Copy link
Member

Closes #2789

Looks like it's about 20% faster in the case I benchmarked to use PySet_Add directly without the intermediate list.

src/types/frozenset.rs Show resolved Hide resolved
pub fn new<'p, T: ToPyObject>(py: Python<'p>, elements: &[T]) -> PyResult<&'p PyFrozenSet> {
let list = elements.to_object(py);
unsafe { py.from_owned_ptr_or_err(ffi::PyFrozenSet_New(list.as_ptr())) }
pub fn new<'p, T: ToPyObject>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record, this is breaking since &[T] is IntoIterator<Item = &T>.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I think in which case let's use &T here for now too. We can revisit that as a different breaking change in future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to be breaking code anyway, what about taking IntoPy instead? i.e. inserting pyclasses into the set by value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think I'd like to do this (and I've thought similar in the past for PyList::new). I will hold off from doing that here, as this is again non-breaking after I've corrected to use Item = &T for now.

I'd be keen to revisit this along with #2316 and also a design about how we maybe also handle fallible into-py conversions.

@davidhewitt
Copy link
Member Author

bors r=birkenfeld

@bors
Copy link
Contributor

bors bot commented Dec 9, 2022

Build succeeded:

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 this pull request may close these issues.

Change PySet::new to accept a ExactSizeIterator
3 participants