So, I am developing a library and I would like the feature parallel to be optional. If it is not enabled, then I will default to std::rc::Rc<>, but when it is activated I will use std::sync::Arc<>. Now, I have a trait somewhere that needs to be extended by Sync and Send in one case but not in the other. I wonder if it is possible to use conditional compilation to handle this...?
// My initial thought was this, which I know is experimental... I would like to avoid
// experimental things.
#[cfg(feature = "parallel")]
trait Aux = Intersect + Sync + Send;
#[cfg(not(feature = "parallel"))]
trait Aux = Intersect;
pub trait Sampleable : Aux {
//... fill this
}
You only have to write literally this much code – I thought the very point of the Aux trait was to encapsulate this difference only, and that the bulk of the real code resides in Sampleable. If that is not the case, you can always introduce an additional trait between the two.
Rc is cheaper to clone than Arc ( since it doesn't have to go to shared memory rather than local CPU cache memory ).
[ What I have found useful, not sure if you could call it a design pattern, is to have local Rc pointers which point to a struct which has the actual Arc data in it ( and can therefore be shared between threads ). There is a lot of Rc cloning, but Arc clones are infrequent. ]