Skip to content

Emit const function for bitfields in bindings #392

@Luro02

Description

@Luro02

Motivations

  • Would you like to implement this feature? maybe

While working on my PR for esp-idf-hal esp-rs/esp-idf-hal#548 I noticed that I am unable to create a const for the used rmt_symbol_word_t wrapper.

This is unfortunate, because I would have to use lazy_static instead or leave them in the respective functions as local variables:

fn byte_to_symbols(byte: u8) -> impl IntoIterator<Item = Symbol> {
    let ws2812_zero = Symbol::new_with(
        RMT_LED_STRIP_RESOLUTION_HZ,
        PinState::High,
        Duration::from_micros(300),
        PinState::Low,
        Duration::from_micros(900),
    )
    .unwrap();
    let ws2812_one = Symbol::new_with(
        RMT_LED_STRIP_RESOLUTION_HZ,
        PinState::High,
        Duration::from_micros(900),
        PinState::Low,
        Duration::from_micros(300),
    )
    .unwrap();

    (0..8)
        .map(|i| 0x80 >> i)
        .map(move |bitmask| (byte & bitmask) != 0)
        .map(move |is_one| if is_one { ws2812_one } else { ws2812_zero })
}

It is a relatively minor inconvenience, but given that the generated code like this one

    #[inline]
    pub fn set_level0(&mut self, val: u16) {
        unsafe {
            let val: u16 = ::core::mem::transmute(val);
            self._bitfield_1.set(15usize, 1u8, val as u64)
        }
    }

could be a const function, I am making this issue in the hopes that this can be achieved by simply setting a flag when generating the bindings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions