-
Notifications
You must be signed in to change notification settings - Fork 763
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
Implement FromPyObject
and IntoPy<PyObject>
traits for arrays (up to 32)
#778
Conversation
src/types/list.rs
Outdated
create_tests!(array); | ||
create_tests!(vec vec!); |
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.
Not sure if this approach is desired. If so, I will also do the same in sequence.rs
.
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.
I think this is a good idea, thanks for adding comprehensive tests!
src/types/list.rs
Outdated
macro_rules! array_impls { | ||
($($N:expr),+) => { | ||
$( | ||
impl<T> ToPyObject for [T; $N] |
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.
As these imples are not faster than one for slices(impl ~ for &[T]
), I don't think we need this.
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.
I agree for ToPyObject
but not for IntoPy<PyObject>
- see below.
@@ -282,6 +325,22 @@ where | |||
Ok(v) | |||
} | |||
|
|||
fn extract_sequence_into_slice<'s, T>(obj: &'s PyAny, slice: &mut [T]) -> PyResult<()> |
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.
How about extract_sequence_inplace
?
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.
I tend to think _inplace
as referring to the first argument, so I would vote against that name.
extract_sequence_into_slice
is fine imo.
Thank you! |
Also thanks from me!
I slightly disagree with this. For example, the following function will not compile without the
|
CHANGELOG.md
Outdated
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. | |||
|
|||
## [0.9.0] | |||
|
|||
* Implement `*Py` traits for arrays (up to 32). |
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.
Can you please change the line to read the following and also move it to the Added
section of the 0.9.0
changelog.
* Implement `*Py` traits for arrays (up to 32). | |
* Implement `FromPyObject` and `IntoPy<PyObject>` traits for arrays (up to 32). [#778](https://github.com/PyO3/pyo3/pull/778) |
src/types/list.rs
Outdated
create_tests!(array); | ||
create_tests!(vec vec!); |
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.
I think this is a good idea, thanks for adding comprehensive tests!
FromPyObject
and IntoPy<PyObject>
traits for arrays (up to 32)
079029d
to
4d5241d
Compare
Comments addressed. Thank you guys |
That makes sense, thanks 👍 |
src/types/list.rs
Outdated
$( | ||
impl<T> IntoPy<PyObject> for [T; $N] | ||
where | ||
T: Copy + IntoPy<PyObject>, |
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.
Let's change this bound to T: ToPyObject
.
Then we can use self.as_ref().to_object(py)
for implementation.
src/types/sequence.rs
Outdated
T: FromPyObject<'s>, | ||
{ | ||
let seq = <PySequence as PyTryFrom>::try_from(obj)?; | ||
if (seq.len()? as usize) != slice.len() { |
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.
if (seq.len()? as usize) != slice.len() { | |
if seq.len()? as usize != slice.len() { |
src/types/sequence.rs
Outdated
@@ -330,6 +389,9 @@ impl<'v> PyTryFrom<'v> for PySequence { | |||
|
|||
#[cfg(test)] | |||
mod test { | |||
macro_rules! create_tests { |
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.
Here not all tests are for FromPyObject
.
Do we need to repeat all of them?
I think what we really need is array version of test_extract_bytearray_to_vec
.
src/types/list.rs
Outdated
@@ -205,6 +232,9 @@ where | |||
|
|||
#[cfg(test)] | |||
mod test { |
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.
Same as below.
Don't need to repeat all tests.
Thank you for addressing comments, but as I commented, I think current macro-generated tests seem to make it unclear what is tested here. We can say the same thing for |
I was waiting for David but looks like these tests are actually unwanted |
Thanks - sorry about the extra work there. I thought your macro test implementation was nice, but @kngwyu has been a maintainer of pyo3 for much longer than I so I'm still deferring to his opinion when we differ. I wasn't going to ask you to undo the work though - was thinking about pushing a change myself. Thanks for doing so. |
@c410-f3r
You don't have to do so. We sometimes make mistakes, but fortunately, we can help us with each other. When our opinion differs, it just means we need a discussion. |
Fixes #775.
Copy + Default
bounds to initialize memory forcopy_to_slice_impl
and avoid unsafe (MaybeUninit
andptr
friends).Copy
bound is also necessary due to the lack of a "real"IntoIterator
implementation for arrays.