gio/auto/
network_address.rs1use gio_sys;
6use glib::object::IsA;
7use glib::translate::*;
8use glib::GString;
9use std::fmt;
10use std::ptr;
11use Error;
12use SocketConnectable;
13
14glib_wrapper! {
15 pub struct NetworkAddress(Object<gio_sys::GNetworkAddress, gio_sys::GNetworkAddressClass, NetworkAddressClass>) @implements SocketConnectable;
16
17 match fn {
18 get_type => || gio_sys::g_network_address_get_type(),
19 }
20}
21
22impl NetworkAddress {
23 pub fn new(hostname: &str, port: u16) -> NetworkAddress {
24 unsafe {
25 from_glib_full(gio_sys::g_network_address_new(
26 hostname.to_glib_none().0,
27 port,
28 ))
29 }
30 }
31
32 #[cfg(any(feature = "v2_44", feature = "dox"))]
33 pub fn new_loopback(port: u16) -> NetworkAddress {
34 unsafe { from_glib_full(gio_sys::g_network_address_new_loopback(port)) }
35 }
36
37 pub fn parse(host_and_port: &str, default_port: u16) -> Result<NetworkAddress, Error> {
38 unsafe {
39 let mut error = ptr::null_mut();
40 let ret = gio_sys::g_network_address_parse(
41 host_and_port.to_glib_none().0,
42 default_port,
43 &mut error,
44 );
45 if error.is_null() {
46 Ok(from_glib_full(ret))
47 } else {
48 Err(from_glib_full(error))
49 }
50 }
51 }
52
53 pub fn parse_uri(uri: &str, default_port: u16) -> Result<NetworkAddress, Error> {
54 unsafe {
55 let mut error = ptr::null_mut();
56 let ret = gio_sys::g_network_address_parse_uri(
57 uri.to_glib_none().0,
58 default_port,
59 &mut error,
60 );
61 if error.is_null() {
62 Ok(from_glib_full(ret))
63 } else {
64 Err(from_glib_full(error))
65 }
66 }
67 }
68}
69
70unsafe impl Send for NetworkAddress {}
71unsafe impl Sync for NetworkAddress {}
72
73pub const NONE_NETWORK_ADDRESS: Option<&NetworkAddress> = None;
74
75pub trait NetworkAddressExt: 'static {
76 fn get_hostname(&self) -> Option<GString>;
77
78 fn get_port(&self) -> u16;
79
80 fn get_scheme(&self) -> Option<GString>;
81}
82
83impl<O: IsA<NetworkAddress>> NetworkAddressExt for O {
84 fn get_hostname(&self) -> Option<GString> {
85 unsafe {
86 from_glib_none(gio_sys::g_network_address_get_hostname(
87 self.as_ref().to_glib_none().0,
88 ))
89 }
90 }
91
92 fn get_port(&self) -> u16 {
93 unsafe { gio_sys::g_network_address_get_port(self.as_ref().to_glib_none().0) }
94 }
95
96 fn get_scheme(&self) -> Option<GString> {
97 unsafe {
98 from_glib_none(gio_sys::g_network_address_get_scheme(
99 self.as_ref().to_glib_none().0,
100 ))
101 }
102 }
103}
104
105impl fmt::Display for NetworkAddress {
106 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107 write!(f, "NetworkAddress")
108 }
109}