10000 Made requested changes. (Combined short and long versions of hton and… · RustPython/RustPython@93e2eec · GitHub
[go: up one dir, main page]

Skip to content

Commit 93e2eec

Browse files
committed
Made requested changes. (Combined short and long versions of hton and ntoh with generics, simplified their return values, replaced a chunk of code with from_be.)
1 parent 2912d79 commit 93e2eec

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

vm/src/stdlib/socket.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -445,30 +445,12 @@ fn socket_inet_ntoa(packed_ip: PyBytesRef, vm: &VirtualMachine) -> PyResult {
445445
Ok(vm.new_str(Ipv4Addr::from(ip_num).to_string()))
446446
}
447447

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()
450450
}
451451

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()
472454
}
473455

474456
#[derive(FromArgs)]
@@ -657,10 +639,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
657639
"inet_aton" => ctx.new_rustfunc(socket_inet_aton),
658640
"inet_ntoa" => ctx.new_rustfunc(socket_inet_ntoa),
659641
"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>),
664646
"getdefaulttimeout" => ctx.new_rustfunc(|vm: &VirtualMachine| vm.get_none()),
665647
"getaddrinfo" => ctx.new_rustfunc(socket_getaddrinfo),
666648
"gethostbyaddr" => ctx.new_rustfunc(socket_gethostbyaddr),

0 commit comments

Comments
 (0)
0