-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix compile error when trying to use static slot methods
- Loading branch information
1 parent
658bc6b
commit 92e99e3
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![cfg(feature = "macros")] | ||
|
||
use pyo3::exceptions::PyIndexError; | ||
use pyo3::prelude::*; | ||
use pyo3::types::IntoPyDict; | ||
|
||
use pyo3::py_run; | ||
|
||
mod common; | ||
|
||
#[pyclass] | ||
struct Vector3 { | ||
elements: [f64; 3], | ||
} | ||
|
||
#[pymethods] | ||
impl Vector3 { | ||
#[new] | ||
fn new(x: f64, y: f64, z: f64) -> Self { | ||
Self { | ||
elements: [x, y, z], | ||
} | ||
} | ||
|
||
#[staticmethod] | ||
fn __len__() -> usize { | ||
3 | ||
} | ||
|
||
fn __getitem__(&self, idx: isize) -> PyResult<f64> { | ||
self.elements | ||
.get(idx as usize) | ||
.copied() | ||
.ok_or_else(|| PyIndexError::new_err("list index out of range")) | ||
} | ||
|
||
fn __setitem__(&mut self, idx: isize, value: f64) { | ||
self.elements[idx as usize] = value; | ||
} | ||
} | ||
|
||
/// Return a dict with `s = Vector3(1, 2, 3)`. | ||
fn seq_dict(py: Python<'_>) -> &pyo3::types::PyDict { | ||
let d = [("Vector3", py.get_type::<Vector3>())].into_py_dict(py); | ||
// Though we can construct `s` in Rust, let's test `__new__` works. | ||
py_run!(py, *d, "s = Vector3(1, 2, 3)"); | ||
d | ||
} | ||
|
||
#[test] | ||
fn test_len() { | ||
Python::with_gil(|py| { | ||
let d = seq_dict(py); | ||
|
||
py_assert!(py, *d, "len(s) == 3"); | ||
}); | ||
} |