Skip to content

Add htons, ntohl, and ntohs to socket.rs#1664

Merged
coolreader18 merged 4 commits intoRustPython:masterfrom
metaphorshear:metaphorshear/extend-socket
Jan 8, 2020
Merged

Add htons, ntohl, and ntohs to socket.rs#1664
coolreader18 merged 4 commits intoRustPython:masterfrom
metaphorshear:metaphorshear/extend-socket

Conversation

@metaphorshear
Copy link
Contributor

For #1176. Just added each of those functions to vm/src/stdlib/socket.rs, mostly by cheating off of the htonl implementation.

@metaphorshear metaphorshear force-pushed the metaphorshear/extend-socket branch from bc6ae61 to 2912d79 Compare January 7, 2020 10:12
Ok(vm.new_int(host.to_be()))
}

fn socket_htons(host: u16, vm: &VirtualMachine) -> PyResult {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the only difference is type, probably generic function will work

fn socket_hton<U>(host: U, _vm: &VirtualMachine) -> U {
    host.to_be()
}

and make rustfunc like this:

"htons" => ctx.new_rustfunc(socket_hton::<u16>),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'd have to give the trait bound U: num_traits::PrimInt for that to work.

fn socket_ntohs(network: u16, vm: &VirtualMachine) -> PyResult {
if cfg!(target_endian = "big") {
Ok(vm.new_int(network))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem like no-ops -- if the machine is big endian, leave it as is, or else if it's little endian, convert to little endian, i.e. leave it as is. You might be looking for u{16,32}::from_{le,be}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Yeah, I couldn't find those before.

Ok(vm.new_int(host.to_be()))
}

fn socket_htons(host: u16, vm: &VirtualMachine) -> PyResult {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have all these functions just return u16 or u32 rather than a PyResult -- ctx.new_rustfunc takes care of converting the return type.

… ntoh with generics, simplified their return values, replaced a chunk of code with from_be.)
@metaphorshear
Copy link
Contributor Author

One sec, I might have pushed that a bit early.

@metaphorshear
Copy link
Contributor Author

Let me know if you want those rebased.

@coolreader18
Copy link
Member

I think it's fine; thanks for contributing! I'll merge this once the ci finishes.

@coolreader18 coolreader18 dismissed youknowone’s stale review January 7, 2020 22:11

They made it generic

@coolreader18
Copy link
Member

It looks like the rustfmt job failed @metaphorshear, could you run cargo fmt and push again?

@coolreader18 coolreader18 merged commit e4636b4 into RustPython:master Jan 8, 2020
@metaphorshear metaphorshear deleted the metaphorshear/extend-socket branch January 9, 2020 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants