Pre-RFC: Overridable global static symbols

This pre-rfc aims to add a way to declare global static symbols such as static, const and type as override by the user as used by the standard library with the global allocator.

Use case

Features discussed by this pre-rfc can be used on crates implementing API dependent to platform features (e.g. Windowing library, Firmware driver…) who want to allow the end-user to use a custom backend for the whole project.

Early design

Declaring the global symbol

The global symbol can be declared in multiple ways:

  • The symbol must be defined outside the declaring crate:
// crate: device
// file: /lib.rs

#[overrideable]
pub type GlobalScreenDriver: ScreenDriver;

#[overrideable]
pub const STACK_WIDTH: usize;
  • The symbol have a default value:
#[overrideable]
pub const PRINT_DEBUG_UART: bool = true;

Assigning a custom global symbol

Any crate can assign a custom global symbol. However, only one library crate can assign it at a time. This assign can finally be overloaded by the binary crate.

Based on the declaration made in the previous section, here is an example of assignation:

#[overrides(device::GlobalScreenDriver)]
type ScreenDriver = st7789v::ScreenDriver;

For type aliases, it is possible to directly implement the associated type by setting the previous attribute.

4 Likes

Hello, Thank you for your answer, I missed these features and it fits with my needs :sweat_smile:

1 Like