amv-dev / yata

Yet Another Technical Analysis library [for Rust]
Apache License 2.0
334 stars 52 forks source link

need send on some traits #24

Closed Igosuki closed 2 years ago

Igosuki commented 2 years ago

Hi, I'm using yata with pyo3 which requires boxed values to be Send. IndicatorInstance isn't Send so I can't send IndicatorInstanceDyn between threads unless I mark it with Send, since IndicatorConfigDyn::init returns IndicatorInstanceDyn which isn't Send, I figured I needed to add the trait explicitly.

If you have a better option or recommendation feel free to close the PR with comment.

amv-dev commented 2 years ago

Hello. Thank you for you participation. Unfortunately, I don't think it's a good idea. It is better to keep traits restrictions as low as possible in the libraries.

IndicatorConfigDyn and IndicatorInstanceDyn in fact are just auto-traits. So you can create traits like this on your own:

trait IndicatorConfigSendDyn<T: OHLCV>: Send {
    fn init(&self, initial_value: &T) -> Result<Box<dyn IndicatorConfigSendDyn<T>>, Error>;
    ... // other methods
}

impl<T, C, I> IndicatorConfigSendDyn<T> for C 
where
    T: OHLCV,
    I: IndicatorInstanceSendDyn<T> + IndicatorInstance<Config = Self> + Send + 'static,
    C: IndicatorConfig<Instance = I> + Clone + Send + 'static,
{
    fn init(&self, initial_value: &T) -> Result<Box<dyn IndicatorInstanceSendDyn<T>>, Error> {
        let instance = IndicatorConfig::init(self.clone(), initial_value)?;
        Ok(Box::from(instance))
    }

    ... // other methods
}
Igosuki commented 2 years ago

You're right, copy paste will be enough, thanks.