我有一个必须在Windows和Linux系统上运行的服务器应用程序. 这个问题始于我使用的mdns/zeroconf个模块.
我两个都用:
-
Linux的
zeroconf
(https://docs.rs/zeroconf/latest/zeroconf/) -
Windows的
astro_dnssd
(https://github.com/astrohq/astro-dnssd)
Why I had to use 2 modules to do the same thing?个
我不得不使用两个模块来做基本上相同的事情,因为zeroconf
支持Linux但不支持Windows,astro_dnssd
使用bonjour-compatibility layer on top of Avahi for Linux which no longer supported,但astro_dnssd
在Windows上运行得很好
My 100:
// asto_dnssd can be compiled on both Windows and Linux, but doesn't work as intended on Linux
use astro_dnssd::DNSServiceBuilder;
// this modules doesn't even compile on Windows
// so I had to manually disable this line and everything that uses zeroconf to run my server app on Windows, including the "Cargo.toml" file
use zeroconf::prelude::*;
use zeroconf::{MdnsService, ServiceRegistration, ServiceType, TxtRecord};
The problem:个
到目前为止,每当我在Windows上工作时,我都必须手动禁用zeroconf
(通过注释该行),当我在Linux上工作时,我必须再次启用它.这是一项繁琐的工作,从长远来看是不可持续的.是否可以使用env变量或env::consts::OS
来检测并在Rust中进行"有条件导入"?例如:
let current_os = env::consts::OS;
match current_os {
"windows" => {
use astro_dnssd::DNSServiceBuilder;
//... the rest of the code
}
"linux" => {
use zeroconf::prelude::*;
use zeroconf::{MdnsService, ServiceRegistration, ServiceType, TxtRecord};
// ... the rest of the code
}
_ => {
panic!("This operating system is not supported yet")
}
};