ns3_parallel/
core.rs

1//! Core traits for the library
2
3/// # BuildParam
4///
5/// This trait is used to build the parameters for the NS3 program.
6///
7/// Implement this trait and also the `Default` trait on your own config struct.
8///
9/// ## Example
10///
11/// ```
12/// use serde::{Deserialize, Serialize};
13/// use ns3_parallel::{BuildParam, BuildCmd};
14///
15/// #[derive(Debug, Serialize, Deserialize)]
16/// #[serde(default)]
17/// pub struct Config {
18///     pub sim_time: u32,
19///     pub app_name: String,
20///     pub policy: Vec<u32>,
21/// }
22///
23/// #[derive(Debug, Clone, Serialize, Deserialize)]
24/// pub struct Param {
25///     pub sim_time: u32,
26///     pub app_name: String,
27///     pub policy: u32,
28/// }
29///
30/// impl Default for Config {
31///     fn default() -> Self {
32///         Config {
33///             sim_time: 100,
34///             app_name: "ns3-tcp-bbr".to_string(),
35///             policy: vec![1, 2, 3],
36///         }
37///     }
38/// }
39///
40/// impl BuildParam<Param> for Config {
41///     fn build_param(&self) -> Vec<Param> {
42///         let mut params: Vec<Param> = Vec::new();
43///         for policy in &self.policy {
44///             let param = Param {
45///                 sim_time: self.sim_time,
46///                 app_name: self.app_name.clone(),
47///                 policy: *policy,
48///             };
49///             params.push(param);
50///         }
51///         params
52///     }      
53/// }
54/// impl BuildCmd for Param {
55///     fn build_cmd(&self) -> String {
56///         format!(
57///             "xxx --app-name={} --sim-time={} --policy={}",
58///             self.app_name, self.sim_time, self.policy
59///         )
60///     }
61/// }
62pub trait BuildParam<P: BuildCmd> {
63    fn build_param(&self) -> Vec<P>;
64}
65
66/// # BuildCmd
67///
68/// This trait is used to build the command line for the NS3 program.
69///
70/// Implement this trait on your own param struct.
71///
72/// ## Example
73///
74/// ```
75/// use serde::{Deserialize, Serialize};
76/// use ns3_parallel::{BuildParam, BuildCmd};
77///
78/// #[derive(Debug, Serialize, Deserialize)]
79/// #[serde(default)]
80/// pub struct Config {
81///     pub sim_time: u32,
82///     pub app_name: String,
83///     pub policy: Vec<u32>,
84/// }
85///
86/// #[derive(Debug, Clone, Serialize, Deserialize)]
87/// pub struct Param {
88///     pub sim_time: u32,
89///     pub app_name: String,
90///     pub policy: u32,
91/// }
92///
93/// impl Default for Config {
94///     fn default() -> Self {
95///         Config {
96///             sim_time: 100,
97///             app_name: "ns3-tcp-bbr".to_string(),
98///             policy: vec![1, 2, 3],
99///         }
100///     }
101/// }
102///
103/// impl BuildParam<Param> for Config {
104///     fn build_param(&self) -> Vec<Param> {
105///         let mut params: Vec<Param> = Vec::new();
106///         for policy in &self.policy {
107///             let param = Param {
108///                 sim_time: self.sim_time,
109///                 app_name: self.app_name.clone(),
110///                 policy: *policy,
111///             };
112///             params.push(param);
113///         }
114///         params
115///     }      
116/// }
117/// impl BuildCmd for Param {
118///     fn build_cmd(&self) -> String {
119///         format!(
120///             "xxx --app-name={} --sim-time={} --policy={}",
121///             self.app_name, self.sim_time, self.policy
122///         )
123///     }
124/// }
125pub trait BuildCmd {
126    fn build_cmd(&self) -> String;
127}