Skip to content

Allow constructing an empty GlobSet in const #3098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dtolnay
Copy link
Contributor

@dtolnay dtolnay commented Jul 8, 2025

My use case: I have a struct that contains a GlobSet, among other information:

pub struct Thing {
    pub extra_srcs: GlobSet,
    pub flags: Vec<String>,
    ...
}

and some instances of this struct held in a different data structure — imagine this is akin to Cargo.toml's [dependencies] and [target.'cfg(…)'.dependencies]:

pub struct ConfigFile {
    common: Option<Thing>,
    platform_specific: BTreeMap<Platform, Thing>,
}

and a function where it would be really convenient to be able to use a const to get a sufficiently long-lived reference to a static Thing containing an empty globset:

impl ConfigFile {
    pub fn things(&self) -> impl Iterator<Item = &Thing> {
        self.common
            .iter()
            .chain(self.platform_specific.values())
            .chain([
                const {
                    &Thing {
                        extra_srcs: GlobSet::empty(),  // <---
                        flags: Vec::new(),
                        ...
                    }
                },
                ...
            ])
    }
}

Without being able to call GlobSet::empty() in const, I would need to do one of the following workarounds instead:

  • Store Option<GlobSet> instead of just GlobSet, where None means the same thing as GlobSet::empty().

  • Find a place to put my constant Thing at runtime, either inside ConfigFile (awkward) or some other storage that is passed in to things (awkward).

  • Switch to Cow<'_, Thing> and sprinkle the implementation of things with Cow::Owned and map(Cow::Borrowed).

  • Clone everything every time we iterate this data structure.

  • Something with a static OnceLock.

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.

1 participant