winapi-rs
winapi-rs copied to clipboard
Allow for BOOL types in bitfield structs
I'm working with the DCB struct, which has a bunch of setters for boolean fields like set_fBinary() and set_fDsrSensitivity(). However, the current declaration makes all field setters take a DWORD. So instead of dcb.set_fBinary(TRUE) I have to write dcb.set_fBinary(TRUE as DWORD). It'd be more ergonomic and type-safe if instead of DWORD BOOL (or BOOLEAN, I'm not certain which is applicable here) could be used for some of the getters here. I'm wondering if the BITFIELDS! macro could be changed to either allow a per-field override type or allow for specifying the type per-field. So either:
BITFIELD!{DCB BitFields [
fBinary BOOL set_fBinary[0..1],
fParity BOOL set_fParity[1..2],
fOutxCtsFlow BOOL set_fOutxCtsFlow[2..3],
fOutxDsrFlow BOOL set_fOutxDsrFlow[3..4],
fDtrControl DWORD set_fDtrControl[4..6],
fDsrSensitivity BOOL set_fDsrSensitivity[6..7],
fTXContinueOnXoff BOOL set_fTXContinueOnXoff[7..8],
fOutX BOOL set_fOutX[8..9],
fInX BOOL set_fInX[9..10],
fErrorChar BOOL set_fErrorChar[10..11],
fNull BOOL set_fNull[11..12],
fRtsControl DWORD set_fRtsControl[12..14],
fAbortOnError BOOL set_fAbortOnError[14..15],
fDummy2 BOOL set_fDummy2[15..32],
]}
or
BITFIELD!{DCB BitFields: DWORD [
fBinary BOOL set_fBinary[0..1],
fParity BOOL set_fParity[1..2],
fOutxCtsFlow BOOL set_fOutxCtsFlow[2..3],
fOutxDsrFlow BOOL set_fOutxDsrFlow[3..4],
fDtrControl DWORD set_fDtrControl[4..6],
fDsrSensitivity BOOL set_fDsrSensitivity[6..7],
fTXContinueOnXoff BOOL set_fTXContinueOnXoff[7..8],
fOutX BOOL set_fOutX[8..9],
fInX BOOL set_fInX[9..10],
fErrorChar BOOL set_fErrorChar[10..11],
fNull BOOL set_fNull[11..12],
fRtsControl set_fRtsControl[12..14],
fAbortOnError BOOL set_fAbortOnError[14..15],
fDummy2 BOOL set_fDummy2[15..32],
]}
Note that there's also another type improvement that could be made here with DTR_CONTROL_* and RTS_CONTROL_*, as those are really enums for set_fDtrControl() and set_fRtsControl(). So it'd be even better if we could make an enum for that type and have it look like:
ENUM!{enum DTR_CONTROL {
DTR_CONTROL_DISABLE = 0, // Might make sense to shorten this to just DISABLE
DTR_CONTROL_ENABLE = 1,
DTR_CONTROL_HANDSHAKE = 2,
}}
ENUM!{enum RTS_CONTROL {
RTS_CONTROL_DISABLE = 0,
RTS_CONTROL_ENABLE = 1,
RTS_CONTROL_HANDSHAKE = 2,
RTS_CONTROL_TOGGLE = 3,
}}
STRUCT!{struct DCB {
DCBlength: DWORD,
BaudRate: DWORD,
BitFields: DWORD,
wReserved: WORD,
XonLim: WORD,
XoffLim: WORD,
ByteSize: BYTE,
Parity: BYTE,
StopBits: BYTE,
XonChar: c_char,
XoffChar: c_char,
ErrorChar: c_char,
EofChar: c_char,
EvtChar: c_char,
wReserved1: WORD,
}}
BITFIELD!{DCB BitFields [
fBinary BOOL set_fBinary[0..1],
fParity BOOL set_fParity[1..2],
fOutxCtsFlow BOOL set_fOutxCtsFlow[2..3],
fOutxDsrFlow BOOL set_fOutxDsrFlow[3..4],
fDtrControl DTR_CONTROL set_fDtrControl[4..6],
fDsrSensitivity BOOL set_fDsrSensitivity[6..7],
fTXContinueOnXoff BOOL set_fTXContinueOnXoff[7..8],
fOutX BOOL set_fOutX[8..9],
fInX BOOL set_fInX[9..10],
fErrorChar BOOL set_fErrorChar[10..11],
fNull BOOL set_fNull[11..12],
fRtsControl RTS_CONTROL set_fRtsControl[12..14],
fAbortOnError BOOL set_fAbortOnError[14..15],
fDummy2 BOOL set_fDummy2[15..32],
]}
Would either of these make sense to implement?