1cac7dca0Sopenharmony_ci// Copyright (c) 2023 Huawei Device Co., Ltd. 2cac7dca0Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 3cac7dca0Sopenharmony_ci// you may not use this file except in compliance with the License. 4cac7dca0Sopenharmony_ci// You may obtain a copy of the License at 5cac7dca0Sopenharmony_ci// 6cac7dca0Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 7cac7dca0Sopenharmony_ci// 8cac7dca0Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 9cac7dca0Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 10cac7dca0Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11cac7dca0Sopenharmony_ci// See the License for the specific language governing permissions and 12cac7dca0Sopenharmony_ci// limitations under the License. 13cac7dca0Sopenharmony_ci 14cac7dca0Sopenharmony_ciuse std::os::windows::io::RawHandle; 15cac7dca0Sopenharmony_ci 16cac7dca0Sopenharmony_ciuse crate::sys::winapi::{CloseHandle, HANDLE}; 17cac7dca0Sopenharmony_ci 18cac7dca0Sopenharmony_ci/// Ensures that the handle can be closed correctly when it is not needed 19cac7dca0Sopenharmony_ci#[derive(Debug)] 20cac7dca0Sopenharmony_cipub(crate) struct Handle(HANDLE); 21cac7dca0Sopenharmony_ci 22cac7dca0Sopenharmony_ciimpl Handle { 23cac7dca0Sopenharmony_ci pub(crate) fn new(handle: HANDLE) -> Self { 24cac7dca0Sopenharmony_ci Self(handle) 25cac7dca0Sopenharmony_ci } 26cac7dca0Sopenharmony_ci 27cac7dca0Sopenharmony_ci pub(crate) fn raw(&self) -> HANDLE { 28cac7dca0Sopenharmony_ci self.0 29cac7dca0Sopenharmony_ci } 30cac7dca0Sopenharmony_ci 31cac7dca0Sopenharmony_ci pub(crate) fn into_raw(self) -> RawHandle { 32cac7dca0Sopenharmony_ci let ret = self.0; 33cac7dca0Sopenharmony_ci // This is super important so that drop is not called! 34cac7dca0Sopenharmony_ci std::mem::forget(self); 35cac7dca0Sopenharmony_ci ret as RawHandle 36cac7dca0Sopenharmony_ci } 37cac7dca0Sopenharmony_ci} 38cac7dca0Sopenharmony_ci 39cac7dca0Sopenharmony_ciimpl Drop for Handle { 40cac7dca0Sopenharmony_ci fn drop(&mut self) { 41cac7dca0Sopenharmony_ci unsafe { CloseHandle(self.0) }; 42cac7dca0Sopenharmony_ci } 43cac7dca0Sopenharmony_ci} 44