WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

PickList: Use a closure instead of the toString trait #922

@itytophile

Description

@itytophile

Hi, in my iced app I use a localization system to translate options inside pick lists. However, Picklist only relies on the toString trait to display its options. So for the translation to happen, my Display trait implementation must check a global variable at each call:

impl Display for Armor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.apply_locale(&*crate::LOCALE.lock().unwrap()))
    }
}

LOCALE is the global variable (I use once_cell). In my opinion, this is not very pretty to check a global variable inside an implementation of such a trait.

There is another limitation, I can't implement the Display trait for a Struct from an external crate. What I would like is to be able to give a closure to the PickList struct to handle converting options into String (maybe Fn(T) -> String or another trait like PickListDisplay?).

Is this possible? I could try implementing it if you think this is. Thank you for maintaining iced by the way.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions