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

Add #[derive(ListBuild)] #223

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft

Conversation

Ben-PH
Copy link

@Ben-PH Ben-PH commented Aug 30, 2023

This allows a struct to be annotated with a ListBuild derive. Essentially, all it does is create a hl_new function for the struct it's annotating. hl_new constructs the type by plucking a value from the passed-in list for each field in the struct, returning the new struct, and the post-plucked list.

The motivation is for it to be used in esp32-hal crate. Currently, managing pins is done with a struct. These pins are zero-sized, const-generic separated types. In order to build a peripheral, you need to be careful partial moves. This is not feasable:

impl Peripheral {
    fn new(pins: Pins) -> (Self, Pins) { 
        (Self {
            pins.gpio1
        }, pins)
    }
}

This PR allows for code like this:

#[derive(ListBuild)]
struct Peripheral {
    pin1: GpioPin<1>,
}

let (periph, list) = Peripheral::hl_new(list);

...this makes helpers, constructors, etc. an option, opening the posibility for much more idiomatic organisation of code.

It also allows for correctness of managing resources to be easily encapsulated at the type-level. The global set of resources (pins, clocks, etc.) could be encapsulated as a type, and all resources get rolled-up into a single HList. You cannot pluck the same type from this HList more than once. You always have on hand a list of available resources.

Future work:

  • be able to set initialization methods. e.g. if a field is annotated with into_push_pull_output(fielda: SomeType) on fielda:
let (fielda, l1): (SomeType, L1) = l0.pluck();
let fielda = fielda.into_push_pull_output()
  • recover items back into a list:
fn recover<L0, L1>(self, l0: L0) -> L1 {
    l0.push(self.fielda)
}

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

Successfully merging this pull request may close these issues.

1 participant