Optional fields in {} constructors

In the last days, I tried to update my Bevy chess GUI from Bevy 0.14 to 0.16, and so I had to study a few of the recent Bevy examples codes. One is Text which has the code segment below. I wonder if there is really no more compact method available for the optional font field?

if cfg!(feature = "default_font") {
    (
        TextFont {
            font_size: 33.0,
            // If no font is specified, the default font (a minimal subset of FiraMono) will be used.
            ..default()
        },
        TextColor(GOLD.into()),
    )
} else {
    (
        // "default_font" feature is unavailable, load a font to use instead.
        TextFont {
            font: asset_server.load("fonts/FiraMono-Medium.ttf"),
            font_size: 33.0,
            ..Default::default()
        },
        TextColor(GOLD.into()),
    )
},

OK, the two GPT-5 solutions seems to work:

First:

(
    TextFont {
        font: if cfg!(feature = "default_font") {
            default()
        } else {
            asset_server.load("fonts/FiraMono-Medium.ttf")
        },
        font_size: 33.0,
        ..default()
    },
    TextColor(GOLD.into()),
),

Or for true conditional compilation (only one branch compiled):

At the top of the function:

#[cfg(feature = "default_font")]
let font: Handle<Font> = default();

#[cfg(not(feature = "default_font"))]
let font: Handle<Font> = asset_server.load("fonts/FiraMono-Medium.ttf");



and then

(
    TextFont { font, font_size: 33.0, ..default() },
    TextColor(GOLD.into()),
)

Another option is to use #[cfg]:

(
    TextFont {
        font_size: 33.0,
        #[cfg(not(feature = "default_font"))]
        font: asset_server.load("fonts/FiraMono-Medium.ttf"),
        ..default()
    },
    TextColor(GOLD.into()),
)

The difference is that this uses the value of the font field from TextFont::default(), whereas yours will use the value produced by Handle::<Font>::default(). These happen to be the same in Bevy 0.16, but for an arbitrary choice of struct, they may be different values.

4 Likes

...especially once https://github.com/rust-lang/rfcs/pull/3681 stabilizes.

3 Likes

Thanks. That was my assumption initially, but GPT-5 first said it would not work, with a strange explanation. When I told it its explanation makes not much sense, it suggested

(
    TextFont {
        #[cfg(feature = "default_font")]
        font: default(), // Handle::<Font>::default()

        #[cfg(not(feature = "default_font"))]
        font: asset_server.load("fonts/FiraMono-Medium.ttf"),

        font_size: 33.0,
        ..default()
    },
    TextColor(GOLD.into()),
)

and when I replied that due to ..default() the code below should also work, it finally agreed:

(
    TextFont {
        //#[cfg(feature = "default_font")]
        //font: default(), // Handle::<Font>::default()

        #[cfg(not(feature = "default_font"))]
        font: asset_server.load("fonts/FiraMono-Medium.ttf"),

        font_size: 33.0,
        ..default()
    },
    TextColor(GOLD.into()),
),

This is for latest Rust 1.89 -- my own earlier attempts for such conditional compilation most of the time failed, I assume because of my own stupid syntax errors.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.