Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust htons and ntohs

Tags:

rust

I have a program that has been relying on native::io::net::{htons, ntohs} but now errors on Could not find 'io' in 'packet::native'. The change seems to have happened some time in the last week

Searching doesn't yield much info about the change and a search for htons or ntohs in the docs doesn't yield any useful information.

What is the (new?) standard way to perform htons or ntohs in Rust?

An obvious solution would be to write my own but one would expect that it would be in the standard library.

like image 276
arcyqwerty Avatar asked Nov 19 '14 23:11

arcyqwerty


1 Answers

These are now in std::sys_common::net, but std::sys_common is private. But their implementation in src/libstd/sys/common/net.rs is very simple:

pub fn htons(u: u16) -> u16 {
    u.to_be()
}
pub fn ntohs(u: u16) -> u16 {
    Int::from_be(u)
}
like image 200
Chris Morgan Avatar answered Oct 19 '22 09:10

Chris Morgan