1f857971dSopenharmony_ci/* 2f857971dSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f857971dSopenharmony_ci * you may not use this file except in compliance with the License. 5f857971dSopenharmony_ci * You may obtain a copy of the License at 6f857971dSopenharmony_ci * 7f857971dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f857971dSopenharmony_ci * 9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f857971dSopenharmony_ci * See the License for the specific language governing permissions and 13f857971dSopenharmony_ci * limitations under the License. 14f857971dSopenharmony_ci */ 15f857971dSopenharmony_ci 16f857971dSopenharmony_ci//! Implementation of task handle. 17f857971dSopenharmony_ci 18f857971dSopenharmony_ciuse fusion_utils_rust::{ FusionErrorCode, FusionResult }; 19f857971dSopenharmony_ciuse ylong_runtime::task::JoinHandle; 20f857971dSopenharmony_ci 21f857971dSopenharmony_ci/// Representation of task handle. 22f857971dSopenharmony_cipub struct TaskHandle<R> { 23f857971dSopenharmony_ci join_handle: Option<JoinHandle<R>>, 24f857971dSopenharmony_ci} 25f857971dSopenharmony_ci 26f857971dSopenharmony_ciimpl<R> From<JoinHandle<R>> for TaskHandle<R> { 27f857971dSopenharmony_ci fn from(value: JoinHandle<R>) -> Self 28f857971dSopenharmony_ci { 29f857971dSopenharmony_ci Self { 30f857971dSopenharmony_ci join_handle: Some(value), 31f857971dSopenharmony_ci } 32f857971dSopenharmony_ci } 33f857971dSopenharmony_ci} 34f857971dSopenharmony_ci 35f857971dSopenharmony_ciimpl<R> TaskHandle<R> { 36f857971dSopenharmony_ci /// Cancel this task. 37f857971dSopenharmony_ci pub fn cancel(&mut self) 38f857971dSopenharmony_ci { 39f857971dSopenharmony_ci if let Some(join_handle) = self.join_handle.take() { 40f857971dSopenharmony_ci join_handle.cancel(); 41f857971dSopenharmony_ci } 42f857971dSopenharmony_ci } 43f857971dSopenharmony_ci 44f857971dSopenharmony_ci /// Get result of this task. 45f857971dSopenharmony_ci pub fn result(&mut self) -> FusionResult<R> 46f857971dSopenharmony_ci { 47f857971dSopenharmony_ci if let Some(join_handle) = self.join_handle.take() { 48f857971dSopenharmony_ci if let Ok(ret) = ylong_runtime::block_on(join_handle) { 49f857971dSopenharmony_ci Ok(ret) 50f857971dSopenharmony_ci } else { 51f857971dSopenharmony_ci Err(FusionErrorCode::Fail) 52f857971dSopenharmony_ci } 53f857971dSopenharmony_ci } else { 54f857971dSopenharmony_ci Err(FusionErrorCode::Fail) 55f857971dSopenharmony_ci } 56f857971dSopenharmony_ci } 57f857971dSopenharmony_ci} 58