nautilus_core/drop.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Explicit, manually-invocable cleanup hook used to break reference cycles before `Drop`.
17//!
18//! Many long-lived components register callbacks or handlers that retain strong references back to
19//! them, creating reference-count cycles that prevent Rust’s automatic destructor (`Drop`) from
20//! running. The `CleanDrop` trait provides an *object-safe* method, `clean_drop`, that can be
21//! called explicitly (e.g. during an orderly shutdown) to release such resources. Implementations
22//! should also call `clean_drop` from their `Drop` impl as a final safety net.
23//!
24//! Design contract:
25//! 1. **Idempotent** – multiple calls must be safe.
26//! 2. Perform all externally-observable cleanup here (unregister handlers, abort tasks, clear
27//! callbacks, downgrade `Rc`/`Arc` references, etc.).
28
29/// Trait providing an explicit cleanup method that may be invoked prior to `Drop`.
30pub trait CleanDrop {
31 /// Perform custom cleanup, releasing external resources and breaking strong reference cycles.
32 fn clean_drop(&mut self);
33}