metrics-rs / metrics

A metrics ecosystem for Rust.
MIT License
1.13k stars 157 forks source link

Expand lifetime of KeyName as_str #390

Closed miguelff closed 1 year ago

miguelff commented 1 year ago

A SharedString is valid during the duration of the program, once built. This PR extends the lifetime of the string slice returned by as_str to avoid needing to clone the key name.

tobz commented 1 year ago

Unfortunately, SharedString does not work this way.

SharedString represents a copy-on-write pointer that may be based on a &'static str, but also may be based on a String. It would be invalid to provide a static reference to String if SharedString itself was also not 'static.

As the code currently exists, if you could construct KeyName statically, then you could use the returned reference from SharedString (i.e. KeyName::as_str) statically as well.

miguelff commented 1 year ago

Ohhhh! right, thanks for explaining. I completely misunderstood the code. Sorry for the noise.

tobz commented 1 year ago

No worries at all. 👍🏻