-
Notifications
You must be signed in to change notification settings - Fork 760
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
Add support for arbitrary arrays #1128
Conversation
Thank you, but is there any strong reason we should have this |
Agreed in that I was hoping the goal was to remove the nightly feature. Maybe instead of providing this in the pyo3 core, we can extend the new |
TBH I'd rather this to be kept behind a feature flag for longer, than get merged without the second that const-generics are stabilized (#1076 calling). |
Agreed whatever we do we should give time for MSRV support to propagate. |
I did include it within the Constant generics is now behind its own opt-in flag and all |
What I meant as Anyway, let's consider merging after the upstream PR is merged. |
Ah, and I realized that now we don't have any tests for |
As the I'd prefer to move away from the pyo3 core crate having any CI which requires nightly, as it consumes contributor time (e.g. PRs spuriously failing because something changed in nightly). Hence my question above whether we can make this easy to integrate via a separate crate for now. |
I have been thinking further on this topic, and I wanted to propose that we do support this PR, behind a new feature. I think it's beneficial to the Rust ecosystem that we support this because it enables more users to experiment with const generics. This can help the feature to eventually stabilize and also benefits immediately our users on nightly Rust. (Even serde has an I'd prefer to keep away from having a single So I propose to move forward on this PR with a new feature named |
@davidhewitt Maybe wait for the 1.50 release? |
The time has come. |
We support all the way back to Rust 1.41, so this will need to go behind a |
An alternative to a user-facing feature flag is we could detect the compiler version in the build script, and then conditionally enable const generics based on that. |
I miss the good ol' days when nightly was the only thing available for this project
Sure, this is a good approach. I will update everything accordingly. Thanks! |
👍 you might be interested in https://github.com/dtolnay/anyhow/blob/master/build.rs |
2afaa93
to
5370406
Compare
Clippy failure is not related to this PR |
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.
Thanks very much for the continued refinements! This is looking great.
Afraid I have another round of comments; I think we're really close now, very excited to see this land! 😅
As well as the comments below, I have two additional points:
-
When I suggested moving code into
conversions/array.rs
, I meant all of the existingarray_impls!
macros and tests too. If it's all together in one place it's much easier for reviewers to audit what's tested and also maintain in the future once we don't need to support older rustc < 1.51. -
This still needs a CHANGELOG entry. I propose the following:
### Added - Add conversion for `[T; N]` for all `N` on Rust 1.51 and up. [#1128](https://github.com/PyO3/pyo3/pull/1128)
Happy for you to choose alternative wording if you think there's a better phrase out there.
926f70a
to
8f47a76
Compare
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.
Thank you for your continuous works, happy to see this works actually! 🎉
src/conversions/array.rs
Outdated
let initialized_part = core::ptr::slice_from_raw_parts_mut(self.dst, *self.initialized); | ||
unsafe { | ||
core::ptr::drop_in_place(initialized_part); | ||
} |
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.
Why do we need to drop
the initialized slice?
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.
Guard drops the initialized part when the closure panics to prevent leaks
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.
Ah, OK, I'm sorry but I was only thinking of the situation where T
is a number or so (then dropping the slice does not mean so much...).
How about using [MaybeUninit<T>; N]
instead?
Then, for me, it is clearer that each element should be dropped.
Some examples (https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#initializing-an-array-element-by-element and https://doc.rust-lang.org/src/core/mem/maybe_uninit.rs.html#1042) use this style.
Also, please consider moving ArrayGuard
definition in the try_create_array
, since it is not used in other places.
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.
This data structure was used by previous releases of the nightly compiler and is battle tested by many people around the globe. Please consider avoid changing its original formulation.
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.
@kngwyu note that eventually an API for this might be added to std
: rust-lang/rust#75644
Personally I'm happy as long as we're sure the implementation we use is sound.
eb0720e
to
3ba4a05
Compare
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.
Whew, this PR is turning out to be quite fiddly! Thank you for all the effort you've put into this.
I'm apologetic to say I've got another round of comments. In particular I have a concern that we might have some UB inside the try_create_array
function.
@c410-f3r thanks very much for all the work you've done getting this PR through to this point. I'm doing some tidying up of pending items ahead of the 0.14 release so I'm shortly going to rebase this PR and push a commit to finish it off so that we can include it in the upcoming release. Hope that's ok. |
1de9d37
to
7814c2a
Compare
Hey @davidhewitt Thanks for finishing this PR! |
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.
Thanks! I have some minor requests for tests
Extends my previous work on #778 and an additional PR will soon be provided to remove the
Default
bound restriction.If rust-lang/rust#75644 is going to be accepted, then the auxiliary functions won't be necessary in the near future.