Skip to content

Add #[derive(VolatileFieldAccess)] for easy, access-limited field-based access to structs#49

Merged
phil-opp merged 1 commit intorust-osdev:mainfrom
mkroening:derive
Apr 21, 2024
Merged

Add #[derive(VolatileFieldAccess)] for easy, access-limited field-based access to structs#49
phil-opp merged 1 commit intorust-osdev:mainfrom
mkroening:derive

Conversation

@mkroening
Copy link
Copy Markdown
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
Copy Markdown
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?

Comment thread volatile-macro/src/lib.rs Outdated
Comment thread src/lib.rs
Comment thread volatile-macro/src/volatile.rs
Comment thread src/lib.rs
… to structs

Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
@mkroening
Copy link
Copy Markdown
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
@phil-opp
Copy link
Copy Markdown
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
Copy Markdown
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