我有一个比这更复杂的问题。但我认为这会使它崩溃。我有一些泛型结构,它有三个构造函数来获得一个具体类型的结构。除了构造函数之外,它们都有相同的泛型方法。我想要的是这样的东西:
use pyo3::prelude::*;
#[pyclass]
struct AnyVec<T> {
vec_: Vec<T>,
}
// General methods which
#[pymethods]
impl<T> AnyVec<T> {
fn push(&mut self, v: T) {
self.vec_.push(v)
}
fn pop(&mut self, v: T) -> T {
self.vec_.pop()
}
}
#[pymethods]
impl AnyVec<String> {
#[new]
fn new() -> Self {
AnyVec { vec_: vec![] }
}
}
#[pymethods]
impl AnyVec<f32> {
#[new]
fn new() -> Self {
AnyVec { vec_: vec![] }
}
}
当我试图编译这个的时候pyo3
警告我它不能使用泛型error: #[pyclass] cannot have generic parameters
是否可以创建某种类型的泛型基class
,具体类型从中继承
完成;这是我的Cargo.toml
:
[package]
name = "example"
edition = "2018"
[dependencies]
pyo3 = { version="0.9.0-alpha.1", features = ["extension-module"] }
[lib]
name = "example"
crate-type = ["cdylib"]
目前没有回答
相关问题 更多 >
编程相关推荐