cxx_qt/connectionguard.rs
1// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use crate::QMetaObjectConnection;
7
8/// Represents a guard to a signal-slot (or signal-functor) connection.
9///
10/// This struct can be created from a [`QMetaObjectConnection`].
11///
12/// Note that when this struct is dropped the connection is disconnected.
13/// So to keep a connection active either hold onto the struct for the duration
14/// that the connection should be active or call `release`, hence the `#[must_use]`.
15#[must_use]
16pub struct QMetaObjectConnectionGuard {
17 connection: QMetaObjectConnection,
18}
19
20impl From<QMetaObjectConnection> for QMetaObjectConnectionGuard {
21 fn from(connection: QMetaObjectConnection) -> Self {
22 Self { connection }
23 }
24}
25
26impl Drop for QMetaObjectConnectionGuard {
27 /// Disconnect and deconstruct the connection.
28 fn drop(&mut self) {
29 self.connection.disconnect();
30 }
31}
32
33impl QMetaObjectConnectionGuard {
34 /// Release the connection without disconnecting.
35 pub fn release(mut self) -> QMetaObjectConnection {
36 // Take the connection as our Drop implementation disconnects automatically
37 // whereas we just want to release
38 core::mem::take(&mut self.connection)
39 }
40}