swift-mmio
swift-mmio copied to clipboard
Merge properties of `Raw` struct into `Read`/`Write` structs
(I originally proposed this change here.)
The @Register
macro generates 3 structs: Read
, Write
, and Raw
. The Raw
struct is accessed through the .raw
property of the Read
and Write
structs:
@Register(bitWidth: 32)
struct CR1 {
@ReadOnly(bits: 0..<1, as: Bool.self)
var en: EN
@WriteOnly(bits: 1..<2, as: Bool.self)
var clken: CLKEN
}
cr1.modify { (read, write) in
print(read.raw.en)
write.raw.clken = 1
}
One issue with this approach is that the generated Raw
struct is reused by both the Read
and Write
structs, which means it needs to contain all properties of both structs. However, that allows a developer to write something like write.raw.en = 1
, which is unsafe because en
is supposed to be read-only.
Instead, I propose that the properties of the Raw
struct be merged into the Read
/Write
structs. The new properties could be prefixed with $
, similar to property wrappers, to denote that the properties are just a different view on the non-prefixed properties.
cr1.modify { (read, write) in
print(read.$en)
write.$clken = 1
write.$en = 1 // compile error: `$en` does not exist
}
(If #70 is accepted, this change would also make the Read
/Write
structs less awkward by avoiding having similarly-named rawValue
and raw
properties.)