Skip to content

Custom Element implementation leads to weird results #386

Closed Answered by hombit
hombit asked this question in Q&A
Discussion options

You must be logged in to vote

Just for the records: I was able to make it work with np.dtype(align=True). The full working example:

use numpy::Element;
use numpy::PyArray1;
use numpy::PyArrayDescr;
use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::types::PyDict;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct Record {
    x: f64,
    y: f64,
    k: i32,
}

fn dtype<'py>(py: Python<'py>) -> PyResult<&'py PyArrayDescr> {
    let locals = PyDict::new(py);
    py_run!(
        py,
        *locals,
        "dtype = __import__('numpy').dtype([('x', 'f8'), ('y', 'f8'), ('k', 'i4')], align=True)"
    );
    Ok(locals
        .get_item("dtype")
        .unwrap()
        .downcast::<PyArrayDescr>()?)
}

unsafe impl Element for R…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@hombit
Comment options

Answer selected by hombit
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants