changeset 16:bc76507dd878

Fixed conditional compilation based on OS A mistake was causing systemd to never enable and the tests were too clever to detect that. Not sure what to do with it.
author Martin Habovstiak <martin.habovstiak@gmail.com>
date Tue, 22 Dec 2020 13:58:47 +0100
parents 08b37039504b
children dfb727367934
files Cargo.toml src/error.rs src/lib.rs tests/systemd.rs
diffstat 4 files changed, 25 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/Cargo.toml	Tue Dec 22 13:56:56 2020 +0100
+++ b/Cargo.toml	Tue Dec 22 13:58:47 2020 +0100
@@ -17,10 +17,12 @@
 [features]
 default = ["enable_systemd"]
 serde = ["serde_crate", "serde_str_helpers"]
-enable_systemd = []
+enable_systemd = ["libsystemd"]
 
-[target.'cfg(all(target_os = "linux", feature = "enable_systemd"))'.dependencies]
-libsystemd = "0.2.1"
+[target.'cfg(target_os = "linux")'.dependencies]
+# WARNING: It is NOT guaranteed that this crate will always use libsystemd as dependency!
+#          This as a feature is not considered a public interface!
+libsystemd = { version = "0.2.1", optional = true }
 
 [dependencies]
 thiserror = "1.0.21"
--- a/src/error.rs	Tue Dec 22 13:56:56 2020 +0100
+++ b/src/error.rs	Tue Dec 22 13:58:47 2020 +0100
@@ -19,15 +19,15 @@
 pub(crate) enum ParseErrorInner {
     #[error("failed to parse socket address")]
     ResolvAddr(#[from] crate::resolv_addr::ResolvAddrError),
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     #[error("invalid character '{c}' in systemd socket name {string} at position {pos}")]
     InvalidCharacter { string: String, c: char, pos: usize, },
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     #[error("systemd socket name {string} is {len} characters long which is more than the limit 255")]
     LongSocketName { string: String, len: usize, },
-    #[cfg(not(all(linux, feature = "enable_systemd")))]
-    #[cfg_attr(not(linux), error("can't parse {0} because systemd is not supported on this operating system"))]
-    #[cfg_attr(linux, error("can't parse {0} because systemd support was disabled during build"))]
+    #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
+    #[cfg_attr(not(target_os = "linux"), error("can't parse {0} because systemd is not supported on this operating system"))]
+    #[cfg_attr(target_os = "linux", error("can't parse {0} because systemd support was disabled during build"))]
     SystemdUnsupported(String),
 }
 
@@ -66,14 +66,14 @@
     BindFailed { addr: std::net::SocketAddr, #[source] error: io::Error, },
     #[error("failed to bind {addr}")]
     BindOrResolvFailed { addr: crate::resolv_addr::ResolvAddr, #[source] error: io::Error, },
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     #[error("failed to receive descriptors with names")]
     ReceiveDescriptors(#[source] crate::systemd_sockets::Error),
     #[error("missing systemd socket {0} - a typo or an attempt to bind twice")]
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     MissingDescriptor(String),
     #[error("the systemd socket {0} is not an internet socket")]
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     NotInetSocket(String),
 }
 
--- a/src/lib.rs	Tue Dec 22 13:56:56 2020 +0100
+++ b/src/lib.rs	Tue Dec 22 13:58:47 2020 +0100
@@ -68,10 +68,10 @@
 use crate::error::*;
 use crate::resolv_addr::ResolvAddr;
 
-#[cfg(not(all(linux, feature = "enable_systemd")))]
+#[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
 use std::convert::Infallible as Never;
 
-#[cfg(all(linux, feature = "enable_systemd"))]
+#[cfg(all(target_os = "linux", feature = "enable_systemd"))]
 pub(crate) mod systemd_sockets {
     use std::fmt;
     use std::sync::Mutex;
@@ -227,7 +227,7 @@
     // rules.
     fn try_from_generic<'a, T>(string: T) -> Result<Self, ParseError> where T: 'a + std::ops::Deref<Target=str> + Into<String> {
         if string.starts_with(SYSTEMD_PREFIX) {
-            #[cfg(all(linux, feature = "enable_systemd"))]
+            #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
             {
                 let name_len = string.len() - SYSTEMD_PREFIX.len();
                 match string[SYSTEMD_PREFIX.len()..].chars().enumerate().find(|(_, c)| !c.is_ascii() || *c < ' ' || *c == ':') {
@@ -236,7 +236,7 @@
                     Some((pos, c)) => Err(ParseErrorInner::InvalidCharacter { string: string.into(), c, pos, }.into()),
                 }
             }
-            #[cfg(not(all(linux, feature = "enable_systemd")))]
+            #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
             {
                 Err(ParseErrorInner::SystemdUnsupported(string.into()).into())
             }
@@ -248,7 +248,7 @@
         }
     }
 
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     fn get_systemd(socket_name: String) -> Result<(std::net::TcpListener, SocketAddrInner), BindError> {
         use libsystemd::activation::IsType;
         use std::os::unix::io::{FromRawFd, IntoRawFd};
@@ -268,8 +268,8 @@
     }
 
     // This approach makes the rest of the code much simpler as it doesn't require sprinkling it
-    // with #[cfg(all(linux, feature = "enable_systemd"))] yet still statically guarantees it won't execute.
-    #[cfg(not(linux))]
+    // with #[cfg(all(target_os = "linux", feature = "enable_systemd"))] yet still statically guarantees it won't execute.
+    #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
     fn get_systemd(socket_name: Never) -> Result<(std::net::TcpListener, SocketAddrInner), BindError> {
         match socket_name {}
     }
@@ -300,9 +300,9 @@
 enum SocketAddrInner {
     Ordinary(std::net::SocketAddr),
     WithHostname(resolv_addr::ResolvAddr),
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     Systemd(String),
-    #[cfg(not(all(linux, feature = "enable_systemd")))]
+    #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
     #[allow(dead_code)]
     Systemd(Never),
 }
@@ -404,13 +404,13 @@
     }
 
     #[test]
-    #[cfg(all(linux, feature = "enable_systemd"))]
+    #[cfg(all(target_os = "linux", feature = "enable_systemd"))]
     fn parse_systemd() {
         assert_eq!("systemd://foo".parse::<SocketAddr>().unwrap().0, SocketAddrInner::Systemd("systemd://foo".to_owned()));
     }
 
     #[test]
-    #[cfg(not(all(linux, feature = "enable_systemd")))]
+    #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))]
     #[should_panic]
     fn parse_systemd() {
         "systemd://foo".parse::<SocketAddr>().unwrap();
--- a/tests/systemd.rs	Tue Dec 22 13:56:56 2020 +0100
+++ b/tests/systemd.rs	Tue Dec 22 13:58:47 2020 +0100
@@ -23,7 +23,7 @@
 }
 
 #[test]
-#[cfg_attr(not(all(linux, feature = "enable_systemd")), should_panic)]
+#[cfg_attr(not(all(target_os = "linux", feature = "enable_systemd")), should_panic)]
 fn main() {
     comm::main::<Test>();
 }