8000 Merge pull request #1664 from metaphorshear/metaphorshear/extend-socket · RustPython/RustPython@e4636b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4636b4

Browse files
authored
Merge pull request #1664 from metaphorshear/metaphorshear/extend-socket
Add htons, ntohl, and ntohs to socket.rs
2 parents a3293a7 + a72109c commit e4636b4

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

vm/src/stdlib/socket.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +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::int::PrimInt>(host: U, _vm: &VirtualMachine) -> U {
449+
U::to_be(host)
450+
}
451+
452+
fn socket_ntoh<U: num_traits::int::PrimInt>(network: U, _vm: &VirtualMachine) -> U {
453+
U::from_be(network)
450454
}
451455

452456
#[derive(FromArgs)]
@@ -635,7 +639,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
635639
"inet_aton" => ctx.new_rustfunc(socket_inet_aton),
636640
"inet_ntoa" => ctx.new_rustfunc(socket_inet_ntoa),
637641
"gethostname" => ctx.new_rustfunc(socket_gethostname),
638-
"htonl" => ctx.new_rustfunc(socket_htonl),
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>),
639646
"getdefaulttimeout" => ctx.new_rustfunc(|vm: &VirtualMachine| vm.get_none()),
640647
"getaddrinfo" => ctx.new_rustfunc(socket_getaddrinfo),
641648
"gethostbyaddr" => ctx.new_rustfunc(socket_gethostbyaddr),

0 commit comments

Comments
 (0)
0