GregoryConrad / rearch-rs

Re-imagined approach to application design and architecture
https://crates.io/crates/rearch
MIT License
81 stars 4 forks source link

Consider introducing trait to remove a lot of `Clone + Send + Sync` boilerplate #4

Closed GregoryConrad closed 10 months ago

GregoryConrad commented 11 months ago

We could either rename CapsuleData to something else to add this new trait (naming it CapsuleData), or we could just add in the new trait as so:

pub trait CData: Clone + Send + Sync {}
impl<T: Clone + Send + Sync> CData for T {}

This will help implementation logic some here and there, but most importantly, this will help library users when creating capsules that are higher order functions. Example:

// Before:
fn stateful(
    CapsuleHandle { register, .. }: CapsuleHandle,
) -> (u32, impl Fn(u32) + Clone + Send + Sync) {
    let (state, set_state) = register.register(side_effects::state(0));
    (*state, set_state)
}

// After:
fn stateful(CapsuleHandle { register, .. }: CapsuleHandle) -> (u32, impl CData + Fn(u32)) {
    let (state, set_state) = register.register(side_effects::state(0));
    (*state, set_state)
}

Not sure on what the name should be. Shorter is better because this will be abundant in user code, but we also don't want to lose meaning.

GregoryConrad commented 10 months ago

Also, this would have the added benefit of other container implementations (different crates) being able to expose the exact same (give or take) API while requiring different traits to be in a container (like perhaps a single threaded container wouldn't need Send + Sync, so it's CData is only impl<T: Clone> CData for T ).

GregoryConrad commented 10 months ago

Fixed in c4b36fb8