Skip to content

Add support for Box<dyn Format>#49

Open
Finomnis wants to merge 1 commit intobcmyers:mainfrom
Finomnis:box_dyn_format
Open

Add support for Box<dyn Format>#49
Finomnis wants to merge 1 commit intobcmyers:mainfrom
Finomnis:box_dyn_format

Conversation

@Finomnis
Copy link

@Finomnis Finomnis commented Jul 24, 2025

Rationale

I would like to be able to switch between a Locale and a SystemLocale at runtime. Therefore I would like to store them in Box<Locale> and Box<SystemLocale>, which both can be stored in a Box<dyn Format>.

Sadly, Box<dyn Format> is unsized and therefore unsupported by this crate.

As all the calls use &Format (as a reference), this can be fixed by adding + ?Sized to all function calls, allowing unsized object references.

This allows the following code to compile:

use num_format::{Format, Locale, SystemLocale, ToFormattedString};

fn main() {
    let locale: Box<dyn Format> = match SystemLocale::default() {
        Ok(sys_locale) => Box::new(sys_locale),
        Err(_) => Box::new(Locale::en),
    };

    println!("{}", 1_234_567_u32.to_formatted_string(locale.as_ref()));
}
1,234,567

@Finomnis Finomnis changed the title impl Format for Box<dyn Format> Add support for Box<dyn Format> Jul 24, 2025
@Finomnis Finomnis changed the title Add support for Box<dyn Format> Add support for Box<dyn Format> Jul 24, 2025
@Finomnis
Copy link
Author

@bcmyers This still maintained?

@Finomnis
Copy link
Author

Already contained in #47, which also fixes other problems.

@Finomnis
Copy link
Author

Actually, not completely contained in #47, so I'll keep this open

@Finomnis Finomnis reopened this Jul 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants