Skip to content

Commit

Permalink
Add support for Arc<T> to metrics::Cow<'a, T>.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz committed Oct 21, 2023
1 parent 3fca508 commit a78efbb
Show file tree
Hide file tree
Showing 6 changed files with 534 additions and 325 deletions.
2 changes: 1 addition & 1 deletion metrics-tracing-context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tracing-core = { version = "0.1.21", default-features = false }
tracing-subscriber = { version = "0.3.1", default-features = false, features = ["std"] }

[dev-dependencies]
criterion = { version = "=0.3.3", default-features = false }
criterion = "0.3"
parking_lot = { version = "0.12.1", default-features = false }
tracing = { version = "0.1.29", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.3.1", default-features = false, features = ["registry"] }
9 changes: 9 additions & 0 deletions metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate

### Added

- Support for using `Arc<T>` with `Cow<'a, T>`.
([#402](https://github.com/metrics-rs/metrics/pull/402))

This will primarily allow using `Arc<str>` for metric names and labels, where previously only
`&'static str` or `String` were allowed. There's still work to be done to also support labels in
this regard.

## [0.21.1] - 2023-07-02

### Added
Expand Down
13 changes: 8 additions & 5 deletions metrics/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use crate::cow::Cow;

/// An allocation-optimized string.
///
/// We specify `SharedString` to attempt to get the best of both worlds: flexibility to provide a
/// static or dynamic (owned) string, while retaining the performance benefits of being able to
/// take ownership of owned strings and borrows of completely static strings.
/// `SharedString` uses a custom copy-on-write implementation that is optimized for metric keys,
/// providing ergonomic sharing of single instances, or slices, of strings and labels. This
/// copy-on-write implementation is optimized to allow for constant-time construction (using static
/// values), as well as accepting owned values and values shared through [`Arc<T>`](std::sync::Arc).
///
/// `SharedString` can be converted to from either `&'static str` or `String`, with a method,
/// `const_str`, from constructing `SharedString` from `&'static str` in a `const` fashion.
/// End users generally will not need to interact with this type directly, as the top-level macros
/// (`counter!`, etc), as well as the various conversion implementations
/// ([`From<T>`](std::convert::From)), generally allow users to pass whichever variant of a value
/// (static, owned, shared) is best for them.
pub type SharedString = Cow<'static, str>;

/// Key-specific hashing algorithm.
Expand Down
Loading

0 comments on commit a78efbb

Please sign in to comment.