From cd97d90314143141cecd694ca5f4a0edad35c257 Mon Sep 17 00:00:00 2001
From: chenjiayi <chenjiayi22@huawei.com>
Date: Thu, 18 May 2023 11:45:17 +0800
Subject: [PATCH] fix: compatible with rustc 1.60

---
 coms/service/src/rentry.rs       | 45 +++++++++++++++-----
 coms/socket/src/rentry.rs        |  9 +++-
 core/bin/main.rs                 |  3 +-
 core/bin/unit/entry/config.rs    |  9 +++-
 core/bin/unit/rentry.rs          |  9 +++-
 core/lib/unit/kill.rs            |  9 +++-
 exts/sctl/src/main.rs            | 71 +++++++-------------------------
 libs/device/src/device_action.rs |  9 +++-
 8 files changed, 88 insertions(+), 76 deletions(-)

diff --git a/coms/service/src/rentry.rs b/coms/service/src/rentry.rs
index 175828e..a68a3df 100644
--- a/coms/service/src/rentry.rs
+++ b/coms/service/src/rentry.rs
@@ -39,10 +39,9 @@ struct ServiceReDb<K, V>(ReDb<K, V>);
 const RELI_DB_HSERVICE_CONF: &str = "svcconf";
 const RELI_DB_HSERVICE_MNG: &str = "svcmng";
 
-#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy, Default)]
+#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy)]
 pub(super) enum ServiceType {
     #[serde(alias = "simple")]
-    #[default]
     Simple,
     #[serde(alias = "forking")]
     Forking,
@@ -56,6 +55,12 @@ pub(super) enum ServiceType {
     TypeInvalid = -1,
 }
 
+impl Default for ServiceType {
+    fn default() -> Self {
+        Self::Simple
+    }
+}
+
 impl DeserializeWith for ServiceType {
     type Item = Self;
     fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
@@ -82,10 +87,9 @@ pub(super) enum NotifyAccess {
     Main,
 }
 
-#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy, Default)]
+#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone, Copy)]
 pub(super) enum ServiceRestart {
     #[serde(alias = "no")]
-    #[default]
     No,
     #[serde(alias = "on-success")]
     OnSuccess,
@@ -101,6 +105,12 @@ pub(super) enum ServiceRestart {
     Always,
 }
 
+impl Default for ServiceRestart {
+    fn default() -> Self {
+        Self::No
+    }
+}
+
 #[derive(Debug, Clone, Serialize, Deserialize, Default)]
 pub struct ExitStatusSet {
     status: Vec<u8>,
@@ -163,14 +173,19 @@ impl DeserializeWith for ExitStatusSet {
     }
 }
 
-#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
+#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
 pub enum PreserveMode {
-    #[default]
     No,
     Yes,
     Restart,
 }
 
+impl Default for PreserveMode {
+    fn default() -> Self {
+        Self::No
+    }
+}
+
 impl DeserializeWith for PreserveMode {
     type Item = Self;
 
@@ -330,9 +345,8 @@ impl ServiceReConf {
     }
 }
 
-#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize, EnumDisplay, Default)]
+#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize, EnumDisplay)]
 pub(super) enum ServiceState {
-    #[default]
     Dead,
     Condition,
     StartPre,
@@ -354,7 +368,13 @@ pub(super) enum ServiceState {
     Cleaning,
 }
 
-#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize, Default)]
+impl Default for ServiceState {
+    fn default() -> Self {
+        Self::Dead
+    }
+}
+
+#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize)]
 pub(super) enum ServiceResult {
     Success,
     FailureProtocol,
@@ -366,10 +386,15 @@ pub(super) enum ServiceResult {
     FailureCoreDump,
     FailureTimeout,
     SkipCondition,
-    #[default]
     ResultInvalid,
 }
 
+impl Default for ServiceResult {
+    fn default() -> Self {
+        Self::ResultInvalid
+    }
+}
+
 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy, Clone, Serialize, Deserialize)]
 pub(super) enum ServiceCommand {
     Condition,
diff --git a/coms/socket/src/rentry.rs b/coms/socket/src/rentry.rs
index 2409c84..44f5be3 100644
--- a/coms/socket/src/rentry.rs
+++ b/coms/socket/src/rentry.rs
@@ -146,15 +146,20 @@ pub(super) enum SocketCommand {
     StopPost,
 }
 
-#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Default)]
+#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
 pub(super) enum PortType {
-    #[default]
     Socket,
     Fifo,
     Special,
     Invalid,
 }
 
+impl Default for PortType {
+    fn default() -> Self {
+        Self::Socket
+    }
+}
+
 #[derive(Clone, Debug, Serialize, Deserialize)]
 struct SocketReMng {
     state: SocketState,
diff --git a/core/bin/main.rs b/core/bin/main.rs
index 1949cae..26638d6 100644
--- a/core/bin/main.rs
+++ b/core/bin/main.rs
@@ -186,7 +186,8 @@ fn do_reexecute(args: &Vec<String>, reload: bool) {
     // Remove '--deserialize' from the previous parameter first, as this may be a fault recovery start.
     for index in argv.iter().enumerate() {
         if index.1 == "--deserialize" {
-            argv.remove(index.0);
+            let idx = index.0;
+            argv.remove(idx);
             break;
         }
     }
diff --git a/core/bin/unit/entry/config.rs b/core/bin/unit/entry/config.rs
index 9e8cc62..e629c77 100644
--- a/core/bin/unit/entry/config.rs
+++ b/core/bin/unit/entry/config.rs
@@ -31,10 +31,9 @@ pub(crate) struct UeConfig {
 }
 
 #[allow(missing_docs)]
-#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
 pub enum UnitEmergencyAction {
     #[serde(alias = "none")]
-    #[default]
     None,
     #[serde(alias = "reboot")]
     Reboot,
@@ -54,6 +53,12 @@ pub enum UnitEmergencyAction {
     ExitForce,
 }
 
+impl Default for UnitEmergencyAction {
+    fn default() -> Self {
+        Self::None
+    }
+}
+
 impl DeserializeWith for UnitEmergencyAction {
     type Item = Self;
     fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
diff --git a/core/bin/unit/rentry.rs b/core/bin/unit/rentry.rs
index 739a323..5d154df 100644
--- a/core/bin/unit/rentry.rs
+++ b/core/bin/unit/rentry.rs
@@ -68,12 +68,11 @@ impl UnitReLoad {
 }
 
 /// jobMode why in UnitRentry? change the name?
-#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
 pub(crate) enum JobMode {
     #[serde(alias = "fail")]
     Fail,
     #[serde(alias = "replace")]
-    #[default]
     Replace,
     #[serde(alias = "replace_irreversible")]
     ReplaceIrreversible,
@@ -89,6 +88,12 @@ pub(crate) enum JobMode {
     Trigger,
 }
 
+impl Default for JobMode {
+    fn default() -> Self {
+        Self::Replace
+    }
+}
+
 impl DeserializeWith for JobMode {
     type Item = Self;
     fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
diff --git a/core/lib/unit/kill.rs b/core/lib/unit/kill.rs
index 893125e..4d66861 100644
--- a/core/lib/unit/kill.rs
+++ b/core/lib/unit/kill.rs
@@ -42,10 +42,9 @@ impl KillOperation {
 }
 
 /// the method to kill the process
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
 pub enum KillMode {
     /// kill all the process in the cgroup of the unit
-    #[default]
     ControlGroup,
     /// only kill the main process
     Process,
@@ -53,6 +52,12 @@ pub enum KillMode {
     Mixed,
 }
 
+impl Default for KillMode {
+    fn default() -> Self {
+        Self::ControlGroup
+    }
+}
+
 impl DeserializeWith for KillMode {
     type Item = Self;
     fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
diff --git a/exts/sctl/src/main.rs b/exts/sctl/src/main.rs
index 20f1934..f799629 100644
--- a/exts/sctl/src/main.rs
+++ b/exts/sctl/src/main.rs
@@ -13,17 +13,14 @@
 //!
 
 use clap::Parser;
-use cmdproto::{
-    error::ERROR_CODE_MASK_PRINT_STDOUT,
-    proto::{
-        abi::{sys_comm, unit_comm, CommandRequest},
-        mngr_comm, unit_file, ProstClientStream,
-    },
+use cmdproto::proto::{
+    abi::{sys_comm, unit_comm, CommandRequest},
+    mngr_comm, unit_file, ProstClientStream,
 };
 use std::io::Write;
 use std::{
     net::{SocketAddr, TcpStream},
-    process::{ExitCode, Termination},
+    process::exit,
 };
 
 /// parse program arguments
@@ -187,50 +184,13 @@ fn generate_command_request(args: Args) -> Option<CommandRequest> {
     Some(command_request)
 }
 
-/// Result used in sctl
-pub enum Result {
-    ///
-    OK,
-    ///
-    Failure(String, u32),
-}
-
-/*
- * Implementing Termination is important. Because the default implementation
- * always adds a "Error: " prefix to our error message, this is ugly. And it
- * always exits with 1 if we return Err, this is bad, we want sctl to return
- * many other positive errors like systemctl has done.
- */
-
-impl Termination for Result {
-    fn report(self) -> ExitCode {
-        match self {
-            Result::OK => ExitCode::SUCCESS,
-            Result::Failure(s, error_code) => {
-                if error_code & ERROR_CODE_MASK_PRINT_STDOUT != 0 {
-                    let _ = writeln!(std::io::stdout(), "{s}");
-                } else {
-                    let _ = writeln!(std::io::stderr(), "{s}");
-                }
-                let error_code = error_code ^ ERROR_CODE_MASK_PRINT_STDOUT;
-                if error_code > u8::MAX.into() {
-                    return ExitCode::FAILURE;
-                }
-                ExitCode::from(error_code as u8)
-            }
-        }
-    }
-}
-
-fn main() -> Result {
+fn main() {
     let args = Args::parse();
 
     let command_request = match generate_command_request(args) {
         None => {
-            return Result::Failure(
-                "This command is currently not supported".to_string(),
-                nix::Error::ENOTSUP as u32,
-            );
+            eprintln!("This command is currently not supported");
+            exit(nix::Error::ENOTSUP as i32);
         }
         Some(v) => v,
     };
@@ -242,8 +202,8 @@ fn main() -> Result {
 
     let stream = match TcpStream::connect(&addrs[..]) {
         Err(e) => {
-            let err_msg = format!("Failed to connect to sysmaster: {}", e);
-            return Result::Failure(err_msg, e.raw_os_error().unwrap() as u32);
+            eprintln!("Failed to connect to sysmaster: {}", e);
+            exit(e.raw_os_error().unwrap() as i32);
         }
         Ok(v) => v,
     };
@@ -252,23 +212,24 @@ fn main() -> Result {
 
     let data = match client.execute(command_request) {
         Err(e) => {
-            let err_msg = format!("Failed to execute the given command: {}", e);
-            // don't want to map all kinds of errors the message is clear, just return 1.
-            return Result::Failure(err_msg, 1);
+            eprintln!("Failed to execute the given command: {}", e);
+            exit(1);
         }
         Ok(v) => v,
     };
 
     /* We should always print the error message if the returned error code is not 0. */
     if data.message.is_empty() {
-        return Result::OK;
+        exit(0);
     }
 
     if data.error_code == 0 {
         /* Don't care if we fail to write the error out. */
         let _ = writeln!(std::io::stdout(), "{}", data.message);
-        return Result::OK;
+        exit(0);
     }
 
-    Result::Failure(data.message, data.error_code)
+    eprintln!("{}", data.message);
+
+    exit(data.error_code as i32);
 }
diff --git a/libs/device/src/device_action.rs b/libs/device/src/device_action.rs
index ac39820..d9c5289 100644
--- a/libs/device/src/device_action.rs
+++ b/libs/device/src/device_action.rs
@@ -19,7 +19,7 @@ use nix::errno::Errno;
 
 /// device action based on kobject from kernel
 #[allow(missing_docs)]
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 pub enum DeviceAction {
     Add,
     Remove,
@@ -29,10 +29,15 @@ pub enum DeviceAction {
     Offline,
     Bind,
     Unbind,
-    #[default]
     Invalid,
 }
 
+impl Default for DeviceAction {
+    fn default() -> Self {
+        Self::Invalid
+    }
+}
+
 impl FromStr for DeviceAction {
     type Err = crate::error::Error;
 
-- 
2.33.0

