Hello, I tried to make a prototype of syntactic sugar for bit-fields.
My initial design is something like this:
declare_bitfields!(MipsOpR: u32 {
opcode: 6, rs: 5, rt: 5, rd: 5, shamt: 5, funct: 6
})
...which should expand to something like this:
enum MipsOpR = u32;
impl MipsOpR {
pure fn opcode(self) -> u32 {
let MipsOpR(__val) = self;
__val & 0xf
}
// this may go to ~MipsOpR etc.
fn set_opcode(&mut self, __new: u32) {
let MipsOpR(__val) = *self;
let __mask = (1<<6)-1, __shift = 5+5+5+6;
*self = MipsOpR((__val & !(__mask << __shift)) |
((__new & __mask) << __shift));
}
// ...snip...
}
The rules required for declare_bitfields!() are straightforward, but I have
encountered two problems while prototyping this:
- Multiple items are not allowed. (Issue #4375)
- concat_idents!() does not work for field/method names. (Issue #4365)
The first problem is more or less a matter of internal representations, but
the second problem is more severe because concat_idents!() is thought to be
made for exactly this situation. I personally prefer to have macros work on
the ident position, but there are several alternatives if this makes
the macro system more difficult:
- Add a set of built-in macros for generated names. e.g.
`declare_fn!(foo { ... })` where `foo` is actually an expr (so
concat_idents!() work) but declare_fn!() checks for expanded exprs which
are not idents.
- Make concat_idents!() occur during the expansion. So for example, we will
have something like `set_$+$e` (check for $e being an ident is done when
the macro is defined). Then concat_idents!() will be rewritten in terms of
$+ I think.
...Any thoughts?
--
-- Kang Seonghoon | Software Engineer, iPlateia Inc. | http://mearie.org/
-- Opinions expressed in this email do not necessarily represent the
views of my employer.
--
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev