google / google/safe-mmio

Allow child fields in field!

Open
#39 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
41
Forks
9
PR merge metrics
No merged PRs in 30d

Description

I ran into an annoyance with the current implementation (v0.2.7).

In register blocks, sometimes there are logical blocks of registers, which can be grouped with a struct.
I want to be able to do something like this without having to manage a temporary variable for field and all the lifetime annoyances mismanaging it brings:
```rust
field!(self.regs, field.child_field).write({...});
```

Maybe change `field!` like this?
```rust
macro_rules! field {
($mmio_pointer:expr, $field:ident$(.$child_field:ident)*) => {{
// Make sure $mmio_pointer is the right type.
let mmio_pointer: &mut safe_mmio::UniqueMmioPointer<_> = &mut $mmio_pointer;
// SAFETY: ptr_mut is guaranteed to return a valid pointer for MMIO, so the pointer to the
// field must also be valid. MmioPointer::child gives it the same lifetime as the original
// pointer.
unsafe {
let child_pointer =
core::ptr::NonNull::new(&raw mut (*mmio_pointer.ptr_mut()).$field).unwrap();
$(
let child_pointer = core::ptr::NonNull::new(&raw mut (*child_pointer.as_ptr()).$child_field).unwrap();
)*
mmio_pointer.child(child_pointer)
}
}};
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.