@@ -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+
376405impl < ' 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 {
11761206pub ( 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 ) ) ;
0 commit comments