Hello,
I'm a Rust beginner and I'm struggeling a little bit with the ownership especially with 'static
of an external dependency. I'm using the crate for Prolog Swipl and this is my current code:
use pnet::datalink::NetworkInterface;
use swipl::context::{ActivatedEngine, Context};
use swipl::engine::EngineActivation;
use swipl::init::initialize_swipl;
use swipl::result::PrologError;
use swipl::{pred, term};
pub struct Engine<> {
engine: EngineActivation<'static>,
}
impl Engine {
/// constructor
pub fn new() -> Self {
let engine = initialize_swipl().unwrap();
Self { engine }
}
pub fn add_interface(self, ip: NetworkInterface) -> Result<(), PrologError> {
let context: Context<_> = self.engine.into();
for i in ip.ips.clone() {
let value = format!(
"_{{interface: _{{name: \"{}\"}}, _{{ip: \"{}\"}}",
ip.name, i
);
let term = context.term_from_string(&*value)?;
tracing::info!("append interface term: {:?}", term);
context.call_once(pred!(writeq / 1), [&term])?;
}
Ok(())
}
pub fn add_ip(self, ip: String) -> Result<(), PrologError> {
let context: Context<_> = self.engine.into();
let value = format!("_{{ip: \"{}\"}}", ip);
let term = context.term_from_string(&*value)?;
tracing::info!("append ip term: {:?}", term);
context.call_once(pred!(writeq / 1), [&term])?;
Ok(())
}
}
I get into stuck, because the engine is a static lifetime but I would like to get a struct, which hides the Prolog engine.
I would like to do in my main program
use pnet::datalink;
[...]
let prolog = Engine::new()
let interfaces = datalink::interfaces();
interfaces
.into_iter()
.filter(|i| !i.is_loopback() && !i.is_multicast() && i.is_up() && i.is_running())
.for_each(|i| add_interface(prolog, i).unwrap());
Is this the correct way to do it? How can I improve my code in a better way?
Thanks a lot