Description
Proposal
Problem statement
Implement From<&'a &'static str>
for Arguments<'a>
.
Motivating examples or use cases
The log!
macro from the log
crate once had an optimization for calls with a string literal, but later the optimization was removed to support capturing variables in log messages (see rust-lang/log#446).
The idea is that constructing an Arguments
object is more costly than constructing an &str
object, so in order to reduce the binary size, the log!
macro could dispatch the call to two different functions based on whether the message is a string literal:
fn __private_api_log_1(args: &'static str, ...) { ... }
fn __private_api_log_2(args: Arguments, ...) { ... }
macro_rules! log {
// If the argument is a string literal,
// call `__private_api_log_1`,
// otherwise call `__private_api_log_2`.
}
I intend to bring that optimization back, the problem is that although we can construct an Arguments
object from an &str
object using format_args!("{s}")
, the result Arguments
object won’t has its as_str
method returning Some(&'static str)
, which essentially prevents logging backend from utilizing as_str
.
Solution sketch
The standard library provides a way to construct an Arguments
object that returns Some(&'static str)
if called on its as_str()
method from an &'static str
object. I think implementing From<&'a &'static str>
for Arguments<'a>
seems to be a reasonable thing to do this.
Alternatives
The only thing I think of is utilizing private API Arguments::new_const
, but it’s not a good idea.
Links and related work
Implementation PR: rust-lang/rust#114531
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.