lxc::Container::get_ips() design improvement
koutheir opened this issue · comments
The lxc::Container::get_ips()
API encapsulates the get_ips()
of lxc_sys::lxc_container
.
There are multiples issues with the design of lxc::Container::get_ips()
:
- It returns
Vec<String>
instead ofVec<std::net::IpAddr>
. IP addresses are rarely used simply as strings. They're often parsed before becoming useful. - The first argument is
interfaces: &str
:- The raw
get_ips()
gets aninterface: *const c_char
. Using the plural form "interfaces" is misleading. - The raw
get_ips()
can acceptNULL
as the value for this parameter, which has its own semantics in the implementation ofget_ips()
inliblxc
.
- The raw
- The second argument is
family: &str
: the rawget_ips()
can acceptNULL
as the value for this parameter, which has its own semantics in the implementation ofget_ips()
inliblxc
. - The third argument is
scope: i32
, but the rawget_ips()
gets ascope: c_int
. Assuming that a Cint
maps always toi32
is a bad idea, and is not always true.
Given all these points, I suggest changing the prototype of lxc::Container::get_ips()
to the following:
pub fn get_ips(
&self,
interface: Option<&str>,
family: Option<&str>,
scope: std::os::raw::c_int)
-> Result<Vec<std::net::IpAddr>>
{
/*... */
}
It’s work fine:
Container state: RUNNING
Container PID: 32254
Interfaces: ["eth0", "lo"]
IPs: [10.0.3.206]
The only issue remaining is the unwrap()
calls that panic if liblxc
generates an error. This is not reasonable for a library.
The only issue remaining is the
unwrap()
calls that panic ifliblxc
generates an error. This is not reasonable for a library.
There is many unwrap
in my code, I work on a redesign of errors handling.
Until panics are reduced to a minimum, the only crate I can currently use is lxc-sys
, basically reimplementing the ergonomic structures while reporting liblxc
errors as normal Result
s.