From 5fbd070f79a0526f647fb5bb734ad9edac9e2fe1 Mon Sep 17 00:00:00 2001 From: licunlong Date: Wed, 31 May 2023 11:08:24 +0800 Subject: [PATCH 3/9] feature: add as_any() to SubUnit so we can downcast SubUnit trait to concrete componnent --- coms/mount/src/unit.rs | 4 ++++ coms/service/src/unit.rs | 4 ++++ coms/socket/src/unit.rs | 5 +++++ coms/target/src/unit.rs | 4 ++++ core/lib/unit/base.rs | 3 +++ 5 files changed, 20 insertions(+) diff --git a/coms/mount/src/unit.rs b/coms/mount/src/unit.rs index dfa6087..dc84403 100644 --- a/coms/mount/src/unit.rs +++ b/coms/mount/src/unit.rs @@ -64,6 +64,10 @@ impl MountUnit { } impl SubUnit for MountUnit { + fn as_any(&self) -> &dyn std::any::Any { + self + } + fn load(&self, _paths: Vec) -> Result<()> { if let Some(u) = self.comm.owner() { u.set_ignore_on_isolate(true) diff --git a/coms/service/src/unit.rs b/coms/service/src/unit.rs index c91a296..7ea3c44 100644 --- a/coms/service/src/unit.rs +++ b/coms/service/src/unit.rs @@ -71,6 +71,10 @@ impl ReStation for ServiceUnit { } impl SubUnit for ServiceUnit { + fn as_any(&self) -> &dyn std::any::Any { + self + } + fn init(&self) { todo!() } diff --git a/coms/socket/src/unit.rs b/coms/socket/src/unit.rs index 198fafc..8e33ee8 100644 --- a/coms/socket/src/unit.rs +++ b/coms/socket/src/unit.rs @@ -23,6 +23,7 @@ use crate::{ use basic::logger; use nix::sys::wait::WaitStatus; use std::{path::PathBuf, rc::Rc}; +use std::any::Any; use sysmaster::error::*; use sysmaster::exec::ExecContext; use sysmaster::rel::{ReStation, Reliability}; @@ -68,6 +69,10 @@ impl ReStation for SocketUnit { } impl SubUnit for SocketUnit { + fn as_any(&self) -> &dyn Any { + self + } + fn load(&self, paths: Vec) -> Result<()> { log::debug!("socket begin to load conf file"); self.config.load(paths, true)?; diff --git a/coms/target/src/unit.rs b/coms/target/src/unit.rs index 26b9950..e4caf5d 100644 --- a/coms/target/src/unit.rs +++ b/coms/target/src/unit.rs @@ -120,6 +120,10 @@ impl Target { } impl SubUnit for Target { + fn as_any(&self) -> &dyn std::any::Any { + self + } + fn load(&self, _conf_str: Vec) -> Result<()> { //todo add default dependency funnction need add log::debug!("load for target"); diff --git a/core/lib/unit/base.rs b/core/lib/unit/base.rs index 0996f5d..d279295 100644 --- a/core/lib/unit/base.rs +++ b/core/lib/unit/base.rs @@ -17,6 +17,7 @@ use super::umif::UnitMngUtil; use crate::error::*; use nix::sys::wait::WaitStatus; use nix::{sys::socket::UnixCredentials, unistd::Pid}; +use std::any::Any; use std::{collections::HashMap, path::PathBuf, rc::Rc}; ///The trait Defining Shared Behavior from Base Unit to SUB unit @@ -72,6 +73,8 @@ pub trait UnitBase { /// difference sub unit ref by dynamic trait /// pub trait SubUnit: ReStation + UnitMngUtil { + /// + fn as_any(&self) -> &dyn Any; /// fn init(&self) {} -- 2.30.2