Skip to content

Commit 8027f86

Browse files
committed
refactor(pyargus): implement new for PySignal
1 parent e2cfe3d commit 8027f86

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

pyargus/src/semantics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use pyo3::prelude::*;
77
use pyo3::types::{PyDict, PyString};
88

99
use crate::expr::PyBoolExpr;
10-
use crate::signals::{BoolSignal, FloatSignal, PySignal, SignalKind};
10+
use crate::signals::{BoolSignal, FloatSignal, PyInterp, PySignal, SignalKind};
1111
use crate::PyArgusError;
1212

1313
#[pyclass(name = "Trace", module = "argus")]
@@ -63,12 +63,12 @@ impl Trace for PyTrace {
6363
#[pyfunction]
6464
fn eval_bool_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<BoolSignal>> {
6565
let sig = BooleanSemantics::eval(&expr.0, trace).map_err(PyArgusError::from)?;
66-
Python::with_gil(|py| Py::new(py, (BoolSignal, BoolSignal::super_type(sig.into()))))
66+
Python::with_gil(|py| Py::new(py, (BoolSignal, PySignal::new(sig, PyInterp::Linear))))
6767
}
6868
#[pyfunction]
6969
fn eval_robust_semantics(expr: &PyBoolExpr, trace: &PyTrace) -> PyResult<Py<FloatSignal>> {
7070
let sig = QuantitativeSemantics::eval(&expr.0, trace).map_err(PyArgusError::from)?;
71-
Python::with_gil(|py| Py::new(py, (FloatSignal, FloatSignal::super_type(sig.into()))))
71+
Python::with_gil(|py| Py::new(py, (FloatSignal, PySignal::new(sig, PyInterp::Linear))))
7272
}
7373

7474
pub fn init(_py: Python, m: &PyModule) -> PyResult<()> {

pyargus/src/signals.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ pub struct PySignal {
4040
pub(crate) signal: SignalKind,
4141
}
4242

43+
impl PySignal {
44+
pub fn new<T>(signal: T, interpolation: PyInterp) -> Self
45+
where
46+
T: Into<SignalKind>,
47+
{
48+
Self {
49+
interpolation,
50+
signal: signal.into(),
51+
}
52+
}
53+
}
54+
4355
#[pymethods]
4456
impl PySignal {
4557
#[getter]
@@ -106,31 +118,21 @@ macro_rules! impl_signals {
106118
#[derive(Debug, Copy, Clone)]
107119
pub struct [<$ty_name Signal>];
108120

109-
impl [<$ty_name Signal>] {
110-
#[inline]
111-
pub fn super_type(signal: SignalKind) -> PySignal {
112-
PySignal {
113-
interpolation: PyInterp::Linear,
114-
signal,
115-
}
116-
}
117-
}
118-
119121
#[pymethods]
120122
impl [<$ty_name Signal>] {
121123
/// Create a new empty signal
122124
#[new]
123125
#[pyo3(signature = ())]
124126
fn new() -> (Self, PySignal) {
125-
(Self, Self::super_type(Signal::<$ty>::new().into()))
127+
(Self, PySignal::new(Signal::<$ty>::new(), PyInterp::Linear))
126128
}
127129

128130
/// Create a new signal with constant value
129131
#[classmethod]
130132
fn constant(_: &PyType, py: Python<'_>, value: $ty) -> PyResult<Py<Self>> {
131133
Py::new(
132134
py,
133-
(Self, Self::super_type(Signal::constant(value).into()))
135+
(Self, PySignal::new(Signal::constant(value), PyInterp::Linear))
134136
)
135137
}
136138

@@ -144,7 +146,7 @@ macro_rules! impl_signals {
144146
Python::with_gil(|py| {
145147
Py::new(
146148
py,
147-
(Self, Self::super_type(ret.into()))
149+
(Self, PySignal::new(ret, PyInterp::Linear))
148150
)
149151
})
150152
}

0 commit comments

Comments
 (0)