webrtc icon indicating copy to clipboard operation
webrtc copied to clipboard

Newtype internal states

Open w-utter opened this issue 1 year ago • 0 comments

Currently all internal states are stored as an AtomicU8 which is not as descriptive as we can be. I think moving to a newtyped State<T> type would be more helpful in being explicit about the type that we are storing.

Below is an example of what could be used instead

struct State<T> where T: Into<u8> + From<u8> {
    inner: std::sync::AtomicU8,
    _: core::marker::PhantomData<T>,
}

impl <T> State<T> where T: Into<u8> + From<u8> {
    fn new(state: T) -> Self {
        inner: state,
        _: core::marker::PhantomData,
    }
    
    fn load(&self) -> T {
        self.inner.load(Ordering::SeqCst).into();
    }
    
    fn store(&mut self, state: T) {
        self.inner.store(state.into(), Ordering::SeqCst);
    }
}

w-utter avatar Jan 06 '24 07:01 w-utter