Skip to content

Commit 885dd9d

Browse files
committed
Simplify Get APIs
1 parent 0303a38 commit 885dd9d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

opentelemetry/src/baggage.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ impl Baggage {
7575
///
7676
/// assert_eq!(cc.get("my-name"), Some(&Value::from("my-value")))
7777
/// ```
78-
pub fn get<T: Into<Key>>(&self, key: T) -> Option<&Value> {
79-
self.inner.get(&key.into()).map(|(value, _metadata)| value)
78+
pub fn get<K: AsRef<str>>(&self, key: K) -> Option<&Value> {
79+
self.inner.get(key.as_ref()).map(|(value, _metadata)| value)
8080
}
8181

8282
/// Returns a reference to the value and metadata associated with a given name
@@ -91,8 +91,8 @@ impl Baggage {
9191
/// // By default, the metadata is empty
9292
/// assert_eq!(cc.get_with_metadata("my-name"), Some(&(Value::from("my-value"), BaggageMetadata::from(""))))
9393
/// ```
94-
pub fn get_with_metadata<T: Into<Key>>(&self, key: T) -> Option<&(Value, BaggageMetadata)> {
95-
self.inner.get(&key.into())
94+
pub fn get_with_metadata<K: AsRef<str>>(&self, key: K) -> Option<&(Value, BaggageMetadata)> {
95+
self.inner.get(key.as_ref())
9696
}
9797

9898
/// Inserts a name/value pair into the baggage.

opentelemetry/src/common.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::borrow::Cow;
1+
use std::borrow::{Borrow, Cow};
22
use std::sync::Arc;
33
use std::{fmt, hash};
44

@@ -135,6 +135,18 @@ impl fmt::Display for Key {
135135
}
136136
}
137137

138+
impl Borrow<str> for Key {
139+
fn borrow(&self) -> &str {
140+
self.0.as_str()
141+
}
142+
}
143+
144+
impl AsRef<str> for Key {
145+
fn as_ref(&self) -> &str {
146+
self.0.as_str()
147+
}
148+
}
149+
138150
#[derive(Clone, Debug, Eq)]
139151
enum OtelString {
140152
Owned(Box<str>),

0 commit comments

Comments
 (0)