Not a constructor, an initializer?

⚓ Rust    📅 2025-09-26    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 34 days ago. The information described in this article may have changed.

So my embedded environment does not support allocation - everything must be statically allocated. I have a LARGE (complex) data structure that I can easily initialize with code, but it is really hard to initialize statically at compile time.

HOWEVER - RUST demands that I always declare and initialize :frowning: what a pain.

I've read through this: https://stackoverflow.com/questions/73175571/how-to-use-a-struct-without-initializing-its-members and Google is not helping me.

a) The structure is defined in C - not rust (Think device driver structure)
b) I pass a pointer to that struct to a C initialization function (along with a device name)
c) RUST says no - I have to initialize the damn thing.

What's a workaround? The code is sort of like this:

     // My example is a network interface structure */
     #[repr(C)]
     struct network_interface { /* lots of stuff here */ };
     struct uart_interface { /* lots of stuff here */ };
     /* i have others, I2C, SPI, etc */

     /* I'd like to do something like this, note: Not initialized! */
     pub static eth_FRONT_PANNEL : network_driver_interface;
     pub static eth_BACK_PANNEL  : network_driver_interface;;
     pub static uartFRONT_PANNEL : uart_driver_interface;
     pub static uartBACK_PANNEL : uart_driver_interface;

     pub fn bsp_init_drivers()
     {
          ether_driver_initializer( &eth_FRONT_PANNEL, "eth0" );
          ether_driver_initializer( &eth_BACK_PANNEL, "eth1" );
          // other drivers here too
      }

I effectively need to tell rust:
The variable FOO is initialized by means you cannot determine.

1 post - 1 participant

Read full topic

🏷️ Rust_feed