Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

inet_address: add new_from_bytes constructor #201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/inet_address.rs
@@ -0,0 +1,40 @@
use SocketFamily;
use ffi;
use glib::translate::*;
use InetAddress;

#[derive(Debug)]
pub enum InetAddressBytes<'a> {
V4(&'a [u8; 4]),
V6(&'a [u8; 16]),
}

impl<'a> InetAddressBytes<'a> {
fn deref(&self) -> &[u8] {
use self::InetAddressBytes::*;

match *self {
V4(bytes) => bytes,
V6(bytes) => bytes,
}
}
}

impl InetAddress {
pub fn new_from_bytes(inet_address_bytes: InetAddressBytes) -> Self {
use self::InetAddressBytes::*;

let bytes = inet_address_bytes.deref();

let family = match inet_address_bytes {
V4(_) => SocketFamily::Ipv4,
V6(_) => SocketFamily::Ipv6,
};
unsafe {
from_glib_full(ffi::g_inet_address_new_from_bytes(
bytes.to_glib_none().0,
family.to_glib(),
))
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -49,6 +49,8 @@ mod subprocess;
mod subprocess_launcher;
#[cfg(any(unix, feature = "dox"))]
mod unix_socket_address;
mod inet_address;
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need a pub use inet_address::InetAddressOctet here then?

Copy link
Author

Choose a reason for hiding this comment

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

Ah, let me fix.

pub use inet_address::InetAddressBytes;

#[cfg(test)]
mod test_util;
Expand Down