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(VolatileFieldAccess)] for easy, access-limited field-based access to structs #49

Merged
merged 1 commit into from
Apr 21, 2024

Conversation

mkroening
Copy link
Member

This PR adds a new proc-macro that adds additional functions to VolatilePtr for user-defined types, with support for access-limitations.

This means this code

#[repr(C)]
#[derive(Volatile)]
pub struct DeviceConfig {
    feature_select: u32,
    #[access(ReadOnly)]
    feature: u32,
}

will derive this code:

pub trait DeviceConfigVolatile<'a> {
    fn feature_select(self) -> VolatilePtr<'a, u32, ReadWrite>;

    fn feature(self) -> VolatilePtr<'a, u32, ReadOnly>;
}

impl<'a> DeviceConfigVolatile<'a> for VolatilePtr<'a, DeviceConfig, ReadWrite> {
    fn feature_select(self) -> VolatilePtr<'a, u32, ReadWrite> {
        map_field!(self.feature_select).restrict()
    }

    fn feature(self) -> VolatilePtr<'a, u32, ReadOnly> {
        map_field!(self.feature).restrict()
    }
}

My motivation for this is that I found map_field! not ideal to use when your field is nested (map_field!(one.two.three) does not work) and it does not chain well (cannot be nested).
Additionally, this proc-macro allows you to properly specify access restrictions for your fields.

I thought this would be a nice fit for this repository rather than publishing the crate independently, for visibility, discoverability, and maintenance.
What are your thoughts on this?

This PR depends on #47

Copy link
Member

@phil-opp phil-opp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, this is really nice! I like having getter-like methods instead of macro invocations and I think rust-analyzer works better this way too.

My motivation for this is that I found map_field! not ideal to use when your field is nested (map_field!(one.two.three) does not work) and it does not chain well (cannot be nested).

I opened #50 with an attempt to remove this limitation. Maybe you could take a quick look?

volatile-macro/src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
volatile-macro/src/volatile.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
… to structs

Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
@mkroening
Copy link
Member Author

I have also slightly refactored the macro implementation (parsing into one struct instead of many) and have made the macro copy the docs from struct to trait.

@phil-opp phil-opp merged commit d0d2215 into rust-osdev:main Apr 21, 2024
5 checks passed
@phil-opp
Copy link
Member

Thanks a lot!

Should I create a new release or do you have other changes planned that we should wait for?

@mkroening mkroening deleted the derive branch April 21, 2024 16:38
@mkroening
Copy link
Member Author

Should I create a new release or do you have other changes planned that we should wait for?

I just opened #51 and #52, but they are not urgent. Apart from that, this should be good to go. Thanks a lot! :)

@mkroening mkroening changed the title Add #[derive(Volatile)] for easy, access-limited field-based access to structs Add #[derive(VolatileFieldAccess)] for easy, access-limited field-based access to structs Apr 21, 2024
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.

2 participants