Skip to content

std: Refactor env::var function#151690

Open
xtqqczze wants to merge 1 commit intorust-lang:mainfrom
xtqqczze:env-var
Open

std: Refactor env::var function#151690
xtqqczze wants to merge 1 commit intorust-lang:mainfrom
xtqqczze:env-var

Conversation

@xtqqczze
Copy link
Copy Markdown
Contributor

@xtqqczze xtqqczze commented Jan 26, 2026

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jan 26, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@xtqqczze xtqqczze changed the title std: Refactor _var function to use env_imp::getenv std: Refactor env::_var function Jan 26, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread library/std/src/env.rs Outdated
Some(s) => s.into_string().map_err(VarError::NotUnicode),
None => Err(VarError::NotPresent),
}
env_imp::getenv(key).ok_or(VarError::NotPresent)?.into_string().map_err(VarError::NotUnicode)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure why this refactoring is necessary. It looks like var_os(key) wraps around env_imp::getenv(key) and does what you're doing (or what you want to do) with match cases:

/// Doc comments omitted
#[stable(feature = "env", since = "1.0.0")]
pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
    _var(key.as_ref())
}

fn _var(key: &OsStr) -> Result<String, VarError> {
    match var_os(key) {
        Some(s) => s.into_string().map_err(VarError::NotUnicode),
        None => Err(VarError::NotPresent),
    }
}

/// Doc comments omitted
#[must_use]
#[stable(feature = "env", since = "1.0.0")]
pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
    _var_os(key.as_ref())
}

fn _var_os(key: &OsStr) -> Option<OsString> {
    env_imp::getenv(key)
}

Although, I'll admit, I'm curious on about the separate _var_os() and _var() functions here; I'm not noticing anywhere else _var_os() and _var() is used, so it makes me wonder why not just put what's inside _var_os()/_var() inside var_os()/var()?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The split exists to minimize the size of the generic versions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh got it, that makes sense to me!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure why this refactoring is necessary.

Primarily to avoid: var -> _var -> -> var_os -> _var_os -> env_imp::getenv

When we can do: var -> _var -> env_imp::getenv

Replacing match with combinators because I thought it read a bit nicer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I can definitely see the layers of function indirection here; I'm wondering if the compiler would optimize this away though and inline it?

Still, I'm with @hkBst on this part:

not a fan of using env_imp::getenv directly here, when this is clearly function that does a small modification to the output of var_os

Comment thread library/std/src/env.rs Outdated
Some(s) => s.into_string().map_err(VarError::NotUnicode),
None => Err(VarError::NotPresent),
}
env_imp::getenv(key).ok_or(VarError::NotPresent)?.into_string().map_err(VarError::NotUnicode)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of using env_imp::getenv directly here, when this is clearly function that does a small modification to the output of var_os. I suppose _var_os might be okay, but only if there was a benefit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why env_imp::getenv is an issue, it’s one less indirection, avoids unnecessary generic var_os calls, and _var_os seems like it should be an inner function to var_os.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why env_imp::getenv is an issue, it’s one less indirection,

env_imp::getenv seems like an implementation detail, that's wrapped by (_)var_os, so we should probably prefer to use the wrapper when possible.

avoids unnecessary generic var_os calls,

I don't think there is any cost to this, since the compiler can easily inline these simple calls.

and _var_os seems like it should be an inner function to var_os.

Yes, that may well make sense, but is not what you implemented.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I've reimplemented var to use an inner function (to minimize the size of the generic versions) and removed the pointless _var_os function.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've not addressed: "env_imp::getenv seems like an implementation detail, that's wrapped by (_)var_os, so we should probably prefer to use the wrapper when possible."

@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Jan 29, 2026

Looks like @hkBst is on top of reviewing things already.

r? hkBst

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jan 29, 2026

Failed to set assignee to hkBst: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Jan 29, 2026

Well, let me know when you think it's good to go and I can approve it on your behalf 🙂

@hkBst
Copy link
Copy Markdown
Member

hkBst commented Jan 29, 2026

Well, let me know when you think it's good to go and I can approve it on your behalf 🙂

Sure thing!

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 29, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jan 29, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 29, 2026
@SpriteOvO
Copy link
Copy Markdown
Member

@xtqqczze Hi, ping from triage team. This PR has been inactive for a while. There are still some review comments that need to be addressed. Would you like to proceed with this PR? Thanks.

@xtqqczze
Copy link
Copy Markdown
Contributor Author

xtqqczze commented May 3, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 3, 2026
@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented May 4, 2026

Currently failing CI

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 4, 2026
Comment thread library/std/src/env.rs Outdated
Some(s) => s.into_string().map_err(VarError::NotUnicode),
None => Err(VarError::NotPresent),
}
env_imp::getenv(key).ok_or(VarError::NotPresent)?.into_string().map_err(VarError::NotUnicode)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why env_imp::getenv is an issue, it’s one less indirection,

env_imp::getenv seems like an implementation detail, that's wrapped by (_)var_os, so we should probably prefer to use the wrapper when possible.

avoids unnecessary generic var_os calls,

I don't think there is any cost to this, since the compiler can easily inline these simple calls.

and _var_os seems like it should be an inner function to var_os.

Yes, that may well make sense, but is not what you implemented.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 4, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@xtqqczze
Copy link
Copy Markdown
Contributor Author

xtqqczze commented May 4, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 4, 2026
@xtqqczze xtqqczze changed the title std: Refactor env::_var function std: Refactor env::var function May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants