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

[Proposal] Swap to Type State Model #165

Open
gluax opened this issue Feb 28, 2023 · 0 comments
Open

[Proposal] Swap to Type State Model #165

gluax opened this issue Feb 28, 2023 · 0 comments
Labels
enhancement New feature or request proposal A proposal for a change.
Milestone

Comments

@gluax
Copy link
Collaborator

gluax commented Feb 28, 2023

💥 Proposal

Currently, our types models contain two versions of structs: UncheckedFoo and a Foo for each type. This setup is such that we can align with the original rosetta SDK spec.

However, in terms of code quality, and API usage, it leaves much desired.

I suggest we leverage a type state model.

Behind the scenes, the code will still be ugly, but the API usage should be vastly improved as all types will be unified to one struct.

For example:

pub struct Checked;
pub struct Unchecked;

pub struct Foo<State = Unchecked> {
  account_ser: Option<Account<Unchecked>>,
  account: Option<Account<Checked>>,
  size_ser: Option<isize>,
  #[serde(skip)]
  size: Option<usize>,
  state: std::marker::PhantomDate<State>,
}

impl Foo<Unchecked> {
  pub fn account(&self) -> Option<&Account<Unchecked>> { self.account_ser.as_ref() }
  pub fn size(&self) -> Option<isize> { self.size_ser }
}

impl Foo<Checked> {
  pub fn account(&self) -> &Account<Checked> { self.account.as_ref().unwrap() // Can safe unwrap }
   pub fn size(&self) -> usize { self.size.unwrap() }
}

The asserter module would then transform the Unchecked version to the Checked version.

The usage of PhantomData should be fine as it's a zero-sized type at the end of the day optimized out of the way by the compiler.

@gluax gluax added the proposal A proposal for a change. label Feb 28, 2023
@gluax gluax added this to the 1.0 milestone Feb 28, 2023
@gluax gluax added the enhancement New feature or request label Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proposal A proposal for a change.
Projects
None yet
Development

No branches or pull requests

1 participant