// Copyright (c) 2023 Huawei Device Co., Ltd. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::os::windows::io::RawHandle; use crate::sys::winapi::{CloseHandle, HANDLE}; /// Ensures that the handle can be closed correctly when it is not needed #[derive(Debug)] pub(crate) struct Handle(HANDLE); impl Handle { pub(crate) fn new(handle: HANDLE) -> Self { Self(handle) } pub(crate) fn raw(&self) -> HANDLE { self.0 } pub(crate) fn into_raw(self) -> RawHandle { let ret = self.0; // This is super important so that drop is not called! std::mem::forget(self); ret as RawHandle } } impl Drop for Handle { fn drop(&mut self) { unsafe { CloseHandle(self.0) }; } }