修复 rust 引用问题

This commit is contained in:
YuWuKunCheng
2026-05-28 01:07:16 +08:00
parent 4908870245
commit 52823e6c75
19 changed files with 1777 additions and 323 deletions
+181 -52
View File
@@ -72,23 +72,23 @@ impl 分型Py {
#[getter]
fn (&self) -> Option<K线Py> {
self.inner..as_ref().map(|k| K线Py {
inner: Rc::clone(k),
})
self.inner
.
.as_ref()
.map(|k| K线Py::from_rc(Rc::clone(k)))
}
#[getter]
fn (&self) -> K线Py {
K线Py {
inner: Rc::clone(&self.inner.),
}
K线Py::from_rc(Rc::clone(&self.inner.))
}
#[getter]
fn (&self) -> Option<K线Py> {
self.inner..as_ref().map(|k| K线Py {
inner: Rc::clone(k),
})
self.inner
.
.as_ref()
.map(|k| K线Py::from_rc(Rc::clone(k)))
}
#[getter]
@@ -116,6 +116,17 @@ impl 分型Py {
self.__str__()
}
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
}
#[getter]
fn (&self) -> Option<(Py, Py, Py)> {
self.inner.().map(|(a, b, c)| {
@@ -208,8 +219,8 @@ impl 分型Py {
/// 方法:
/// 之前是(其他虚线) -> bool — 判断当前虚线是否紧接在另一虚线之前
/// 之后是(其他虚线) -> bool — 判断当前虚线是否紧接在另一虚线之后
/// 获取普K序列(普K序列) -> list[K线] — 截取该虚线的原始K线范围
/// 获取缠K序列(缠K序列) -> list[缠论K线] — 截取该虚线的缠K范围
/// 获取普K序列(观察员) -> list[K线] — 截取该虚线的原始K线范围
/// 获取缠K序列(观察员) -> list[缠论K线] — 截取该虚线的缠K范围
/// 获取数据文本() -> str
///
/// 类方法(算法辅助):
@@ -294,6 +305,25 @@ impl 虚线Py {
self.inner._特征序列_显示
}
#[getter]
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for item in &self.inner. {
match item {
Some(feat) => list.append(Py::new(
py,
线Py {
inner: Rc::clone(feat),
},
)?)?,
None => {
list.append(py.None())?;
}
}
}
Ok(list.into())
}
#[getter]
fn (&self) -> bool {
self.inner.
@@ -301,9 +331,10 @@ impl 虚线Py {
#[getter]
fn K线(&self) -> Option<K线Py> {
self.inner.K线.as_ref().map(|k| K线Py {
inner: Rc::clone(k),
})
self.inner
.K线
.as_ref()
.map(|k| K线Py::from_rc(Rc::clone(k)))
}
#[getter]
@@ -413,39 +444,43 @@ impl 虚线Py {
self.inner.(&.borrow().inner)
}
fn K序列(&self, K序列: Vec<Py<K线Py>>, py: Python<'_>) -> PyResult<Py<PyAny>> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.collect();
let result = self.inner.K序列(&rc_list);
let list = pyo3::types::PyList::empty(py);
fn K序列(
&self,
: &Bound<'_, crate::business_py::Py>,
) -> PyResult<Py<PyAny>> {
let obs_ref = .borrow();
let observer_inner = obs_ref.obs();
let result = self.inner.K序列(&observer_inner.K线序列);
let list = pyo3::types::PyList::empty(.py());
for k in &result {
list.append(K线Py {
inner: (**k).clone(),
})?;
}
Ok(list.into())
}
fn K序列(
&self, K序列: Vec<Py<K线Py>>, py: Python<'_>
) -> PyResult<Py<PyAny>> {
let rc_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.collect();
let result = self.inner.K序列(&rc_list);
let list = pyo3::types::PyList::empty(py);
for k in &result {
list.append(K线Py {
inner: Rc::clone(k),
})?;
}
Ok(list.into())
}
#[getter]
fn K序列(
&self,
: &Bound<'_, crate::business_py::Py>,
) -> PyResult<Py<PyAny>> {
let obs_ref = .borrow();
let observer_inner = obs_ref.obs();
let result = self.inner.K序列(&observer_inner.K线序列);
let list = pyo3::types::PyList::empty(.py());
for k in &result {
list.append(K线Py::from_rc(Rc::clone(k)))?;
}
Ok(list.into())
}
#[classmethod]
fn _武(_cls: &Bound<'_, PyType>, 线: &Bound<'_, Self>) -> Py {
Py {
inner: 线.borrow().inner._武(),
}
}
fn (&self) -> String {
self.inner.()
}
@@ -458,6 +493,17 @@ impl 虚线Py {
self.__str__()
}
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
}
// ---- 静态工厂方法 ----
#[classmethod]
@@ -547,7 +593,7 @@ impl 虚线Py {
) -> f64 {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD柱子均值(
&rc_list,
@@ -564,7 +610,7 @@ impl 虚线Py {
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD均值(
&rc_list,
@@ -581,7 +627,7 @@ impl 虚线Py {
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD均值(&rc_list, &线.borrow().inner)
}
@@ -595,7 +641,7 @@ impl 虚线Py {
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD极值(&rc_list, &线.borrow().inner)
}
@@ -609,7 +655,7 @@ impl 虚线Py {
) -> Option<f64> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD柱子均值_阴(
&rc_list,
@@ -626,7 +672,7 @@ impl 虚线Py {
) -> Option<f64> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD柱子均值_阳(
&rc_list,
@@ -643,7 +689,7 @@ impl 虚线Py {
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD均值_阴(&rc_list, &线.borrow().inner)
}
@@ -657,7 +703,7 @@ impl 虚线Py {
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD均值_阳(&rc_list, &线.borrow().inner)
}
@@ -671,7 +717,7 @@ impl 虚线Py {
) -> [bool; 3] {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::K线序列MACD趋向背驰(
&rc_list,
@@ -687,7 +733,7 @@ impl 虚线Py {
) -> Vec<Vec<f64>> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = k线序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD柱子分段(&rc_list)
}
@@ -713,12 +759,37 @@ impl 虚线Py {
: usize,
: usize,
py: Python<'_>,
) -> HashMap<String, String> {
) -> PyResult<Py<PyAny>> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| Rc::new(k.bind(py).borrow().inner.clone()))
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
chanlun::structure::dash_line::线::MACD行为(&rc_list, , )
let result =
chanlun::structure::dash_line::线::MACD行为(&rc_list, , );
let dict = pyo3::types::PyDict::new(py);
dict.set_item("DIF上穿0", result.DIF上穿0)?;
dict.set_item("DIF下穿0", result.DIF下穿0)?;
dict.set_item("DEA上穿0", result.DEA上穿0)?;
dict.set_item("DEA下穿0", result.DEA下穿0)?;
dict.set_item("金叉次数", result.)?;
dict.set_item("死叉次数", result.)?;
let : Vec<Py<PyAny>> = result
.
.iter()
.map(|(a, b, c)| {
let tup = pyo3::types::PyTuple::new(
py,
[
(*a).into_pyobject(py)?.into_any().unbind(),
(*b).into_pyobject(py)?.into_any().unbind(),
(*c).into_pyobject(py)?.into_any().unbind(),
],
)?;
Ok(tup.into_any().unbind())
})
.collect::<PyResult<Vec<_>>>()?;
dict.set_item("密集交叉区域", )?;
Ok(dict.into())
}
#[classmethod]
@@ -821,6 +892,53 @@ impl 线段特征Py {
self.__str__()
}
fn __len__(&self) -> usize {
self.inner..len()
}
fn __getitem__(&self, index: isize, py: Python<'_>) -> PyResult<Py<PyAny>> {
let len = self.inner..len() as isize;
let idx = if index < 0 { index + len } else { index };
if idx < 0 || idx >= len {
return Err(pyo3::exceptions::PyIndexError::new_err(format!(
"线段特征 index {index} out of range (len={len})"
)));
}
let dash = &self.inner.[idx as usize];
let obj: Py<PyAny> = Py::new(
py,
线Py {
inner: Rc::clone(dash),
},
)?
.into();
Ok(obj)
}
fn __iter__(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.inner. {
list.append(Py::new(
py,
线Py {
inner: Rc::clone(d),
},
)?)?;
}
list.call_method0("__iter__").map(|iter| iter.into())
}
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
}
// ---- instance methods ----
#[getter]
@@ -1004,6 +1122,17 @@ impl 特征分型Py {
fn __repr__(&self) -> String {
self.__str__()
}
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
}
}
pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {