@@ -31,6 +31,32 @@ use crate::sys::socket::addr::sys_control::SysControlAddr;
31
31
pub use self :: datalink:: LinkAddr ;
32
32
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
33
33
pub use self :: vsock:: VsockAddr ;
34
+ #[ cfg( feature = "net" ) ]
35
+ use static_assertions:: const_assert_eq;
36
+
37
+ /// Convert a std::net::Ipv4Addr into the libc form.
38
+ // In the standard library, this is a simple newtype, but it doesn't expose
39
+ // the inner libc type, so we must use a pointer cast.
40
+ #[ cfg( feature = "net" ) ]
41
+ pub ( crate ) const fn ipv4addr_to_libc ( addr : net:: Ipv4Addr ) -> libc:: in_addr {
42
+ const_assert_eq ! ( mem:: size_of:: <libc:: in_addr>( ) ,
43
+ mem:: size_of:: <net:: Ipv4Addr >( ) ) ;
44
+ unsafe {
45
+ * ( & addr as * const net:: Ipv4Addr as * const libc:: in_addr )
46
+ }
47
+ }
48
+
49
+ /// Convert a std::net::Ipv6Addr into the libc form.
50
+ // In the standard library, this is a simple newtype, but it doesn't expose
51
+ // the inner libc type, so we must use a pointer cast.
52
+ #[ cfg( feature = "net" ) ]
53
+ pub ( crate ) const fn ipv6addr_to_libc ( addr : & net:: Ipv6Addr ) -> & libc:: in6_addr {
54
+ const_assert_eq ! ( mem:: size_of:: <libc:: in6_addr>( ) ,
55
+ mem:: size_of:: <net:: Ipv6Addr >( ) ) ;
56
+ unsafe {
57
+ & * ( addr as * const net:: Ipv6Addr as * const libc:: in6_addr )
58
+ }
59
+ }
34
60
35
61
/// These constants specify the protocol family to be used
36
62
/// in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
@@ -497,14 +523,20 @@ impl fmt::Display for InetAddr {
497
523
* ===== IpAddr =====
498
524
*
499
525
*/
500
- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
526
+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
527
+ #[ allow( deprecated) ]
501
528
#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
529
+ #[ deprecated(
530
+ since = "0.24.0" ,
531
+ note = "Use std::net::IpAddr instead"
532
+ ) ]
502
533
pub enum IpAddr {
503
534
V4 ( Ipv4Addr ) ,
504
535
V6 ( Ipv6Addr ) ,
505
536
}
506
537
507
- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
538
+ #[ allow( deprecated) ]
539
+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
508
540
impl IpAddr {
509
541
/// Create a new IpAddr that contains an IPv4 address.
510
542
///
@@ -537,6 +569,7 @@ impl IpAddr {
537
569
}
538
570
}
539
571
572
+ #[ allow( deprecated) ]
540
573
impl fmt:: Display for IpAddr {
541
574
fn fmt( & self , f: & mut fmt:: Formatter ) -> fmt:: Result {
542
575
match * self {
@@ -552,12 +585,17 @@ impl fmt::Display for IpAddr {
552
585
*
553
586
*/
554
587
555
- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
588
+ #[ deprecated(
589
+ since = "0.24.0" ,
590
+ note = "Use std::net::Ipv4Addr instead"
591
+ ) ]
592
+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
556
593
#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
557
594
#[ repr( transparent) ]
558
595
pub struct Ipv4Addr ( pub libc:: in_addr) ;
559
596
560
- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
597
+ #[ allow( deprecated) ]
598
+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
561
599
impl Ipv4Addr {
562
600
#[ allow( clippy:: identity_op) ] // More readable this way
563
601
pub const fn new( a: u8 , b: u8 , c: u8 , d: u8 ) -> Ipv4Addr {
@@ -591,6 +629,7 @@ impl Ipv4Addr {
591
629
}
592
630
}
593
631
632
+ #[ allow( deprecated) ]
594
633
impl fmt:: Display for Ipv4Addr {
595
634
fn fmt( & self , fmt: & mut fmt:: Formatter ) -> fmt:: Result {
596
635
let octets = self . octets( ) ;
@@ -604,7 +643,11 @@ impl fmt::Display for Ipv4Addr {
604
643
*
605
644
*/
606
645
607
- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
646
+ #[ deprecated(
647
+ since = "0.24.0" ,
648
+ note = "Use std::net::Ipv6Addr instead"
649
+ ) ]
650
+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
608
651
#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
609
652
#[ repr( transparent) ]
610
653
pub struct Ipv6Addr ( pub libc:: in6_addr) ;
@@ -625,7 +668,8 @@ macro_rules! to_u16_array {
625
668
}
626
669
}
627
670
628
- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
671
+ #[ allow( deprecated) ]
672
+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
629
673
impl Ipv6Addr {
630
674
#[ allow( clippy:: many_single_char_names) ]
631
675
#[ allow( clippy:: too_many_arguments) ]
@@ -649,6 +693,7 @@ impl Ipv6Addr {
649
693
}
650
694
}
651
695
696
+ #[ allow( deprecated) ]
652
697
impl fmt:: Display for Ipv6Addr {
653
698
fn fmt( & self , fmt: & mut fmt:: Formatter ) -> fmt:: Result {
654
699
self . to_std( ) . fmt( fmt)
@@ -1172,7 +1217,7 @@ impl From<net::SocketAddrV4> for SockaddrIn {
1172
1217
sin_len : mem:: size_of :: < libc:: sockaddr_in > ( ) as u8 ,
1173
1218
sin_family : AddressFamily :: Inet as sa_family_t ,
1174
1219
sin_port : addr. port ( ) . to_be ( ) , // network byte order
1175
- sin_addr : Ipv4Addr :: from_std ( addr. ip ( ) ) . 0 ,
1220
+ sin_addr : ipv4addr_to_libc ( * addr. ip ( ) ) ,
1176
1221
.. unsafe { mem:: zeroed ( ) }
1177
1222
} )
1178
1223
}
@@ -1266,7 +1311,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
1266
1311
sin6_len : mem:: size_of :: < libc:: sockaddr_in6 > ( ) as u8 ,
1267
1312
sin6_family : AddressFamily :: Inet6 as sa_family_t ,
1268
1313
sin6_port : addr. port ( ) . to_be ( ) , // network byte order
1269
- sin6_addr : Ipv6Addr :: from_std ( addr. ip ( ) ) . 0 ,
1314
+ sin6_addr : * ipv6addr_to_libc ( addr. ip ( ) ) ,
1270
1315
sin6_flowinfo : addr. flowinfo ( ) , // host byte order
1271
1316
sin6_scope_id : addr. scope_id ( ) , // host byte order
1272
1317
.. unsafe { mem:: zeroed ( ) }
@@ -2545,6 +2590,24 @@ pub mod vsock {
2545
2590
mod tests {
2546
2591
use super :: * ;
2547
2592
2593
+ mod types {
2594
+ use super :: * ;
2595
+
2596
+ #[ test]
2597
+ fn test_ipv4addr_to_libc ( ) {
2598
+ let s = std:: net:: Ipv4Addr :: new ( 1 , 2 , 3 , 4 ) ;
2599
+ let l = ipv4addr_to_libc ( s) ;
2600
+ assert_eq ! ( l. s_addr, u32 :: to_be( 0x01020304 ) ) ;
2601
+ }
2602
+
2603
+ #[ test]
2604
+ fn test_ipv6addr_to_libc ( ) {
2605
+ let s = std:: net:: Ipv6Addr :: new ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ;
2606
+ let l = ipv6addr_to_libc ( & s) ;
2607
+ assert_eq ! ( l. s6_addr, [ 0 , 1 , 0 , 2 , 0 , 3 , 0 , 4 , 0 , 5 , 0 , 6 , 0 , 7 , 0 , 8 ] ) ;
2608
+ }
2609
+ }
2610
+
2548
2611
mod link {
2549
2612
#[ cfg( any( target_os = "ios" ,
2550
2613
target_os = "macos" ,
0 commit comments