Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newtype internal states #526

Open
w-utter opened this issue Jan 6, 2024 · 0 comments
Open

Newtype internal states #526

w-utter opened this issue Jan 6, 2024 · 0 comments

Comments

@w-utter
Copy link

w-utter commented Jan 6, 2024

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 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);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant