@@ -445,30 +445,12 @@ fn socket_inet_ntoa(packed_ip: PyBytesRef, vm: &VirtualMachine) -> PyResult {
445
445
Ok ( vm. new_str ( Ipv4Addr :: from ( ip_num) . to_string ( ) ) )
446
446
}
447
447
448
- fn socket_htonl ( host : u32 , vm : & VirtualMachine ) -> PyResult {
449
- Ok ( vm . new_int ( host. to_be ( ) ) )
448
+ fn socket_hton < U : num_traits :: PrimInt > ( host : U , _vm : & VirtualMachine ) -> U {
449
+ host. to_be ( )
450
450
}
451
451
452
- fn socket_htons ( host : u16 , vm : & VirtualMachine ) -> PyResult {
453
- Ok ( vm. new_int ( host. to_be ( ) ) )
454
- }
455
-
456
- fn socket_ntohl ( network : u32 , vm : & VirtualMachine ) -> PyResult {
457
- if cfg ! ( target_endian = "big" ) {
458
- Ok ( vm. new_int ( network) )
459
- }
460
- else {
461
- Ok ( vm. new_int ( network. to_le ( ) ) )
462
- }
463
- }
464
-
465
- fn socket_ntohs ( network : u16 , vm : & VirtualMachine ) -> PyResult {
466
- if cfg ! ( target_endian = "big" ) {
467
- Ok ( vm. new_int ( network) )
468
- }
469
- else {
470
- Ok ( vm. new_int ( network. to_le ( ) ) )
471
- }
452
+ fn socket_ntoh < U : num_traits:: PrimInt > ( network : U , _vm : & VirtualMachine ) -> U {
453
+ network. from_be ( )
472
454
}
473
455
474
456
#[ derive( FromArgs ) ]
@@ -657,10 +639,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
657
639
"inet_aton" => ctx. new_rustfunc( socket_inet_aton) ,
658
640
"inet_ntoa" => ctx. new_rustfunc( socket_inet_ntoa) ,
659
641
"gethostname" => ctx. new_rustfunc( socket_gethostname) ,
660
- "htonl" => ctx. new_rustfunc( socket_htonl ) ,
661
- "htons" => ctx. new_rustfunc( socket_htons ) ,
662
- "ntohl" => ctx. new_rustfunc( socket_ntohl ) ,
663
- "ntohs" => ctx. new_rustfunc( socket_ntohs ) ,
642
+ "htonl" => ctx. new_rustfunc( socket_hton :: < u32 > ) ,
643
+ "htons" => ctx. new_rustfunc( socket_hton :: < u16 > ) ,
644
+ "ntohl" => ctx. new_rustfunc( socket_ntoh :: < u32 > ) ,
645
+ "ntohs" => ctx. new_rustfunc( socket_ntoh :: < u16 > ) ,
664
646
"getdefaulttimeout" => ctx. new_rustfunc( |vm: & VirtualMachine | vm. get_none( ) ) ,
665
647
"getaddrinfo" => ctx. new_rustfunc( socket_getaddrinfo) ,
666
648
"gethostbyaddr" => ctx. new_rustfunc( socket_gethostbyaddr) ,
0 commit comments