Pyo3: Unable to use instance of pyclass

Created on 10 Sep 2018  路  2Comments  路  Source: PyO3/pyo3

Hi community,

I have a use case where I am defining a pyclass and I need to have a static method in that class that takes the same pyclass along with another pyclass as parameters and does some computation on them.

When I try the below code, I get "FromPyObject" not implemented for PyG1 and PyG2, how do I go about fixing this, any help is appreciated.
```rust

[pyclass]

struct PyG1 {
g1 : G1
}

[pymethods]

impl PyG1 {

#[new]                                                                                                                                                                               
fn __new__(obj: &PyRawObject) -> PyResult<()>{                                                                                                                                       
    let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);                                                                                          
    let g =  G1::rand(&mut rng);                                                                                                                                                     
    obj.init(|t| PyG1{                                                                                                                                                               
        g1: g,                                                                                                                                                                       
    })                                                                                                                                                                               
}                                                                                                                                                                                    

#[staticmethod]                                                                                                                                                                      
fn py_pairing(g1: PyG1, g2: PyG2) -> PyResult<()> {                                                                                                                                  
    Ok(())                                                                                                                                                                           
}

}

[pyclass]

struct PyG2 {
g2 : G2
}

[pymethods]

impl PyG2 {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()>{
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let g = G2::rand(&mut rng);
obj.init(|t| PyG2{
g2: g,
})
}

}

Most helpful comment

Since python is call-by-reference only, you can only get references in function calls for your custom types. fn py_pairing(g1: &PyG1, g2: &PyG2) -> PyResult<()> does the trick.

All 2 comments

@hybridNeo You have some formatting issues in your post. Surround your code with
~~~markdown

// Your code

~~~
in order to make it easier to understand.

Since python is call-by-reference only, you can only get references in function calls for your custom types. fn py_pairing(g1: &PyG1, g2: &PyG2) -> PyResult<()> does the trick.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

konstin picture konstin  路  5Comments

jamesray1 picture jamesray1  路  3Comments

fubuloubu picture fubuloubu  路  7Comments

acminor picture acminor  路  3Comments

mvaled picture mvaled  路  3Comments