Skip to content

Commit 7ccd873

Browse files
authored
Merge pull request #724 from rust-lang/feat/net-to-value
Support capturing of `std::net` types
2 parents 67bc7e3 + 923dfaa commit 7ccd873

2 files changed

Lines changed: 124 additions & 1 deletion

File tree

src/kv/value.rs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,35 @@ impl_value_to_primitive![
373373
to_bool -> bool,
374374
];
375375

376+
#[cfg(feature = "std")]
377+
macro_rules! impl_to_value_from_display {
378+
($($into_ty:ty,)*) => {
379+
$(
380+
impl ToValue for $into_ty {
381+
fn to_value(&self) -> Value<'_> {
382+
Value::from_display(self)
383+
}
384+
}
385+
386+
impl<'v> From<&'v $into_ty> for Value<'v> {
387+
fn from(value: &'v $into_ty) -> Self {
388+
Value::from_display(value)
389+
}
390+
}
391+
)*
392+
};
393+
}
394+
395+
#[cfg(feature = "std")]
396+
impl_to_value_from_display![
397+
std::net::IpAddr,
398+
std::net::Ipv4Addr,
399+
std::net::Ipv6Addr,
400+
std::net::SocketAddr,
401+
std::net::SocketAddrV4,
402+
std::net::SocketAddrV6,
403+
];
404+
376405
impl<'v> Value<'v> {
377406
/// Try to convert this value into an error.
378407
#[cfg(feature = "kv_std")]
@@ -736,6 +765,7 @@ pub(in crate::kv) mod inner {
736765

737766
#[derive(Clone)]
738767
pub enum Inner<'v> {
768+
// NOTE: New variants can't be added here; see the module-level doc above
739769
None,
740770
Bool(bool),
741771
Str(&'v str),
@@ -999,7 +1029,7 @@ pub(in crate::kv) mod inner {
9991029
}
10001030

10011031
#[cfg(test)]
1002-
pub fn to_test_token(&self) -> Token {
1032+
pub fn to_test_token(&self) -> Token<'_> {
10031033
match self {
10041034
Inner::None => Token::None,
10051035
Inner::Bool(v) => Token::Bool(*v),
@@ -1176,7 +1206,10 @@ macro_rules! as_sval {
11761206
pub(crate) mod tests {
11771207
use super::*;
11781208

1209+
// For new `ToValue` implementations, also add a test to the `tests/macros` file
1210+
11791211
impl<'v> Value<'v> {
1212+
#[allow(mismatched_lifetime_syntaxes)]
11801213
pub(crate) fn to_token(&self) -> inner::Token {
11811214
self.inner.to_test_token()
11821215
}
@@ -1243,6 +1276,75 @@ pub(crate) mod tests {
12431276
assert_eq!(None::<bool>.to_value().to_string(), "None");
12441277
}
12451278

1279+
#[test]
1280+
#[cfg(feature = "std")]
1281+
fn test_net_to_value_display() {
1282+
use std::str::FromStr;
1283+
1284+
assert_eq!(
1285+
std::net::Ipv4Addr::new(192, 168, 10, 100)
1286+
.to_value()
1287+
.to_string(),
1288+
"192.168.10.100"
1289+
);
1290+
assert_eq!(
1291+
std::net::Ipv6Addr::from_str("f33c::1")
1292+
.unwrap()
1293+
.to_value()
1294+
.to_string(),
1295+
"f33c::1"
1296+
);
1297+
assert_eq!(
1298+
std::net::IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 10, 100))
1299+
.to_value()
1300+
.to_string(),
1301+
"192.168.10.100"
1302+
);
1303+
assert_eq!(
1304+
std::net::IpAddr::V6(std::net::Ipv6Addr::from_str("f33c::1").unwrap())
1305+
.to_value()
1306+
.to_string(),
1307+
"f33c::1"
1308+
);
1309+
assert_eq!(
1310+
std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(192, 168, 10, 100), 12345)
1311+
.to_value()
1312+
.to_string(),
1313+
"192.168.10.100:12345"
1314+
);
1315+
assert_eq!(
1316+
std::net::SocketAddrV6::new(
1317+
std::net::Ipv6Addr::from_str("f33c::1").unwrap(),
1318+
12345,
1319+
0,
1320+
0
1321+
)
1322+
.to_value()
1323+
.to_string(),
1324+
"[f33c::1]:12345"
1325+
);
1326+
assert_eq!(
1327+
std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
1328+
std::net::Ipv4Addr::new(192, 168, 10, 100),
1329+
12345
1330+
))
1331+
.to_value()
1332+
.to_string(),
1333+
"192.168.10.100:12345"
1334+
);
1335+
assert_eq!(
1336+
std::net::SocketAddr::V6(std::net::SocketAddrV6::new(
1337+
std::net::Ipv6Addr::from_str("f33c::1").unwrap(),
1338+
12345,
1339+
0,
1340+
0
1341+
))
1342+
.to_value()
1343+
.to_string(),
1344+
"[f33c::1]:12345"
1345+
);
1346+
}
1347+
12461348
#[test]
12471349
fn test_to_value_structured() {
12481350
assert_eq!(42u64.to_value().to_token(), inner::Token::U64(42));

tests/macros.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,27 @@ fn logger_expr() {
415415
}, "hello");
416416
}
417417

418+
#[cfg(all(feature = "std", feature = "kv"))]
419+
#[test]
420+
fn kv_net() {
421+
use std::{
422+
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
423+
str::FromStr,
424+
};
425+
426+
all_log_macros!(
427+
a = Ipv4Addr::new(192, 168, 10, 100),
428+
b = Ipv6Addr::from_str("f33c::1").unwrap(),
429+
c = IpAddr::V4(Ipv4Addr::new(192, 168, 10, 100)),
430+
d = IpAddr::V6(Ipv6Addr::from_str("f33c::1").unwrap()),
431+
e = SocketAddrV4::new(Ipv4Addr::new(192, 168, 10, 100), 12345),
432+
f = SocketAddrV6::new(Ipv6Addr::from_str("f33c::1").unwrap(), 12345, 0, 0),
433+
g = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 10, 100), 12345)),
434+
h = SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::from_str("f33c::1").unwrap(), 12345, 0, 0));
435+
"hello world"
436+
);
437+
}
438+
418439
/// Some and None (from Option) are used in the macros.
419440
#[derive(Debug)]
420441
enum Type {

0 commit comments

Comments
 (0)