jonas-schievink / rubble

(going to be a) BLE stack for embedded Rust
BSD Zero Clause License
398 stars 55 forks source link

BleRadio buffers are &'static mut, which is difficult to satisfy. Could it be easier? #184

Closed azymohliad closed 2 years ago

azymohliad commented 2 years ago

Hi! I wanted to replicate nrf52-beacon demo with RTIC 0.6.0-rc.2, and I struggled with providing rubble-nrf5x::BleRadio with TX and RX buffers.

BleRadio requires buffers to be &'static mut, which is quite difficult to satisfy cleanly, if I'm not missing anything. It played well with RTIC 0.5 resources, but e.g. RTIC 0.6 (as I understand) no longer has static resources, and all resources are now in return value from init task, so now you have to initialize those buffers in the init task (I might be wrong here, I'm quite new to RTIC). Just to be sure, is there a better way to create BleRadio instance with RTIC 0.6 (or in general case), rather than declaring those buffers as static mut which implies unsafe?

Though even if BleRadio buffers didn't require static lifetime but were still borrowed, it would still be difficult to wrap them together in the single struct (as below, e.g. for RTIC 0.6 local resources) cleanly:

struct SomeStruct {
    ble_tx_buf: PacketBuffer,
    ble_rx_buf: PacketBuffer,
    radio: BleRadio,
}

Such struct can't be fully initialized at once, because BleRadio::new() would need to borrow other fields of the struct. One way is to instantiate mutable struct with initialized buffers and dummy radio field (or making it None with Option), then instantiate proper BleRadio with borrowed buffers from the struct, and then replace the dummy radio value with the correct one it in the struct. But it doesn't look optimal.

And so I was thinking, what if BleRadio owned buffers instead of borrowing them, and their sizes could be specified with const generic params?

azymohliad commented 2 years ago

After some more practice with embedded side of Rust, I realized I had a very naive and distorted view about it. Please, disregard this issue. Sorry for the noise.