1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 //! Builder to configure the task. Tasks that get spawned through 15 //! [`TaskBuilder`] inherit all attributes of this builder. 16 //! 17 //! A task has following attributes: 18 //! - qos 19 //! - task name 20 21 use std::future::Future; 22 23 use crate::spawn::{spawn_async, spawn_blocking}; 24 use crate::task::Qos; 25 use crate::JoinHandle; 26 27 /// Tasks attribute 28 #[derive(Clone)] 29 pub struct TaskBuilder { 30 pub(crate) name: Option<String>, 31 pub(crate) qos: Option<Qos>, 32 } 33 34 impl Default for TaskBuilder { default() -> Self35 fn default() -> Self { 36 Self::new() 37 } 38 } 39 40 impl TaskBuilder { 41 /// Creates a new TaskBuilder with a default setting. new() -> Self42 pub fn new() -> Self { 43 TaskBuilder { 44 name: None, 45 qos: None, 46 } 47 } 48 49 /// Sets the name of the task. name(mut self, name: String) -> Self50 pub fn name(mut self, name: String) -> Self { 51 self.name = Some(name); 52 self 53 } 54 55 /// Sets the qos of the task qos(mut self, qos: Qos) -> Self56 pub fn qos(mut self, qos: Qos) -> Self { 57 self.qos = Some(qos); 58 self 59 } 60 61 /// todo: for multiple-instance runtime, should provide a spawn_on 62 /// Using the current task setting, spawns a task onto the global runtime. spawn<T, R>(&self, task: T) -> JoinHandle<R> where T: Future<Output = R>, T: Send + 'static, R: Send + 'static,63 pub fn spawn<T, R>(&self, task: T) -> JoinHandle<R> 64 where 65 T: Future<Output = R>, 66 T: Send + 'static, 67 R: Send + 'static, 68 { 69 spawn_async(self, task) 70 } 71 72 /// Using the current task setting, spawns a task onto the blocking pool. spawn_blocking<T, R>(&self, task: T) -> JoinHandle<R> where T: FnOnce() -> R, T: Send + 'static, R: Send + 'static,73 pub fn spawn_blocking<T, R>(&self, task: T) -> JoinHandle<R> 74 where 75 T: FnOnce() -> R, 76 T: Send + 'static, 77 R: Send + 'static, 78 { 79 spawn_blocking(self, task) 80 } 81 } 82 83 #[cfg(test)] 84 mod test { 85 use crate::task::{Qos, TaskBuilder}; 86 87 #[test] ut_task()88 fn ut_task() { 89 ut_builder_new(); 90 ut_builder_name(); 91 ut_builder_pri(); 92 } 93 94 /// UT test cases for Builder::new 95 /// 96 /// # Brief 97 /// 1. Checks if the object name property is None 98 /// 2. Checks if the object pri property is None 99 /// 3. Checks if the object worker_id property is None 100 /// 4. Checks if the object is_stat property is false 101 /// 5. Checks if the object is_insert_front property is false ut_builder_new()102 fn ut_builder_new() { 103 let builder = TaskBuilder::new(); 104 assert_eq!(builder.name, None); 105 assert!(builder.qos.is_none()); 106 } 107 108 /// UT test cases for Builder::name 109 /// 110 /// # Brief 111 /// 1. Checks if the object name property is a modified value ut_builder_name()112 fn ut_builder_name() { 113 let builder = TaskBuilder::new(); 114 115 let name = String::from("builder_name"); 116 assert_eq!(builder.name(name.clone()).name.unwrap(), name); 117 } 118 119 /// UT test cases for Builder::name 120 /// 121 /// # Brief 122 /// 1. pri set to Background, check return value 123 /// 2. pri set to Utility, check return value 124 /// 3. pri set to UserInteractive, check return value 125 /// 4. pri set to UserInitiated, check return value 126 /// 5. pri set to Default, check return value ut_builder_pri()127 fn ut_builder_pri() { 128 let builder = TaskBuilder::new(); 129 let pri = Qos::Background; 130 assert_eq!(builder.qos(pri).qos.unwrap(), pri); 131 132 let builder = TaskBuilder::new(); 133 let pri = Qos::Utility; 134 assert_eq!(builder.qos(pri).qos.unwrap(), pri); 135 136 let builder = TaskBuilder::new(); 137 let pri = Qos::UserInteractive; 138 assert_eq!(builder.qos(pri).qos.unwrap(), pri); 139 140 let builder = TaskBuilder::new(); 141 let pri = Qos::UserInitiated; 142 assert_eq!(builder.qos(pri).qos.unwrap(), pri); 143 144 let builder = TaskBuilder::new(); 145 let pri = Qos::Default; 146 assert_eq!(builder.qos(pri).qos.unwrap(), pri); 147 } 148 } 149